9 Copyright © Oracle Corporation, 2002. All rights reserved. Optimizing Sort Operations 9-2 Copyright © Oracle Corporation, 2002. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • List the operations that use temporary space • Create and monitor temporary tablespaces • Identify actions that use the temporary tablespace • Describe the use of disk and memory for sorting • Identify the SQL operations that require sorts • Differentiate between disk and memory sorts • List ways to reduce total sorts and disk sorts • Determine the number of memory sorts performed • Set parameters to optimize sorts 9-3 Copyright © Oracle Corporation, 2002. All rights reserved. Automatic Sort Area Management • Parameters for automatic sort area management: – PGA_AGGREGATE_TARGET (Ranges from 10 MB to 4000 GB) – WORKAREA_SIZE_POLICY – AUTO | MANUAL • Replaces all *_AREA_SIZE parameters 9-4 Copyright © Oracle Corporation, 2002. All rights reserved. PGA Management Resources Statistics to manage the PGA_AGGREGATE_TARGET initialization parameter • Views for monitoring the PGA work area include: – v$sql_workarea_histogram – v$pgastat – v$sql_workarea_active – v$sql_workarea – v$tempseg_usage • Views to assist in sizing the PGA work area are: – v$pga_target_advice – v$pga_target_advice_histogram 9-5 Copyright © Oracle Corporation, 2002. All rights reserved. Work Area Groups and PGA Cache Hit Percentages … 64-128KB Percent executions 100% 0% 0% 128-256KB … 94% 6% 0% 97% 3%0% 64-128MB Optimal size One-pass size Multi-pass size 9-6 Copyright © Oracle Corporation, 2002. All rights reserved. Determining PGA Workload SQL> SELECT low_optimal_size/1024 AS low_kb, 2 (high_optimal_size+1)/1024 AS high_kb, 3 ROUND(100*optimal_executions 4 /total_executions) AS optimal, 5 ROUND(100*onepass_executions 6 /total_executions) AS onepass, 7 ROUND(100*multipasses_executions 8 /total_executions) AS multipass 9 FROM v$sql_workarea_histogram 10 WHERE total_executions != 0 11 ORDER BY low_kb; 9-7 Copyright © Oracle Corporation, 2002. All rights reserved. Other Views for Work Areas SQL> SELECT * 2 FROM v$pgastat 3 WHERE name = 'cache hit percentage'; NAME VALUE UNIT cache hit percentage 93 percent 9-8 Copyright © Oracle Corporation, 2002. All rights reserved. SQL> SELECT ROUND(pga_target_for_estimate 2 /1024/1024) AS target_mb, 3 estd_pga_cache_hit_percentage AS 4 cache_hit_percent, estd_overalloc_count 5 FROM v$pga_target_advice 6 ORDER BY target_mb; Querying v$pga_target_advice 9-9 Copyright © Oracle Corporation, 2002. All rights reserved. Understanding v$pga_target_advice TARGET_MB CACHE_HIT_PERCENT ESTD_OVERALLOC_COUNT 63 23 367 125 24 30 250 30 3 375 39 1 500 58 0 600 59 0 700 59 0 800 60 0 900 60 0 1000 61 0 1500 67 0 2000 76 0 3000 83 0 4000 85 0 9-10 Copyright © Oracle Corporation, 2002. All rights reserved. PGA Sizing Advisor Output in Oracle Enterprise Manager [...]... Tools v $sort_ usage Server process Sort area (UGA) Sort in memory v $sort_ segment PCTINCREASE TEMPORARY INITIAL tablespace NEXT Sort on disk v$sysstat SORT_ AREA_SIZE SORT_ AREA_RETAINED_SIZE Statspack 9-24 Copyright © Oracle Corporation, 2002 All rights reserved Diagnostics and Guidelines • • In an OLTP system the ratio of disk sorts to memory sorts should be less than 5% Increase the value of SORT_ AREA_SIZE... Stack space Sort Area and Parameters • • An execution plan can contain multiple sorts A single server performing a sort needs: – An area of SORT_ AREA_SIZE, in bytes – At least one area of SORT_ AREA_RETAINED_SIZE for a join sort • • Each parallel query server needs SORT_ AREA_SIZE Two sets of servers can be writing at once, so: – Calculate SORT_ AREA_SIZE × 2 × degree of parallelism – Add SORT_ AREA_RETAINED_SIZE... space segment: • Is created by the first sort • Extends as demands are made on it • Comprises extents, which can be used by different sorts • Is described in the sort extent pool (SEP) 9-19 Copyright © Oracle Corporation, 2002 All rights reserved Operations Requiring Sorts Some of the operations that may require sorts are: • Index creation • Parallel insert operations involving index maintenance •...Overview The automatic sort area management feature is: • Easier to set up and size than the * _SORT_ AREA parameters • Easier to monitor using the advisory view 9-11 Copyright © Oracle Corporation, 2002 All rights reserved The Sorting Process If sort space requirement is greater than SORT_ AREA_SIZE: Sort run 1 Sort run 2 TEMPORARY tablespace Temporary segment Server process 9-12 Sort run 2 Segments hold... Identify actions that use the temporary tablespace • Describe the use of disk and memory for sorting • Identify the SQL operations that require sorts • Differentiate between disk and memory sorts • List ways to reduce total sorts and disk sorts • Determine the number of memory sorts performed • Set parameters to optimize sorts 9-31 Copyright © Oracle Corporation, 2002 All rights reserved ... degree of parallelism – Add SORT_ AREA_RETAINED_SIZE × degree of parallelism × number of sorts above two 9-15 Copyright © Oracle Corporation, 2002 All rights reserved Tuning Sorts • • • • 9-17 Use automatic sort area management Avoid sort operations whenever possible Reduce swapping and paging by making sure that sorting is done in memory when possible Reduce space allocation calls by allocating temporary... data while the server works on another sort run Copyright © Oracle Corporation, 2002 All rights reserved Sort Area and Parameters The sort space is in: • The PGA for a dedicated server connection PGA UGA Shared pool • User Stack Cursor session space state data The shared pool for Oracle Shared Server connection PGA UGA User session data 9-13 Sort area Cursor state Sort area Copyright © Oracle Corporation,... v$sysstat m, v$sysstat d WHERE m.name = 'sorts (memory)' AND d.name = 'sorts (disk)'; Disk Mem Ratio 23 206 11.165049 9-26 Copyright © Oracle Corporation, 2002 All rights reserved Performance Manager: Sorts 9-27 Copyright © Oracle Corporation, 2002 All rights reserved Monitoring Temporary Tablespaces • • Default storage parameters apply to sort segments Sort segments have unlimited extents... index maintenance • ORDER BY or GROUP BY clauses • DISTINCT values selection • UNION, INTERSECT, or MINUS operators • Sort- merge joins • ANALYZE command execution 9-20 Copyright © Oracle Corporation, 2002 All rights reserved Avoiding Sorts Avoid sort operations whenever possible: • Use NOSORT to create indexes • • • • • 9-22 Use UNION ALL instead of UNION Use index access for table joins Create indexes... max _sort_ blocks 4 FROM v $sort_ segment; TABLESPACE_NAME CURRENT_USERS TOTAL_EXTENTS USED_EXTENTS EXTENT_HITS MAX_USED_BLOCKS MAX _SORT_ BLOCKS - - - - TEMP 2 4 3 20 200 200 9-28 Copyright © Oracle Corporation, 2002 All rights reserved Temporary Tablespace Configuration • • Set appropriate storage values Set up different temporary tablespaces based on sorting . for sorting • Identify the SQL operations that require sorts • Differentiate between disk and memory sorts • List ways to reduce total sorts and disk sorts • Determine the number of memory sorts. Tools v$sysstat Sort area (UGA) SORT_ AREA_SIZE SORT_ AREA_RETAINED_SIZE Server process TEMPORARY tablespace Sort on disk Sort in memory PCTINCREASE INITIAL NEXT Statspack v $sort_ usage v $sort_ segment . multiple sorts • A single server performing a sort needs: – An area of SORT_ AREA_SIZE, in bytes – At least one area of SORT_ AREA_RETAINED_SIZE for a join sort • Each parallel query server needs SORT_ AREA_SIZE • Two