0% found this document useful (0 votes)
40 views

Oracle Tuning

Uploaded by

bembz87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
40 views

Oracle Tuning

Uploaded by

bembz87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 148
SQL Tuning & Performance Masterclass (2024) bese 0 3 Course Slide for Students This document is exclusively prepared for existing students to make a quick recap easily about what they have learned throughout the course! Pao nS ASTERCLASS What is SQL(Performance) Tuning and Why We Need? S ‘ & Whatis SQL Tuning? % Oracleis a Very Strong Database @ Better Performance - Less Hardware Cost @ SQL Tuning is the combination of some techniques | @ SQL Tuningis not so hard @ Who Will Do the Tuning? SQL TUNING ponaman ppeemeceseen What is SQL(Performance) Tuning and Why We Need? S s @ No Need to Scare SQL Tuning @ You Need to Know Database Architecture in Basic @ There will be many introductive lectures at first @ SQL Tuning Needs Expertise @ Some Tools will Not Be Explained l [ @ All The Subjects are Organized Strategically & Tosumup! SQL TUNING Coe ee eee oy Oracle Database Architecture ? MIC een ee es 2° Architecture Diagram SQL TUNING ponaman ppeemeceseen Oracle Database Architecture ? eens Oracle Database Architecture ? ere eae SQL TUNING CSc ena cao Oracle Database Architecture All the data is storedin Blocks A blockis the smallest unit of database storage A block can have 2KB to 32KB size (8KB default) A block stores row data or index data. RRAARAN Block Header Includes: + Block Type Information + Table Information + Row Directory @ Wecanuse PCTFREE or PCTUSE parameters to specify the space sizein blocks SQL TUNING CSc ena cao Oracle Database Architecture Pane Column Data _— ee Row Header <—— I [| a] Ld] a NON a “s+. Row Piece in a Database Block [ T ——- ee Ro Overhead roe see rcs 10 (fcustered) Chained Row ies ay) mn tenth Column value SQL TUNING = Poe ec) pe) y } J 1 ao. | 2 [psec ‘o> Oracle Database Architecture ‘ ‘ SESSION AREA + Stores session information of each user. SESSION AREA = Stores session variables, login information, session status, ete. @ PRIVATESQL AREA + Persistent area stores the bind variables Q + Runtime area stores the execution state Info. @ CURSORAREA + Stores the information of cursors @ SQLWORKAREA SQLTUNING Fon ~ Rete Oracle Database Architecture Ea @ DATADICTIONARY CACHE + Stores the definitions of the database objects and their permissions @ RESULTCACHE = Stores the result of commonly used queries = Stores the result of functions @ LIBRARYCACHE + Stores the execution plans J * Stores procedures, packages control structures i ; &% OTHERAREAS BS KS = SQL TUNING oe ec Oracle Database Architecture (ERs aed @ Largest memory area of SGA. @ Stores the copies of the data blocks read from the disc =. @ Why to read into the buffer cache? + Much faster than discs, gQ Maintained with a complex algorithm + Stores the most recently used & most touched ones, Database write process handles the Z write operations to the disc. gQ @ Stores the index data, too. SQL TUNING CSc ena cao Oracle Database Architecture QR @ CDN eased Oracle Guarantees Not to Lose Data A Redo Log Entryis created when insert, update, delete, create, alter, drop occurs Redo Log Entries has the changes made to the database They are used for recovery operations Redo Log Entries are stored in the Redo Log Buffer Redo Log Buffer is a circular Buffer Rollback is not done with Redo Log Data SQL TUNING = Poe ec) pe) Meee Oracle Database Architecture % The original data stored into the memory (undo tablespace) is called as undo data. @ Another copy of the datais storedinthe buffer cache for the modifications. 4 Undo data is not modified because: UPDATE * Used for rollback operations DML operations * Used for providing read consistency * Used for providing flashback feature at Lj % Blocks > Extents > Segments > Tablespaces SQL TUNING Coe ee eee oy Oracle Database Architecture ™% When we runa DMLcode, the server: Checks the Shared SQL. Area for similar statements to use. ‘Checks the Data Dictionary Cache and checks if our query is valid. Checks Buffer Cache & Undo Segments for the related data, Locks the related blocks. Makes the change to the blocks in the Buffer Cache. The changes are applied to the Redo Log Buffer before the Buffer Cache. ¥ The server returns the feedback for the change. ™@% When theusercommits: ¥ The server creates a commit record with SCN. ¥ The LGWr process writes redo log entries in the redo log buffer to the redo log files. ¥ The DBWn writes the dirty blocks to the disc & unlocks the blocks. v The server returns a feedback about the transaction completion. SQL TUNING CSc ena cao co. 4 Oracle Database Architecture @ The size of each memory area is important for the execution performance of your queries 4% Oracle can manage the memory . automatically @ Itcan manage both SGA and PGA memories ! @ It is recommended to leave automatic memory management enabled & Enabling automatic memory management will prevent out of memory errors SQL TUNING op a po Oracle Database Architecture @ Storage = Discs @ Control Files: Stores the pyhsical structure information of the database. @ Datafiles : Stores Data (Tables, procedures, application z data...) % Online Redo Log Files : Stores redo log entries Control es @ Archived Redo Log Files: Online redo log files are constantly moved here @ Backup Files : Stores the exact copy of the data files for ‘ Parameter le disaster recovery @ Parameter File : Stores the configure data of the database instance @ Password File: Stores the passwords of the admin users password sie sn aoe en ] (sysdba, sysoper, sysasm) ros Lm ke % AlertLog& Trace Files: Stores log messages and errors ‘occurredin the database. SQL TUNING CSc ena cao Oracle Database Architecture TeSeIe NEN ee eee Blocks : Smallest units of storage (2KB-32KB) Lest Se Extents : Combination of several Database |p consecutive data blocks. Used for storing — i 1. specific type of info. Tablespace = Segments: Combination of several extents. ce Used for storing some big data (tables, ogenaees[ Segment indexes, etc). Tablespaces : Combination of many Ss segments. Used for grouping the related data in one container J Ohare eieinnpoeodtatio Oracle data session i + Permanent Tablespace :Stores the persistent schema KS objects SQL TUNING Performance Tuning Basics CEERI = SQL Tuni is a continuous process = You need to tune your queries: + While creating + After the creation = When to decide tuning? + By checking the top consuming queries frequently + After any complaints of bad performance + The reasons of performance loss + soucuuatenanges —_— + chonaesonine sta volume + Applicaton Changes + Aged suse + Database parades + Database Parameterchances — operating Sytem & Harvane Changes SQL TUNING oe ee eee ~ Bee eee eee MASTERCLASS par Hast 1c ovata Performance Tuning Basics = Bad SQLs use more resources than necessary f§ PCT RTI KS ri + a = Characteristics of a Bad SQL 4 7 1 * Unnecessary parse time * Unnecessary VO operations " fr, + Unnecessary CPU time TONE UC TT ns 1a Te = Time on Wait (CPU) + Time on Execution = DB TIME = The reason of a Bad SQL: = Bad Design, Poor Coding, Inefficient Execution Plan Performance Tuning Basics = Select the data types carefully + Assign data types as much as needed + Select exactly the same data type between parent-child keys I = Enforce data integrity = Use normalization well * Select right table type = Create Clusters = Use indexes often and select index type carefully i = Create index-organized tables (IOT) SQL TUNING Ec eau cao Performance Tuning Basics UES Data’ Data 7 Data = Es WF pata SQL TUNING Ec eau cao Performance Tuning Basics Partitioned ees) SQL TUNING Ec eau cao Performance Tuning Basics ALTER TABLE employees MODIFY PARTITION BY RANGE (hire date) (PARTITION P_NANEL VALUES LESS THAN (TO_DATE('@1/01/2018", 'DD/MY/VYYY')) TABLESPACE USERS, PARTITION P_NANE2 VALUES LESS THAN (TO_DATE(‘C1/01/2019', ‘DO/MM/YYYY")) TABLESPACE USERS, PARTITION PLNGHES VALUES LESS THAN (TO_DATE(' 1/01/2020", 'DO/MA/YYYY")) TABLESPACE USERS, PARTITZON P_NANES VALUES LESS THAN (NAKVALUE) TABLESPACE” USERS, ) ONLINE UPDATE INDEXES (C1Oxa_SALARY oct, Tox2_ENP_ID GLOBAL PARTITION BY RANGE (employee_sd) (PARTITION TP1 VALUES LESS THAN (MAXVALUE)) % 2 TF SQL TUNING Ec eau cao Performance Tuning Basics i, @ (Hard parse) / Yes(Soft parse) SQL TUNING Performance Tuning Basics (ERASMAS oad SELECT * FROM products WHERE prod_category = ‘Electronics’; ‘Check Schema Information ¥ Find Possible Access Pathe CheckStatistics Recultic i ofthe whole table, [[pesuiszanortnewnotetnie ‘ T T / f Useindex Read whole Table SQLTUNING os 7 oN oe ea eee Performance Tuning Basics Ce oi very Transformer J Transformed Query peel pores Statistics ae, ‘ i” oe a Plan 4 Generator . as y (to Row Source Generation) eg SQL TUNING $B wv oracie-mastercom YA oraclemaster@outlookcom Performance Tuning Basics PIES a Sad Query transformer transforms the query into a better performing one but semantically equivalent of it If the transform isn't better. it uses the original one Time restriction and old statistics may lead a wrong plan creation ORExpansion +_Using OR in the where clauce will preventindex ueages SELECT * FROM sales WHERE prod_id = 14 or prono_id = 335 SELECT * FROM sales WHERE prod_id = 14 UNION ALL SELECT * FROM sales MERE prono_id = 33 and prod_id <> 14; ‘Subquery Unnesting +_the optimizer transformsa nested queryintoajoin statement, SELECT * FROM sales WHERE cust_id IN (SELECT cust_id FROM customers); SELECT sales.* FROM sales , customers WHERE sales.cust_id = customers .cust_ids SQLTUNING = Poe ee) pe) 4 4 peer ete Performance Tuning Basics SELECT * FROM sales WHERE promo_id = 999; SELECT * FROM sales HNERE prono_id = 9995 + Sales Table nas 9RBBABrOvs + promo _id column has 4 distinct values. +The Resulthos 267.857 rows SELECT * FROM sales WERE promo_id SELECT num_distinet FROM dba_tab_coluans WHERE table_name = ‘SALES? +The Resulthae 2074 rows selectivity = Number of rows returning from the query SELECT * FROM sales WHERE cust_id = 1000035 Total numberof rows * 7059 distinct values, 7059 é + Why Selectivity and Cardinality important? SELECT * FROM sales WHERE cust_id Cardinality> Total numberof rows x Selectivity = Selectivity : =) secs eratci oe io pron. id= 968 0 cham 1 Stocco , + cectnatylennds etrinjansn and cone ee een ae . + nconccrelettyand carey newectlancosestnation aca cn ae ie SITs EO ean ta Pes oe ‘ MASTERCLASS Performance Tuning Basics & Cost is the optimizer’s best estimate of the number of /Os to execute a statement. & To estimate the cost, the estimator uses; & Disk Yo, @ CPU usage, @ Memory usage. [Segoe [mae oc Single-block read time CPU Cost unter oe Cost = yey SQL TUNING $B wv oracie-mastercom YA oraclemaster@outlookcom Performance Tuning Basics SELECT e.first_name, e.last_name, e.department_name from FROM employees e, departments d WHERE e.department_id = d.department_id; Join erder[2]+ DEPARTHENTS[O]R6 ENPLOEESTE]#2 Bestis Joanhethods Hash Join order[2]+ ERPLOYEES[E}#1 DEPARTHENTS[O]¥ int doin Wa Join @ | setecr stares | ! 1 | HASH 2026 | 106 | . 2 | TABLE access Fut | oepantmets| 27 | 3 | tame access eu | ewvoveer | 307 | f SQL TUNING Eo ad Performance Tuning Basics = Once the plan generator generates the optimum plan, it handles that to the row source generator = Row source generator generates an iterative execution plan usable for the database = Rowsourceis an area that we get the row set (Table, view, result of join or groups) = Row source generator produces a row source tree (A collection of row source: = Rowsource tree shows the following information : Bienes ; Meera ares ose sean mera] SQL TUNING $B wv oracie-mastercom YA oraclemaster@outlookcom Performance Tuning Basics fo} aU NIN fes coe ee Pa eRenrne Sey es par Performance Tuning Basics ETN eee Se % SQL Tuning Principles % SQL Tuning Strategies ¥ Identifying the issue v Parse time reduction Y Clarify the details of that issue. y¥ Plan comparison strategy ¥ Collecting data ¥ Quick solution strategy v Analyze the data ¥ Finding & implementing a good plan ¥ Choose an appropriate tuning ¥ Query analysis strategy > } strategy ‘ / f 4 SQL TUNING coy Booms We peamcasen Performance Tuning Basics @ Weuse Query Analysis Strategy when: ¥ Quick tuning strategies did not work, and we have time to work on this problem ¥ Query can be modified ¥ Determine the underlying cause &@ Whatto doon this Strategy? y Statistics and Parameters ¥ Query Structure f 4 v Access Paths ¥ Join Orders & Join Methods ¥ Others . . / f 4 SQL TUNING = Coe eo ~~ Bee eaeeer neoy Pa eRenrne Sey es pe) Hast Loe Las Performance Tuning Basics ‘Analyzing the Execution Plan = Collecting Data a v Execution Plan {SQL trace, TKPROF, V$_SQL_PLAN, DEMS MONITOR, AWRSQRPT-SQL, ete} ¥ Information of objects in the query HE stow to reste Secu ios v Statistics ¥ check the access pathe Check the join orcer and the join type Compare actual & estimated number of rows, Find the steps where cost and logical reads are different ¥ The available tools are ‘significant (SQLT, DBMS.STATS, TKPROF, AWR Report, ete) * Analyzing by considering the query tuning techniques * Find a possible solution + Updating statistics ¥ Histograms v. Parameter Settings + Pre-Analyze of the Query ¥ Check the volume of resulting data + Using aynamie atatstles ¥ Check the predicates + coating oF re-creating an indox ¥ Check the problematic constructs peace + using Hints + others SQL TUNING Se CS Ree ee ~ Bertone erst Sa 5 a @outh Execution Plan & Statistics SQL TUNING ae Execution Plan & Statistics * Statistic Types + Optimizer Statistics + System Statistics + Can be gathered manually or automatically ANALYZE TABLE CONPUTE STATISTICS; + GATHER_DATABASE_STATS Procedure + Optimizer Statistics = System Statistics EXEC dbms_stats. gather_database_stats; + Used by the Optimizer to estimate V0 and CPU 7 GATHER DICTIONARY. STATS Procedure costs, EXEC dbms_stats. gather_dictionary_stets; + Should be generated regularly + GATHER SCHEMA STATS Procedure + Should be gathered during a normal workload EXEC dbms_stats. gather_schena_stats(ownnane=>'SH’ EXEC dbns_stats. gather_systen_stats(‘Start”); Piacente eee EXEC db ,_stats. gather_table_stats(ownname=>‘SH?, ‘tabnane=> ‘SALES’, cascade=>true); ‘+ GATHER INDEX STATS Procedure SELECT * FRO" sys SQL TUNING Ec ea cae Execution Plan & Statistics @ Howcan wesee the Optimizer Statistics? * DBA_TABLES * DBA_TAB_STATISTICS DBA_TAB_COL_STATISTICS DBA_INDEXES DBA_CLUSTERS DBA_TAB_PARTITIONS f f DBA_IND_PARTITIONS DBA_PART_COL_STATISTICS - » 7, ag SQL TUNING Ec eee cao Execution Plan & Statistics = To analyze an execution plan: 1" Autotrace + V$SQL_PLAN = EXPLAIN PLAN EXPLAIN PLAN FOR 3 + Generates the explain plan and saves into plan_table SELECT * FROM TABLE(DBNS_XPLAN.DISPLAY())3 EXPLAIN PLAN SET statenent_id = “MyID? FOR SELECT FROM EMPLOYEES where employee id = 1005 EXPLAIN PLAN SET st nt_Ad ‘WyID? INTO MyPlanTable FOR SELECT FROM EMPLOYEES where employee_id = 1003 SQL TUNING Ec ea cae Execution Plan & Statistics + Autotrace traces our query and produces the execution plan and the statistics SET AUTOTRACE ON; SET AUTOTRACE ON [EXPLAIN|STATISTICS]; SET AUTOTRACE TRACE[ONLY] ON [EXPLAIN|STATISTICS] SET AUTOTRACE OFF3 * Autotrace uses plan_table like the explain plan. SQL TUNING Ec ea cae Execution Plan & Statistics * There are a lot of performance views that can be used for tuning VSSQLAREA V$SQLLWORKAREA vssqL. V$SQLPLAN V$SQL_PLAN_STATISTICS V$SQL_PLAN_ STATISTICS ALL * V$SQL_PLAN Actual execution plans are stored here It is very similarto plan_table tis connected to V$SQL view SELECT * FROM TABLE(DBMS_XPLAN. DISPLAY_CURSOR( “Sdaxtahvagh94? ))3 SQL TUNING Ferenc peer ren Execution Plan & Statistics SELECT p.prod_id,p.prod_name, s.amount_sold, s.quantity_sold FROM sales s, products p, custoners € VERE s.pred_id = p.prod_id AND s.cust_id = c.cust_id AND sucust_id 2N (2,3,4)5)s Aan Nod * Cost fan 1 1 gat anes Sesto tse ' | Bl ie s Access Methods — [I+ 21 “mar sor I | at | daar S| Mx atcess woz | mocers || “ten Brel | S| "tner tracer ' —— JonMothede@2cinTypee|! | “SEE SEH av soe arnt soma masa) sues | sat |? a Ts WoT ‘BUIVAP CONVERSION TO ROWZDS: 1 1 1 = Partin Pruning |/2 3! gett mest mae | uss ase | | * Others ust 10%4 oR *3%.*C08T 10%-5) ox *o%.so0er a Et a0 SQL TUNING eas | ee Table & Index Access Paths > Types of Indexes v B-TREE Indexes INDEX (TABLE £00127 Tyler Bennett £10297 + Normal Index Eo1234 John Rappl 21437 £03033 George Woltman 00127 De ae Eos242 Adam Smith £69535 + Index-Organized Table (107) E1001 David McClellan E04242 |E10297 Rich Holcomb 01234 ¥ BITMAP Indexes E 163938 Nathan Adams Es1298° , 21437 Richard Potter’ = £43128 > Attributes of Indexes E27002 David Motsinger_ Ez7ooe E41208 Tim Sampair £03033 * Key Compression E4size Kim Atlich £10001 z 1 0 Te ten £63535 Timothy Grove _E 16398 * Ascending ~ Descending Ordered Indexes &/, SQLTUNING $B wv oracie-mastercom ZB [email protected] fwripT rire h Table & Index Access Paths Table & Index Acce: Dene Sales Table 10@01000e00000e01010008000010 @1e0011111100¢100101111111101) e@e119e00e00e111e19e0 02000 C e000) 2@0e00000020000 002000000000 Cee Bees Table & Index Access Paths > Table Access Paths > Index Access Paths ¥ Table Access Full v v Table Access by ROWID v v_ Sample Table Scan v ot UT emis Set BU he rer Index Unique Scan Index Range Scan Index Full Scan Index Fast Full Scan Index skip Scan Index Join Scan oy Index Organized Tables. Bitmap Access Paths ou Hoog = — Poe ee) Cee u Seo acy Coe fea fora Table & Index Access Paths Ee ae) » Table Access by ROWID occurs when : ¥ ROWID is used in the where clause directly v Byan Index Scan operation E SQL TUNING $B wv oracie-mastercom ZB [email protected] Table & Index Access Paths Doane} Table & Index Access Paths INDEX RANGE SCAN >the data we queried Is Bounded from one or both sides, the optimizer can use a > canbe applet ieee and mapinsre cy Cant epee ere tthe optimizer finds one or mere leading columns with= > «sings, will use indo rangescan. ene query includes an order by of group by clauses withthe Indexing colurnn, ange scan wll not do any sort. already sorted. Itshould net have null values order by clause hac desckaywor Invi read the data in descending order. > Youcan create your index as descensing, CREATE 108% andex_nane cit employees (departnent_id D850) a > Fumetion-based indexes canbe secessed 3s Index range scan > Mtwildeard characters are written on the ght, t wll perform indoxrange scan (OHERE doportnant_nane LIKE “A Pe ele) oe eee ee Pye ree See Table & Index Access Paths Esa > All the rows of the tables are indexed by their indexes » When the optimizer uses the index full scan? > Query has order by clause only with the indexed columns. > Query has group by clause only with the indexed columns. | > Query requires a sort-merge join. a a HHoHoOoOGoe eS é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] Pye ree See Table & Index Access Paths > If the query requests only the columns existing in the index, it uses IFF Scan > Can be applied to both b-tree and bitmap indexes. » Hints can be used to force the optimizer to use IFF Scan. > The differences of Index Full Scan vs Index Fast Full Scan > Index Fast Full Scan always reads only from the index X Index Full Scan may read from table, too. > Index Full Scan reads blocks one by one, sequentially X Index Fast Full scan reads multiple simultaneously, in unordered manner. > Index Fast Full Scanis faster than Index Full Scan most of the times. > Index Full Scan can be used to eliminate sorting, but Index Fast Full Scan cannot. Pe RL le oe eee ee MASTERCLASS® Table & Index Access Paths See > Ifyou don’t use the indexed columns on the where clause, the optimizer will not use the indexes. y We don’t create indexes for all the rows because of the costs. V If the second, third, column of a compositeindex is used as an access predicate, the optimizer will consider the index skip scan. y Index skip scan skipsthe leaves which do not have any chance to have any matching rows. » What are the advantages? ¥- Helps to reduce the number of indexes ¥ Decreasestheindex space ¥ Increases the overall performance by reducing index maintenance é&/, SQLTUNING Table & Index Access Paths INDEX SKIP SCAN SELECT + FROn EAPLOVEES WHERE AoE BeTHEEN 20 ano 30] BE Table & Index Access Paths > Ifan index stores the columns of a query, the optimizer will perform index fast full scan. > If the combination of multiple indexes store the columns of a query, the optimizer will join them and read the data from that join. (INDEX JOIN SCAN) » What to know about Index Join Scan? ¥ The combination of indexes must have every colurnn of the select clause, ¥ There is no join limit. More than two indexes can be jolned together to get the data. ¥ ‘There might be any index access path before the index join scan ¥ If you write ROWIDin the select clause, it will NOT perform index join scan. Pe RL le oe eee ee MASTERCLASS® Using Optimizer Hints > To command the optimizer, we use optimizer hints. Optimizer hints force the optimizer to pick a specific action. ‘The optimizer may not follow your hints. Ifthe hint is not reasonable, the optimizer will ignore it. Hints can be operating on a single table, multi-tables, a query block, a specific statement. Categories of the hints: + Hint for optimization approaches Y Access Pathe Hints Query Transformation Hints ¥ oin Order Hints ¥ Join Operations Hints Praia Exeettions Hint ¥ others QLTU Terr yee Using Optimizer Hints SELECT /*+ hint_name(p1 p2 p3..) */ first_name FROM EMPLOYEES; > Hints can be used after a SELECT, UPDATE or DELETE keywords. > You can use the table name or its alias as the hint parameter. But if there is an alias, you cannot use the table name! > There can be only one hint area. > Be careful on the hints you selected, especially if you are using multiple hints. You may lead the optimizer to a bad execution plan. «| SQLTUNING $B wv oracie-mastercom ZB [email protected] eer ee Seer Join Operations é&/, SQLTUNING eer ee Seer P= Foe —_ “a Land Nosted loop join ie efficient when joining row sources are small. Sort merge jin is better than nested loop join if table is big and (or) one side is sorted. Hash join Ie better than sort merge jon for most cases if both sides are not sorted already. Cartesian products the most costly on: ItJoins all the rows of one side with all the rows of the other side. [ype Join Operations Fd | operation || DEFARTMENT: 1 AN | EMP DEPARTMENT 1X | BY INDEX ROWID| EMPLOY 4 > Ajoin operations done by the driving table (external table, outer table) and the inner table. Tableis the general name, but it is actually a row source If two row sources are small, or bigger one has an index, the optimizer may perform a nested loop Nested loop returns may be efficient if you need some rows immediately You can use USE_NL(table! table2) hint to force the optimizer to use nested loops é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] MASTERCLASS® Join Operations DESI Pins es Prins reg TABLE io ioe bat 5 1 5 1 a 5 4 4 5 2 3 2 3 7 3 7 3 2 z a 2 3 4 3 4 PARC o es ey ee MASTERCLASS® Join Operations > If the sort exceeds the sort area in PGA, it will write the sorted data into the disc. But this increases the cost so much. Index Full Scan or Index Range Scan will be very useful in sortsmerge join, since the data is already sorted in indexes. > Sort is done on joining keys. > After the sorts, both row sourees are merged. > Sort Merge is better than Nested Loop Join if table if row sources’ sizes are large > In sort merge, there is no driving table or inner table, > Ifthe row source is already sorted, there will be no sort key in the execution plan. > Sort merge join is efficient when the join condition is not an equijin > use_merge{tablel table2) hint is used to force the optimizer to perform sort merge join Pe RL le oe eee ee eer ee Seer Join Operations Eon Ene INDEX Poe) peta) Petras) Petes) | of 1 5 1 2 : : aaa! 5 I P= ~~ Foe Cees Pod Rated Yo fone Join Operations > Ahash table is built for the smallest row source » Hash table is created by generating the hash values of the key columns > The keys of second row source are hashed and checked against the hash table » Full table scan is performed to the table that will be hashed > Hash join is performed only when an equijoin is used >use_hash(table1 table2) hint is used to force the optimizer to perform hash join ease) a) G MASTERCLASS® a oe eee ee Join Operations rowidl — (2) ee rowid3 (4) etaay rowidl 2 rowid4 1 rowid2 3 2 rowid3 4 3 rowida 4 3 rowids 5 ra rowidé 7 5 9 ease) a) Join Operations > Joins all the rows of a table with all the rows of the other table > It is nota realistic join for business » It is mostly done by mistake by forgetting to write the join condition > It results a very high cost. fl a oe eee ee Join Operations > There are 4 join types : ¥ Equijoins and Nonequijoins v Outer Joins ¥ Semijoins v Antijoins > Equijoins return the matching rows with the equality operator > Joining other than equality operator is called as nonequijoins » Outer joins return matching and nonmatching rows > Semijoins return the rows matching with the EXISTS subquery > Antijoins return the rows which does not match with the NOTIN subquery «| SQLTUNING Ru Ben eer ee Seer Join Operations > If the join condition contains an equality operator, it is an equijoin. > Equijoins are the most commonly used join types > To improve performance, you should use equijoins whenever you can > If you use another operator than the equality operator, it is nonequijoin. > Equijoins can use all join methods mF, > Noneq' ns cannot use the hash join method «| SQLTUNING $B wv oracie-mastercom ZB [email protected] eer ee Seer Join Operations CEN » Outer joins return the matched and unmatched, TT Bla rows of the sources. 5 1 > Outer joins can be used with all join methods rR ie i 5 >» With Nested Loop and Outer Join, inner table is v S the one whose nonmatching rows will return. 7 3 2 2 » With Hash Join and Outer Join, hash table is bullt for the one whose nenmatching rows will 3 4 NOT return, Pe RL le oe eee ee MASTERCLASS® Join Operations » Semijoin returns the first match > Semijoin is the way of transforming the EXISTS Error) subquery into a join 5 = > Sometimes the optimizer may select a different a aaa! 5 method than semijoin even if you used the 2 3 EXISTS subquery 7 3 » Semijoins can be used with all join methods 5 5 » Use EXISTS instead of IN if possible 3 4 Pe RL le oe eee ee MASTERCLASS® Join Operations EN > Antijoins return the rows that do not match with the NOTIN subquery. » By default antijoins are used with sort-merge joins. » The optimizer may select a different one or you can use HASH_AJ or NL_AJ hints to change the join method. é&/, SQLTUNING MASTERCLASS® 5 a P= Foe —_ “a Petitat) 1 [ype Other Optimizer Operators > Result Caches a memory areain SGA to store the results of queries for some time to increase the performance. There are two ways to store results in result cache: > MANUAL (DEFAULT- Needs resuit_cache int) > FORCE (n0_resuit_cachehin is used not tostorein the result cache) > DBMS_RESULT_CACHE package has statistics, information and some memory managing abilities V$RESULT_CACHE_OBJECTS view has the result cache data. Table annotations can be used as the default storage option to the result cache. CREATE TABLE table_name (..) RESULT_CACHE (MODE DEFAULT| FORCE) ; p ALTER TABLE table_name (..) RESULT_CACHE (MODE DEFAULT|FORCE)5 é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] MASTERCLASS® Other Optimizer Operators Every VIEW operator in the operations area does not mean that a view is therein the query Y Each seperate query ina query is pretended as an inline view. Y y What is View Merging? y View Merging means joining the inner query and the outer for a better performance v Eventually, every query insidethe outer query is shown with VIEW operator unless they cannot be merged with the outer query. mF, Pe RL le oe eee ee MASTERCLASS® Other Optimizer Operators Gag » The main goal of clustering is improving the performancewith a different way of storage ‘ i Sales Table » Types of clusters: Fa ae Cluster Table INDEX cLusTeRs aaa eta heer cere Prmeec era Peele Frees ent SINGLE TABLE HASH CLUSTERS: 101136 brea ’ SORTED HASH CLUSTERS Products Table id category corey one can ae Monitors FEC PA Site a Spucsmsaes [Se peusicooeiens Pearce de Sw Lo Other Optimizer Operators Em > Sort Operator Types: > SORT AGGREGATE Operator > SORT UNIQUE Operator IF YOU WANT THE DATA RETURNED IN ORDER, YOU ieccamianaiees ca NEED TO USE THE ORDER BY CLAUSE. THESE ORDER > SORT GROUP BY Operator TYPES DOES NOT GUARANTEE THAT THE ROWS WILL > SORT ORDER BY Operator RETURN IN ORDER! > HASH GROUP BY Operator > HASH UNIQUE Operator aT. > BUFFER SORT Operator é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] yee rr ey Serr Other Optimizer Operators > When we use IN clause, if the values in IN clause are not too many, the optimizer tends to use the INLIST Operator > For INLIST Operator usage, the search must be on the indexed columns >» How does INLIST Operator work? SELECT * FROM EMPLOYEES WHERE employee_id IN (100, 110, 146) 100 110 146 a a a aie SQLTU 0 H NG $B wv oracie-mastercom ZB [email protected] rer SQL Tuning Techniques Query analysis strategy mainly focuses on modifying our queries to a better performing one. > The steps of solving the performance problem: v Check the execution plan basically for the problematic areas. ¥ Check the statistics ¥ Check your query for the common mistakes ” Check the execution plans detailed Check the Access paths ¥ Check join orders and join types v Compare the actual & estimated number of rows mF, Check the operations where the cost and logical reads differ significantly SQLTU yee a oe eee ee SQL Tuning Techniques » Common reasons for a Bad SQL: > Common possible solutions: Make the statistics up to date ¥ Create function-based indexes % Poorly written query Use dynamic statistics ¥ Use index-organized tables X Index used or not used Create or modify indexes ei haat coambnrnece X There is no index + Rewrite the query to use an index » Use parallelexecution X Predicates are not used Use hints / Use materialized views & Wrong types in predicates y Remove wrong hints ¥ Modify or disable triggers and Change the hints X wrong Join order saa Eliminate implicit data type conversion ® Other v Other SQLTU yee a oe eee ee SQL Tuning Techniques > There are many different tools to get the execution plans and the statistics. > Explain plan or Execution Plan? > There are 4 major tools to get the execution plan and the statistics : wv Autotrace ¥ SQL Monitor v-TKPROF ¥ DBMS_XPLAN » AUTOTRACE : > Itis free. » SQL Developer and SQL Plus shows different statistics. » Collapse or expand and focus only the problematic area in SQL Developer Bona Rpeorsgeen SQL Tuning Techniques » AUTOTRACE (Continues) : » Wecan compare two different execution plans and statistics with SQL Developer. » SQL Developer has a hotspot button which shows the problematic area of the plan. » We can exporta plan and use it later. > By default, SQL Developer autotrace doesn't read the all of the rows. (Check Too/s->Preferences->Database->Autotrace/expiain Plan in SQL Developer) » SQL MONITORING: » It shows the execution plan and the statistics of the query being used right now » It can be viewed by SQL Developer or Enterprise Manager or by an SQL code > It captures the queries running longer than 5 seconds or running in parallel mode > If our query doesn’t suit to these, we can add MONITOR hint to make it show on the list. > Needs Diagnostics and Tuning packs to be licensed. SQL TUNING Poe yee [ype SQL Tuning Techniques » TKPRO! Converts Oracle trace files into a human readable format. V > We need to create a trace file first, and use TKPROF tool for this trace file. Using TKPROF needs a bit more work to do comparing to the others. > You need to have the access privilegeto the database server to use this. It includes all the SQL statements run between the tracing starts and ends. It breaks down the execution time into parse, execute and fetch times. > DBMS_XPLAN : > It shows performancestatistics for each step of the plan V V v > If the query has already run before, there is no need to run fot DBMS_XPLAN. » If it is deleted from the cache, the AWR tool can show earlier plans. SQLTU yee a oe eee ee > What does the Real-Time SQL Monitoring Tool do for us? » Wecan check the real-time execution plans and statistics with graphical views. » Captures the execution plans and statistics automatically » Very useful to analyze the complex queries and PL/SQL code executions. » This tool can be used via SQL Developer, Enterprise Manager, or by Code » We can view SQL Monitoring information from v$sql_monitor and v$sql_plan_monitor database views. > Captures the queries running longer than 5 seconds or running in parallel mode. > To see the queries running less than 5 seconds, we can use the MONITOR hint. > Needs to have Diagnostics and Tuning Packs to be licensed. Pe RL le oe eee ee yee rr ey Serr SQL Tuning Techniques >» What are the benefits of this tool? » Shows the execution in real-time with lots of details » Tracks all the running queries, which enables us to find the top consuming queries » We can create active reports of the running queries which lets us analyze offline » Monitors the parallel execution > Helpsto analyze the large plans and complex queries easier » We can utilize poorly used indexes mF, » We can determinethe bind variables of the queries Pe RL le oe eee ee MASTERCLASS® SQL Tuning Techniques >» What are the benefits of SQL Trace files? » Performance information of our queries. » CPUTime and Elapsed Time » Wait Events: » Execution Plans leew cours » Call Counts (Parse, Execute, Fetch) mF, » Physical and Logical Reads Bene Rpeosgeen SQL Tuning Techniques SELECT value FROM V$DIAG_INFO WHERE name = ‘Diag Trace’; >» How to enable/disable tracing? » We can enable/disabletracing for a specific user | specific session | entire database. > dbms_monitor.database_trace_enable(); » dbms_session. set_sql_trace(); » alter session set sql_trace = true; » We can trace user, session, database, application, service, module, etc. mF, SQLTUNIN yee [ype SQL Tuning Techniques USING SQL TRACE FILES & TKPROF TOOL » How to track our own session? ALTER SESSION SET SQL_TRACE = TRUE|FALSE; DBMS_SESSION.SESSION_TRACE_ENABLE(waits => TRUE, binds =>FALSE); DBMS_MONITOR. SESSION_TRACE_ENABLE(session_id=>27, serial_num=>60, waits=>TRUE, binds=>FALSE) DBMS_SESSION. SESSION_TRACE_DISABLE(); DBMS_MONITOR. SESSION_TRACE_DISABLE(session_id=>27,serial_num=>60) Kk. » tresess utility can merge multipletrace files. «| SQLTUNING SQL Tuning Techniques > How to generate TKPROF output? » TKPROF takes trace files as input, and generates a formatted output file. >» Doesn't show the commit and rollback operations. TKPROF trace_file name output_file_name [waits=yes|no] [sort=option] [printsn] [aggregate=yes|no] [insert=insert_file_name] [sys=yes|no] [table=schema.table] [explain=user/password] | [record=record_file_name] [width=n]; SQLTUNIN = — Poe ee) Cee u Seo acy oreo fea fora SQL Tuning Techniques > Do not use select * for all the queries » The optimizer may select a worse plan if you query for unnecessary columns > Whilej ing multiple tables or querying from views, selecting less columns might affect the performance > Ifyou use select *, the databaseneeds to check the data dictionary to get the table structure » select * will make the database perform more I/O operations » select * may decrease the performancesignificantly if the table has LOBs » select * will have a higher overload on the network. So there might be more network waits » select * is tend to have problems on maintenance el ele) oe eee ee MASTERCLASS® SQL Tuning Techniques > How do | make the optimizer use my indexes? > If the selectivity of the predicate is high, using Indexes may increase the performance very much > Add adequate predicates to the queries (Use indexed columns in your queries clearly) > Use reasonable hints in your queries > If possible, select only the indexed columns mF, ease) a) SQL Tuning Techniques > How do | make the optimizer use my indexes? » To make the optimizer use our indexes, the indexed columns must be used clearly. SELECT first_name, last_name, department_name FROM employees WHERE first_name||last_name = ‘StevenKING? SELECT first_name, last_name, department_name FROM employees WHERE first_name = ‘Steven’ AND last_name = ‘KING’; mF, é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] MASTERCLASS® SQL Tuning Techniques » How do | make the optimizer use my indexes? » One of the common mistakes for tuning aspect is using the arithmetic operations on the indexed columns. SQL TUNING yee rr ey Serr SELECT prod_id, cust_id, time_id FROM sales WHERE time_id + 10 = ‘20-2AN-98?5 SELECT prod_id, cust_id, time_id FROM sales WHERE time_id = ‘10-JAN-987 ; SELECT prod_id, cust_id, time_id From sales WHERE time_id = to_date(‘20-JAN-98, ‘DD-MON-RR?)-105 P= ~~ Foe Cees Pod Rated Yo fone SQL Tuning Techniques » How do | make the optimizer use my indexes? SELECT employee_id, first_name, last_name, salary FROM employees WHERE last_name LIKE ‘%on é&/, SQLTUNING MASTERCLASS® SELECT employee_id, first_name, last_name, salary FROM employees WHERE last_name LIKE ‘Ba%?; SELECT employee_id, first_name, last_name, reverse(last_name) FROM employees WHERE reverse(last_name) LIKE ‘rahh%? ; P= ~~ Foe Cees Pod Rated Yo fone SQL Tuning Techniques > How do | make the optimizer use my indexes? » Using functions on the indexed columns may suppress index usage SELECT employee_id, first_name, last_name, salary FROM employees WHERE TRUNC(hire_date, ‘YEAR?) = ‘@1-3AN-2002? 5 SELECT employee_id, first_name, last_name, salary FRON employees WHERE hire date BETWEEN ‘@1-JAN-2@02? AND ‘@1-JAN-2002"; mF, é&/, SQLTUNING $B wv oracie-mastercom ZB [email protected] MASTERCLASS® SQL Tuning Techniques » How do | make the optimizer use my indexes? » B-Tree indexes do not index the null values and this sometimes may suppress the index usages in our queries. » Ways to handle NULL value based performanceloss: > Ifreasonable, create a BITMAP Index instead of a B-TREE Index (BITMAP Indexes store the NULL values) mF, SQLTU yee a oe eee ee SQL Tuning Techniques Io Te oo Sacre ins andi @ELEGRS Ta), — = { sevecr * FROM T, (SELECT X FROM T2) T2 WHERE T2.X = T2.X)5 } (sevect * FRom Ta WHERE EXISTS (SELECT X FRON T2 WHERE TA.X = 12.x); | = — FOR X IN (SELECT * FROM T1) LOOP IF (EXISTS (SELECT X FROM T2)) THEN OUTPUT THE RECORD END IF5 ENDS SQL TUNING $B wv oracie-mastercom ZB [email protected]

You might also like