Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 26 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
26
Dung lượng
155,6 KB
Nội dung
CreatingIndexes 15 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć2 CreatingIndexes 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 CreatingIndexes 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. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć6 CreatingIndexes 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. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć8 CreatingIndexes 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. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder15Ć10 [...]... SQL Reference, Release 7.3, “CREATE INDEX.” CreatingIndexes 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 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 rows D The table is updated frequently CreatingIndexes 15Ć15 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 in an index by querying the USER_IND_COLUMNS view Example Display all previously created indexes, affected column names, and uniqueness... Overview In this 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 indexesCreatingIndexes 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 index on LAST_NAME... 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 CreatingIndexes 15Ć21 15Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure... 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 in a table definition are created automatically Users can create non-unique indexes to speed up searches by... uniqueness check purposes The columns need not be adjacent Index types are not mutually exclusive For example, you can create a unique, concatenated index CreatingIndexes 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]... 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 ––––––––––– ID LAST_NAME USERID COL_POS –––––––– 1 1 1 UNIQUENESS –––––––––– UNIQUE NONUNIQUE... Procedure Builder Removing an Index You 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 where: index Creating Indexes index; is the name of the index 15Ć19 15Ć20 Introduction... 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... 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 column contains a wide range of values D The column contains a large number . PL/SQL Using Procedure Builder15Ć16 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