Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sydney Oracle Meetup - access paths

690 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Sydney Oracle Meetup - access paths

  1. 1. <ul><li>Presenter: Paul Guerin OCP </li></ul><ul><ul><li>Meetup presenter 4 times in past 10 months </li></ul></ul><ul><li>Development/Production DBA at Origin Energy for past 3.5 years </li></ul><ul><ul><li>Previous employers: Bluescope Steel (Wollongong), BHP Billiton (Wollongong) </li></ul></ul><ul><li>2010 Oxfam Trailwalker: Origin Trail Breakers </li></ul><ul><li>http://www2.oxfam.org.au/trailwalker/Sydney/team/67 </li></ul><ul><ul><li>Includes photos from Heysen trail and Flinders rangers…. </li></ul></ul>DB performance - Access paths
  2. 2. Access paths overview <ul><li>Access paths are ways in which data is retrieved from the database. </li></ul><ul><li>The query optimizer chooses an access path based on : </li></ul><ul><ul><li>The available access paths for the statement. eg WHERE and FROM clauses. </li></ul></ul><ul><ul><li>All estimated costs of executing the statement, using each access path or combination of paths. </li></ul></ul><ul><li>The optimizer chooses the execution plan with the lowest estimated cost. </li></ul><ul><ul><li>Estimated cost is based on estimated blocks accessed, not rows accessed. </li></ul></ul><ul><ul><li>Optimiser hints can influence the plan chosen. </li></ul></ul><ul><ul><li>System statistics and system parameters. </li></ul></ul><ul><ul><li>Statistics for the index, columns, and tables. </li></ul></ul>
  3. 3. Full Table Scans <ul><li>This type of scan reads all rows from a table and filters out those that do not meet the WHERE clause. </li></ul><ul><ul><li>All blocks under the high water mark are scanned. </li></ul></ul><ul><ul><ul><li>Note: The high water mark indicates the amount of used space, or space that had been formatted to receive data. </li></ul></ul></ul><ul><li>The blocks are adjacent and read sequentially. </li></ul><ul><ul><li>Note: The size of the read calls range from one block to the number of blocks indicated by the initialization parameter DB_FILE_MULTIBLOCK_READ_COUNT (or extent size). </li></ul></ul>
  4. 4. Full Table Scans <ul><li>Simple example: </li></ul><ul><li>/* EMP_ID is the primary key */ </li></ul><ul><li>select * from employees; </li></ul><ul><li>select * from employees where EMP_ID > 15; </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 53M| 3645M| 19314 (22)| </li></ul><ul><li>|* 1 | TABLE ACCESS FULL | EMPLOYEES | 53M| 3645M| 19314 (22)| </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 208.14 376.56 621870 1146626 0 53059049 <- 5 hrs </li></ul><ul><li>Another simple example : 6 minute full table scan reduced to 40 seconds!!! </li></ul>
  5. 5. Index Scans <ul><li>Overview </li></ul><ul><li>A row is retrieved by traversing the index, using the indexed column values specified by the statement. </li></ul><ul><li>An index scan retrieves data from an index based on the value of one or more columns in the index. </li></ul><ul><li>If all referenced columns are in the index then the table is not accessed. </li></ul>
  6. 6. Rowid Scans <ul><li>The index contains not only the indexed value, but also the rowids of rows in the table having that value. </li></ul><ul><li>The rowid of a row specifies the data file and data block containing the row and the location of the row in that block. </li></ul><ul><li>Locating a row by specifying its rowid is the fastest way to retrieve a single row, because the exact location of the row in the database is specified. </li></ul><ul><li>When the Optimizer Uses Rowids </li></ul><ul><li>This is generally the second step after retrieving the rowid from an index. The table access might be required for any columns in the statement not present in the index. </li></ul><ul><li>Access by rowid does not need to follow every index scan. If the index contains all the columns needed for the statement, then table access by rowid might not occur. </li></ul>
  7. 7. Index Unique Scans <ul><li>This scan returns, at most, a single rowid: </li></ul><ul><ul><li>If a statement contains a UNIQUE or a PRIMARY KEY constraint that guarantees that only a single row is accessed. </li></ul></ul><ul><ul><li>Statement requires all columns of a unique (B-tree) index or primary key constraint </li></ul></ul><ul><ul><li>equality conditions. eg LIKE, IN, = </li></ul></ul><ul><li>/* EMP_ID is the primary key */ </li></ul><ul><li>select * from employees where EMP_ID = 100; </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 1 | 72 | 4 (25)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 72 | 4 (25)| </li></ul><ul><li>|* 2 | INDEX UNIQUE SCAN | EMP_PK | 1 | | 3 (34)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul>
  8. 8. Index Range Scans <ul><li>An index range scan is a common operation for accessing selective data. </li></ul><ul><ul><li>It can be bounded (bounded on both sides) or unbounded (on one or both sides). </li></ul></ul><ul><ul><li>Data is returned in the ascending order of index columns. </li></ul></ul><ul><ul><li>If an index can satisfy an ORDER BY clause, then the optimizer uses this option and avoids a sort. </li></ul></ul><ul><li>/* EMP_ID is the primary key */ </li></ul><ul><li>select * from employees where EMP_ID < 100; </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 1 | 72 | 5 (20)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 1 | 72 | 5 (20)| </li></ul><ul><li>|* 2 | INDEX RANGE SCAN | EMP_PK | 1 | | 4 (25)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul>
  9. 9. Index Range Scans Descending <ul><li>An index range scan descending is identical to an index range scan, except that the data is returned in descending order. </li></ul><ul><li>The optimizer uses index range scan descending when an an index can satisfy an order by descending clause. </li></ul><ul><li>/* EMP_ID is the primary key */ </li></ul><ul><li>select * from employees where EMP_ID < 100 order by EMP_ID desc; </li></ul><ul><li>-------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>-------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 1 | 72 | 5 (20)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID | EMPLOYEES | 1 | 72 | 5 (20)| </li></ul><ul><li>|* 2 | INDEX RANGE SCAN DESCENDING | EMP_PK | 1 | | 4 (25)| </li></ul><ul><li>-------------------------------------------------------------------------------------- </li></ul>
  10. 10. Index Skip Scans <ul><li>Skip scanning lets a composite index be split logically into smaller subindexes. In skip scanning, the initial column of the composite index is not specified in the query. In other words, it is skipped. </li></ul><ul><li>Note: Can skip more than 1 leading column of the index. </li></ul><ul><li>The database determines the number of logical subindexes by the number of distinct values in the initial column. </li></ul><ul><li>Skip scanning is advantageous when there are few distinct values in the leading column of the composite index and many distinct values in the nonleading key of the index. </li></ul>
  11. 11. Index Skip Scans <ul><li>Select * from employees where ID=155222; </li></ul><ul><li>/* index on (CHANGED,ID) and CHANGED has 3 distinct values */ </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 384 | 27648 | 5 (20)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 384 | 27648 | 5 (20)| </li></ul><ul><li>|* 2 | INDEX SKIP SCAN | EMP$CHANGED | 384 | | 4 (25)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 2.92 5.38 5353 20747 0 307106 </li></ul><ul><li>/* index on (ID,CHANGED) */ </li></ul><ul><li>----------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>----------------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 384 | 27648 | 18 (6)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 384 | 27648 | 18 (6)| </li></ul><ul><li>|* 2 | INDEX RANGE SCAN | EMP$ID | 384 | | 7 (15)| </li></ul><ul><li>----------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 0.70 1.59 2024 2025 0 1 </li></ul>
  12. 12. Index Skip Scans <ul><li>Select * from employees where ID=155222; </li></ul><ul><li>/* index on (CHANGED,ID) and CHANGED has 3 distinct values */ </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 384 | 27648 | 5 (20)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 384 | 27648 | 5 (20)| </li></ul><ul><li>|* 2 | INDEX SKIP SCAN | EMP$CHANGED | 384 | | 4 (25)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 2.92 5.38 5353 20747 0 307106 </li></ul><ul><li>/* index on (ID,CHANGED) */ </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 384 | 27648 | 18 (6)| </li></ul><ul><li>| 1 | TABLE ACCESS BY INDEX ROWID| EMPLOYEES | 384 | 27648 | 18 (6)| </li></ul><ul><li>|* 2 | INDEX RANGE SCAN | EMP$ID | 384 | | 7 (15)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 2.85 4.47 5393 11530 0 307106 </li></ul>
  13. 13. Full index scans <ul><li>A full index scan eliminates a sort operation. The database uses a full index scan in any of the following situations: </li></ul><ul><li>An ORDER BY clause that meets the following requirements is present in the query: </li></ul><ul><ul><li>All of the columns in the ORDER BY clause must be in the index. </li></ul></ul><ul><ul><li>The order of the columns in the ORDER BY clause must match the order of the leading index columns. </li></ul></ul><ul><li>The query requires a sort merge join and the query meets the following requirements: </li></ul><ul><ul><li>All of the columns referenced in the query must be in the index. </li></ul></ul><ul><ul><li>The order of the columns referenced in the query must match the order of the leading index columns. </li></ul></ul><ul><li>A GROUP BY clause is present in the query, and the columns in the GROUP BY clause are present in the index. </li></ul><ul><ul><li>The columns do not need to be in the same order in the index and the GROUP BY clause. </li></ul></ul>
  14. 14. Full index scans <ul><li>Select id,count(CHANGED) from employees group by ID; </li></ul><ul><li>/* index on (id,changed) */ </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4850K| 347K (1)| </li></ul><ul><li>| 1 | SORT GROUP BY NOSORT| | 137K| 4850K| 347K (1)| </li></ul><ul><li>| 2 | INDEX FULL SCAN | EMP$ID1 | 53M| 1820M| 347K (1)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 114.50 295.62 347167 350487 0 183292 <- 6 minutes </li></ul><ul><li>/* index on (id,revision) */ </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4849K| | 427K (4)| </li></ul><ul><li>| 1 | SORT GROUP BY | | 137K| 4849K| 2248M| 427K (4)| </li></ul><ul><li>| 2 | TABLE ACCESS FULL | EMPLOYEES | 53M| 1823M| | 18697 (19)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul>
  15. 15. Full index scans <ul><li>Select id,count(CHANGED) from employees group by ID; </li></ul><ul><li>/* index on (id,changed) */ </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4850K| 347K (1)| </li></ul><ul><li>| 1 | SORT GROUP BY NOSORT| | 137K| 4850K| 347K (1)| </li></ul><ul><li>| 2 | INDEX FULL SCAN | EMP$ID1 | 53M| 1820M| 347K (1)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 114.50 295.62 347167 350487 0 183292 <- 6 minutes </li></ul><ul><li>/* index on (id,revision) */ </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4849K| | 427K (4)| </li></ul><ul><li>| 1 | SORT GROUP BY | | 137K| 4849K| 2248M| 427K (4)| </li></ul><ul><li>| 2 | TABLE ACCESS FULL | EMPLOYEES | 53M| 1823M| | 18697 (19)| <- 5 hrs </li></ul><ul><li>----------------------------------------------------------------------------------- </li></ul>
  16. 16. Fast Full Index Scans <ul><li>Fast full index scans are an alternative to a full table scan when the index contains all the columns that are needed for the query, and at least one column in the index key has the NOT NULL constraint. </li></ul><ul><li>A fast full scan accesses the data in the index itself, without accessing the table. The database cannot use this scan to eliminate a sort operation because the data is not ordered by the index key. The database reads the entire index using multiblock reads, unlike a full index scan, and can scan in parallel. </li></ul><ul><li>A fast full scan is faster than a normal full index scan because it can use multiblock I/O and can run in parallel just like a table scan. </li></ul>
  17. 17. Fast Full Index Scans <ul><li>Select ID, count(CHANGED) from employees group by ID order by ID desc ; </li></ul><ul><li>/* index on (ID, CHANGED) */ </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4843K| | 418K (4)| </li></ul><ul><li>| 1 | SORT GROUP BY | | 137K| 4843K| 2246M| 418K (4)| </li></ul><ul><li>| 2 | INDEX FAST FULL SCAN | EMP$ID2 | 53M| 1821M| | 10096 (15)| </li></ul><ul><li>-------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 173.39 190.97 351251 350838 9 183292 < ~4 minutes </li></ul><ul><li>/* index on (ID desc, CHANGED) */ </li></ul><ul><li>------------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| </li></ul><ul><li>| 0 | SELECT STATEMENT | | 137K| 4847K| | 755K (2)| </li></ul><ul><li>| 1 | SORT GROUP BY | | 137K| 4847K| 2246M| 755K (2)| </li></ul><ul><li>| 2 | INDEX FULL SCAN | EMP$ID3 | 53M| 1821M| | 347K (1)| </li></ul><ul><li>------------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>total 157.17 335.36 349129 348667 9 183292 < ~6 minutes </li></ul>
  18. 18. Fast Full Index Scans <ul><li>/* index on (CHANGED, ID) */ </li></ul><ul><li>select ID,CHANGED from employees where CHANGED='PAUL'; </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 26M| 910M| 10071 (15)| </li></ul><ul><li>|* 1 | INDEX FAST FULL SCAN | EMP$CHANGE | 26M| 910M| 10071 (15)| </li></ul><ul><li>------------------------------------------------------------------------------------- </li></ul><ul><li>call cpu elapsed disk query current rows </li></ul><ul><li>------- -------- ---------- ---------- ---------- ---------- ---------- </li></ul><ul><li>total 16.56 41.97 350792 350841 0 0 <- 42 seconds </li></ul><ul><li>select * from employees where CHANGED='PAUL'; </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul><ul><li>| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul><ul><li>| 0 | SELECT STATEMENT | | 26M| 1821M| 18958 (20)| </li></ul><ul><li>|* 1 | TABLE ACCESS FULL | EMPLOYEES | 26M| 1821M| 18958 (20)| <- 5 hours </li></ul><ul><li>--------------------------------------------------------------------------- </li></ul>
  19. 19. Other types <ul><li>Index Joins </li></ul><ul><li>An index join is a hash join of several indexes that together contain all the table columns referenced in the query. If the database uses an index join, then table access is not needed because the database can retrieve all the relevant column values from the indexes. The database cannot use an index join cannot to eliminate a sort operation. </li></ul><ul><li>Bitmap Indexes </li></ul><ul><li>A bitmap join uses a bitmap for key values and a mapping function that converts each bit position to a rowid. Bitmaps can efficiently merge indexes that correspond to several conditions in a WHERE clause, using Boolean operations to resolve AND and OR conditions. </li></ul><ul><li>Cluster Access </li></ul><ul><li>The database uses a cluster scan to retrieve all rows that have the same cluster key value from a table stored in an indexed cluster. In an indexed cluster, the database stores all rows with the same cluster key value in the same data block. To perform a cluster scan, Oracle Database first obtains the rowid of one of the selected rows by scanning the cluster index. Oracle Database then locates the rows based on this rowid. </li></ul><ul><li>Hash Access </li></ul><ul><li>The database uses a hash scan to locate rows in a hash cluster based on a hash value. In a hash cluster, all rows with the same hash value are stored in the same data block. To perform a hash scan, Oracle Database first obtains the hash value by applying a hash function to a cluster key value specified by the statement. Oracle Database then scans the data blocks containing rows with that hash value. </li></ul>
  20. 20. Other types <ul><li>Sample Table Scans </li></ul><ul><li>A sample table scan retrieves a random sample of data from a simple table or a complex SELECT statement, such as a statement involving joins and views. The database uses this access path when a statement's FROM clause includes the SAMPLE clause or the SAMPLE BLOCK clause. To perform a sample table scan when sampling by rows with the SAMPLE clause, the database reads a specified percentage of rows in the table. To perform a sample table scan when sampling by blocks with the SAMPLE BLOCK clause, the database reads a specified percentage of table blocks. </li></ul><ul><li>/* access 1% of the employees table, sampling by blocks */ </li></ul><ul><li>SELECT * FROM employees SAMPLE BLOCK (1); </li></ul>

×