1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu creating indexes docx

26 227 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 26
Dung lượng 187 KB

Nội dung

Creating Indexes 15 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć2 Schedule: Timing Topic 25 minutes Lecture 25 minutes Practice 50 minutes Total Class Management Note: Files required for this lesson are: Demonstration: None Practice: None Creating Indexes 15Ć3 Objectives If you want to improve the performance of some queries, you should consider creating an index. You can also use indexes to enforce uniqueness on a column or a collection of columns. At the end of this lesson, you should be able to D Distinguish between the indexes that are created automatically and those that are created manually. D Identify the uses for indexes. D Explain the index structure and why it improves query speed. D Create a non-unique index. D Remove an index from the data dictionary. D Evaluate guidelines for creating and using indexes. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć4 Creating Indexes 15Ć5 Overview An Oracle7 Server index is a database object that can speed up the retrieval of rows by using a pointer. Indexes can be created explicitly or automatically. They are transparent to the user. If you do not have an index on the column, then a full table scan will occur. What Is an Index? An index is a database object that provides direct and fast access to rows in a table. Its purpose is to reduce the necessity of disk I/O by using a B*Tree indexed path to locate data quickly. The index is automatically used and maintained by the Oracle7 Server. Once an index is created, no direct activity is required by the user. Indexes are logically and physically independent of the table they index. This means that they can be created or dropped at any time and have no effect on the base tables or other indexes. How Are Indexes Created? Two types of indexes can be created. One type is a unique index. The Oracle7 Server automatically creates this index when you define a column in a table to have a PRIMARY KEY or a UNIQUE constraint. The name of the index is the name given to the constraint. The other type of index a user can create is a non-unique index. For example, you can create a FOREIGN KEY column index for a join in a query to improve retrieval speed. For more information, see Oracle7 Server Concepts Manual, Release 7.3, “Schema Objects” section, “Indexes” topic. Technical Note: The decision to create indexes is a global, high-level decision. Creation and maintenance of indexes is often a DBA task. Reference the column that has an index in the predicate WHERE clause without modifying the indexed column with a function or expression. A ROWID is a hexadecimal string representation of the row address containing block identifier, row location in the block, and the database file identifier. The fastest way to access any particular row is by referencing its ROWID. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć6 Creating Indexes 15Ć7 When Is the Index Used? Once the index has been created, the Oracle7 Server will use it whenever possible to speed up access to the data. Note that this use is automatic and usually requires no action by the user. A brief guideline is provided below on how the Server determines to use the index. Optimization Techniques When an index is used depends partly on the Oracle Optimizer being used at the time. The Oracle7 Server uses both rule-based and cost-based optimization. Rule-based optimization is when the Oracle7 Server decides when it is appropriate to use an index based on its internal rules. The Server identifies the columns that are indexed and the index types. The cost-based optimization method uses statistics about tables along with information about available indexes to select an execution plan for the SQL statements. For more information, see Tune Oracle7 Applications course description. Technical Note: Some developers and DBAs create indexes for specific reporting runs, run the reports, and then delete the indexes. Large indexes can be extremely storage-intensive. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć8 Creating Indexes 15Ć9 Index Structure An index is an optional structure that is independent of the table structure. Each index is composed of column values that the index is on and pointers (or ROWIDs) to the row containing that value. The pointer directly points to the appropriate row in the table, therefore avoiding a full table scan. B*Tree The Oracle7 Server uses a balanced B*tree index structure. This is a binary, self-balancing search structure to equalize access times to any row. It is an efficient method of ensuring that access to any specified value will take approximately the same time whether the row is at the beginning, middle, or end of the table. Each index that the Oracle7 Server builds consists of a number of pages (or branches) of storage arranged in a tree. Each page (or branch) holds a series of key values and pointers to pages (or branches) lower in the structure until eventually the key values indicate the location of the data itself. The location identifier at the database level is called a ROWID. Class Management Note: Looking at the accompanying graphic of the S_EMP table, suppose there were 20,000 employee rows in the table and a SELECT * FROM S_EMP WHERE LAST_NAME = ’Menchu’ statement was executed. That would amount to 20,000 potential I/Os even if the only Menchu in the table was hit on the first read because the whole table is scanned. However, with an index on the LAST_NAME column, a shorter search would occur. Starting at the root (or top) of the index, the Server decides which side of the root the value Menchu falls. It continues on the appropriate page (or branch) until it identifies the block that contains Menchu. It accesses the ROWID to point directly to the row location of the Menchu. Therefore, only four potential I/Os occurred instead of a potential 20,000. Technical Note: An index does not have to be in the same tablespace as the associated table. Furthermore, performance of queries that use an index can be improved by storing an index and its table in different tablespaces located on different disk drives because both the index and the table data can be retrieved in parallel. When an index is created, the columns to be indexed are fetched and sorted, and the ROWID is stored along with the index value for each row. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć10 Class Management Note: Primary keys can be a combination of up to 16 column values. It is therefore important that an index can check across those columns for unique combinations of values. [...]... automatically Users can create non-unique indexes to speed up searches by using the CREATE INDEX command Indexes are maintained automatically by the Oracle7 Server Users can view the definitions of the indexes in the USER _INDEXES data dictionary view An index can be dropped by the creator or a user with the DROP ANY INDEX privilege by using the DROP INDEX command Creating Indexes 15Ć21 15Ć22 Introduction to... practice you will create an index on the WORKER table Practice Contents D Creating non-unique indexes D Displaying data dictionary information about the index D Dropping indexes Class Management Note: Duration: 25 minutes Creating Indexes 15Ć23 15Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 15 1 Would any indexes specified be used with the following queries and why? a Non-unique... automatically dropped, but views and sequences remain Creating Indexes 15Ć19 15Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary Indexes are used to improve the query retrieval speed They are database objects and therefore take up disk space Indexes use the B*tree search method to retrieve the pointer to the rows in the tables Unique indexes for the PRIMARY KEY and UNIQUE KEY constraints... do not explicitly define UNIQUE indexes on tables Instead define uniqueness in the table as a constraint Oracle7 Server enforces UNIQUE integrity constraints by automatically defining a unique index on the unique key Creating Indexes 15Ć13 15Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating an Index continued More Is Not Always Better More indexes on a table does not mean... use an index if the WHERE clause contains the IS NULL expression Creating Indexes 15Ć15 Class Management Note: Remind students that the value for the TABLE_NAME is stored in uppercase in the data dictionary 15Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Confirming Indexes Confirm the existence of indexes from the USER _INDEXES data dictionary view You can also check the columns involved... all previously created indexes, affected column names, and uniqueness on the S_EMP table SQL> 2 3 4 5 SELECT FROM WHERE AND ic.index_name, ic.column_name, ic.column_position col_pos, ix.uniqueness user _indexes ix, user_ind_columns ic ic.index_name = ix.index_name ic.table_name = ’S_EMP’; INDEX_NAME –––––––––––––––––––– S_EMP_ID_PK S_EMP_LAST_NAME_IDX S_EMP_USERID_UK Creating Indexes COLUMN_NAME –––––––––––... Always Better More indexes on a table does not mean it will speed up queries Each DML operation that is committed on a table with indexes means that the indexes must be updated The more indexes you have associated with a table, the more effort the Server must make to update all the indexes after a DML When to Create an Index D The column is used frequently in the WHERE clause or in a join condition D The... The order of the columns in a concatenated index is important The Server reads the columns left to right when choosing the efficiency for the index look up Creating Indexes 15Ć11 15Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating an Index Create an index on one or more columns by issuing the CREATE INDEX command Abridged Syntax CREATE INDEX index ON table (column[, column]... S_EMP.DEPT_ID Yes/No SQL> SELECT 2 FROM 3 WHERE Creating Indexes e.last_name, d.name s_emp e, s_dept d e.dept_id = d.id; 15Ć25 Practice 15 continued 2 Create a non-unique index on the foreign key column in the WORKER table 3 Since users will frequently query on the employee last name, create a non-unique index on that column in the WORKER table 4 Display the indexes and uniqueness that exist in the data... cannot modify indexes To change an index, you must drop it and then re-create it Remove an index definition from the data dictionary by issuing the DROP INDEX command In order to drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege Syntax DROP INDEX index; where: index is the name of the index Class Management Note: Remind students that if they drop a table, indexes and . uppercase in the data dictionary. Creating Indexes 15Ć17 Confirming Indexes Confirm the existence of indexes from the USER _INDEXES data dictionary view. You. Evaluate guidelines for creating and using indexes. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć4 Creating Indexes 15Ć5 Overview An

Ngày đăng: 21/12/2013, 06:17

TỪ KHÓA LIÊN QUAN

w