You are on page 1of 11
Holowczak.com Tutorials Posted on September 19,2013 by holowerak Database Query Processing and Optimization Query Optimization We divide the query optimization into two types: Heuristic (sometimes called Rule based) and Systematic (Cost based) Heuristic Query Optimization * Oracle calls this Rule Based optimization. + query can be represented as a tree data structure. Operations are at the interior nodes and data items (tables, columns) are at the leaves, + The query is evaluated ina depth-first pattern Men Search Holowczak.com Q search Donations Welcome Ifyou have found anything ‘educational or entertaining on holowczak-com please consider submitting Donation using PayPal ‘Thanks Recent Posts Getting started with ySparkon Google Cloud Platform Data ring Analytical SQL. with Google BigQuery ‘Consider this query from the Elmasri/Navathe textbook Connecting to Oracle ‘loud from Tableau. Desktopusing Oracle Instant Client SELECT PNUMBER, DNUM, NAME FROM PROJECT PARTMENT, EMPLO} 1EREDNUMCDNUMBER and MGREMPID-EMPID and OTA) Sed 0 inrelational algebra: pec % pmumber, deur, name (GF ploeaton=“Saffor (F mgssmess ( F éoum-dnnber (Px (Dx E)}))) ‘on the following schema: EMPLOYEE 1 FNAME JOHN -B SMITH 123456789 09-JAN-55 731 FONDREN, HOUSTON, TX M FRANKLIN 7 WONG 333445595 08. YOSS, HOUSTON TX 4 ALICTA ZELAYA 99908 9-JUL-58 3321 CASTLE, SPRING, TK F JENNIFER S WALLACE 987654321 20-JUN-31 291 BERRY, BELLAIRE, TK = RA NARAYAN 66688) 975 FIRE OAK, HUMBLE, TX M JONCE A -ENGLIGH 453653453 31-auI~62 RICE, HOUSTON, TRF 4 y JABBAR 94 87987987 29-MAR-S9 980 DALLAS, HOUSTON, TX M E BORG 888665555 10-NOV-27 450 STONE, HOUSTON, TX DEPARTMENT TABLE Dwave DNUMBER MGREMPLD MGRSTART Twitter: rholowezak HEADQUARTERS ADMINISTRATION RESEARCH PROJECT TABLE, NAME, PNUMBER Product, 1 Product 2 Product 3 Conputerizatn 10 Reorganization 20 Newsenefizs 30 888665555 19-cUN-71 987654321 O1-JAN-85 33344) 5 22-MAY-78 PLOCATION NUM Bellaire Sugarland Houston Stafford Houston statford 5 5 4 1 4 + Which ofthe following query trees is more efficient ? 7 PNUMBER, ONUM, LNAWE PpLOCATIONSStatfort and MGRESN = SSN and Ont = ONE emikcartonkids nest. co/oaBvveNzch 0/08/21 02:07.Ginceplyto lkcartonkids ‘esting PayPal Donations ‘tps. co/9¢fp7oDS1m lnk tps. co/FBSPUSVB aunzi20.02:56:02 ust helped ws. Bonheur classroom and you can too. hutpsl.cofwmeTisiQg0 onjazp20 09:11:36, ‘This tree is evaluated in steps as follows: TEMP_DE <- DxE TEMP_PDE <- Px TEMP_DE TEMP_S1 <_ G dnum=dnumber TEMP_PDE ‘Adv. Cloud TEMP_S2 <— G mgrssn=sn_ TEMP_S1 Computing & : — DevOps TEMP_S3_ <— _ G plocation =‘Staffort” TEMP_S2 pnumber, dnum, Iname TEMP_S3 7 PNUMBER, ONUM, LNANE Payoresn= ssn 19 oyund cna LD DNUM= ONUMBER ‘SPLOCATONSStafo ‘This tree is evaluated in steps as follows: Tags TEMP_P1 <— _ G plocation = ‘Stafford’ P. TEMP_PD < TEMP _P1 X|dium-dmumber D TEMP_PDE <— TEMP_PD [X|mgssn-ssn E 7 pnumber, dnurn, Iname TEMP _PDE ‘+ Note the two cross product operations. These require lots of space and time (nested loops) 0 buila + After the two cross products, we have a temporary table with 144 records (6 projects *3 departments * 8 employees) ‘+ An overall rule for heuristic query optimization is to perform as many select and project operations as possible before doing any joins. ‘+ There are a number of transformation rules that can be used to transform a query: 4 Cascading selections. lst of conjunctive conditions can be broken up into separate individual conditions. 2. Commu ity ofthe selection operation. 3. Cascading projections. all ut the last projection can be ignored. 4, Commuting selection and projection. Ifa selection condition only involves attributes, contained in a projection clause, the two can be commuted, ty of Join and Cross Product. 6. Commuting selection with Join 1. Commuting projection with Join 8. Commutativty of set operations. Union and Intersection are commutative 8. Associativty of Union, Intersection, Join and Cross Product. 10. Commuting selection with set operations. 5. Commutati ‘Access Access 2013 ActiveX API Beginner Beginners Bloomberg Ci C++ careers Cloud Database pataintzration Data Warehousing DBA, oebusging Education Employment emcanner Excel ancial IT Financiat ‘Technology Forms Getting Started coos Guide [Binteractive Brokers Javawierset neces OF ACLE ore torme Pentaho Programming saqMktData Socket SQL SQL Server Tadeeworistaton Tutorial TWS ws al Studio Windows Forms Winforms Post Categories 111, Commuting projection with set operations. 412. Logical transformation of selection conditions. For example, using DeMorgan’s law, et. 413, Combine Selection and Cartesian product to form Joins. ‘+ These transformations can be used in various combinations to optimize queries. Some general steps follow: 1 Using rule 1, break up conjunctive selection conditions and chain them together. 2. Using the commutativity rules, move the selection operations as far down the tree as possible, 3. Using the associativity rules, rearrange the leaf nodes so that the most restrictive selection Conditions are executed first. For example, an equality condition is likely more restrictive than an inequality condition (range query) 4. Combine cartesian product operations with associated selection conditions to form a single Join operation 5. Using the commutativity of Projection rules, move the projection operations down the tree toreduce the sizes of intermediate result sets 6 Finally identify subtrees that can be executed using a single efficient access method. Example of Heuristic Query Optimization 1. Original Query Tree Select Category ¥ ox Micici rape: ‘RE Calolate Premium FOR 1 CRORE TERM INSURANCE ace: (25 cenoen (Malev 0 You consume TonAcco? co oxi (Rs. = PNUMBER, DNUM, LNAME | DNUM=DNUMBER 4 MGRSSN = SSN / PLOCATION = 'Stafford" 2. Use Rule 1 to Break up Cascading Selections *PNUMBER, DNUM, LNAME o ie oc MGRSSN = SSN ¢ PLOCATION='Stafford’ ie 3. Commute Selection with Cross Product 7tPNUMBER, DNUM, LNAME 1 oc MGRSSN = SSN Same co DNUM = DNUMBER g PLOCATION= —CCEPARTVEND ‘Stafford’ PROJECT 4, Combine Cross Product and Selection to form Joins aPNUMBER, DNUM, LNAME 1 D1 MGRSSN = SSN Dd pNuM = DNUMBER o PLOCATION= Rogan Pages:1 23 45 6 7 8 9 10 category: Database Tags: Autotrace, Cost based query optimizatio Heuristics optimization, Hints, index, indexes, ‘Optimization, Oracle, Query hints, Query Optimization, Query Processing, Rule based query optimization, Selection cardinality, selectivity, SQL Developer, SQL Server, Tuning ‘= Installing the Oracle Warehouse Builder 11g R2 Introduction to Data Warehousing “> Client © Holowczak.com Tutorials 2022, Built with Storefront

You might also like