Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Optimization Cases. D. ChristozovINF 280 DB Systems Query Optimization: Cases 2 Executable Block 1 Algorithm using Indices (if available) Temporary.

Similar presentations


Presentation on theme: "Query Optimization Cases. D. ChristozovINF 280 DB Systems Query Optimization: Cases 2 Executable Block 1 Algorithm using Indices (if available) Temporary."— Presentation transcript:

1 Query Optimization Cases

2 D. ChristozovINF 280 DB Systems Query Optimization: Cases 2 Executable Block 1 Algorithm using Indices (if available) Temporary data set Executable Block 2 Algorithm without using indices, Indices not available for temporary data sets Execution Pipe-line

3 D. ChristozovINF 280 DB Systems Query Optimization: Cases 3 Disk with block-size – 512 B; size of physical address – 8 B; access time – 10μs (time for one I/O operation. Tables: A with 1,000,000 records, and B with 500,000 record, different values for A.A3 are 50,000; and for B.B3 – 10,000. CREATE TABLE A ( A1 CHAR(10) NOT NULL UNIQUE, A2 CHAR(20) NOT NULL UNIQUE, A3 CHAR(10), A4 CHAR(40), PRIMARY KEY (A1)); CREATE TABLE B( B1 CHAR(10) NOT NULL UNIQUE, B2 CHAR(22) NOT NULL UNIQUE, B3 CHAR(30), B4 CHAR(30), PRIMARY KEY (B1)); CREATE INDEX A.A1; CREATE INDEX A.A2; CREATE INDEX A.A3; CREATE INDEX B.B1; CREATE INDEX B.B2; CREATE INDEX B.B3; SELECT * FROM A WHERE A3 IN ( SELECT B1 FROM B WHERE B4 = ‘ABCDEFGHIJ’); CASE1CASE1

4 D. ChristozovINF 280 DB Systems Query Optimization: Cases 4 1. Execution Plan SELECT * FROM A WHERE A3 IN ( SELECT B1 FROM B WHERE B4 = ‘ABCDEFGHIJ’); SELECT * FROM A WHERE A3 IN list SELECT B1 FROM B WHERE B4 = ‘ABCDEFGHIJ’); There is no index for B4 BRUTE FORCE SEARCH There is cluster index for A3 Search for every value in the list Natural Pipe-line

5 D. ChristozovINF 280 DB Systems Query Optimization: Cases 5 B+ tree for A.A3 Disk with block-size – 512 B; size of physical address – 8 B; access time – 10μs (time for one I/O operation. Tables: A with 1,000,000 records, and B with 500,000 record, different values for A.A3 are 50,000; and for B.B3 – 10,000. 1.In average every category of values A.A3 has 1,000,000 / 50,000 = 20 records. 2.In one block of the clusters we may store 512/8 = 64 physical addresses – 63 addresses of records = one link to the other blocks in the same cluster. OR (in average) we need 1 block for a cluster or 1 I/O operation to retrieve all addresses of relevant records. 3.The B+ tree will have 50,000 entries (one for each category). The size of A.A3 field is 10 bytes. The order of B+ tree: 8*M + 10*(M-1) < 512 or M=29. 50% of 29 is 15 – worst case. Live-level = 3334 blocks. 4.Levels: (1) 223; (2) 15; (3) 1 OR search in the index need 4 I/O operations. 5.Size: B+ tree: 3334+223+15+1 = 3573; clusters: 50,000*1 = 50,000 OR totally 53,573 blocks

6 D. ChristozovINF 280 DB Systems Query Optimization: Cases 6 Time (I/O and seconds) for execution SELECT B1 FROM B WHERE B4 = ‘ABCDEFGHIJ’); 1.500,000 records, 90 bytes each (22+8+30+30); 2.5 records in one block 3.100,000 blocks  100,000 I/O SELECT * FROM A WHERE A3 IN list Assumption: The result may have approximately 1000 values of B1. 1.For every value in the list we need in average 25 I/O operations (4 to search in index, 1 to retrieve the cluster and 20 to retrieve records from master file). 2.OR approximately 25,000 I/O. Totally: 125,000 I/O operations OR 1,250,000 µs = 1,250 s

7 D. ChristozovINF 280 DB Systems Query Optimization: Cases 7 CREATE TABLE A ( A1 CHAR(10) NOT NULL UNIQUE, A2 CHAR(20) NOT NULL UNIQUE, A3 CHAR(30), A4 CHAR(20), PRIMARY KEY (A1)); CREATE TABLE B( B1 CHAR(8) NOT NULL UNIQUE, B2 CHAR(22) NOT NULL UNIQUE, B3 CHAR(30), B4 CHAR(30), PRIMARY KEY (B1)); CREATE INDEX A.A1; CREATE INDEX A.A2; CREATE INDEX A.A3; CREATE INDEX B.B1; CREATE INDEX B.B2; CREATE INDEX B.B3; SELECT * FROM A INNER JOIN B ON A.A3 = B.B3 WHERE A1 = ‘ABCDEFGHIJ’; 1.Explain the query execution plan 2.Evaluate the order and height of the B+ trees for index-files, which will be used in query execution. 3.Evaluate the time (in I/O operation and in seconds) for execution of the query. 4.Rewrite the query to guarantee optimal execution Tables: A with 1,000,000 records, and B with 500,000 records. Disk: block-size – 512 B; size of physical address – 8 B; access time – 10μs. Where, different values for A.A3 are 50,000; and for B.B3 – 10,000. CASE2CASE2

8 D. ChristozovINF 280 DB Systems Query Optimization: Cases 8 1.Explain the query execution plan Step 1:select: using the primary index for A.A1 Step 2:join: single loop, using the cluster index created for B.B3 2.Evaluate the order and height of the B+ trees for index-files, which will be used in query execution. B.B3: (m-1)*30 + m*8 <= 512  m = 14; 50%m = 7 Leaf: 10,000 values/7 = 1428; next = 204; next = 29; next = 4 B+ tree will have 4 levels Cluster: average 50 records will have the same value; 50*8 (physical address) = 400 bytes or one block. A.A1: (m-1)*10 +m*8 <=512  m = 29; 50%m = 15 (it is ok if you use 14) Number of records in one block: 512/80 = 6; Number of blocks: 1,000,000/6 = 166,667 blocks in the Master file Leaf: 166,667/15 = 11,112; next =740; next = 49; next = 3; B+ tree will have 4 levels. 3.Evaluate the time (in I/O operation and in seconds) for execution of the query. Step 1: 4 + 1 = 5 I/O operation to select the record from A Step 2: to combine with B rows 4 (B+ tree) + 1 (cluster) + 50 (average number of matching rows in B) = 55 I/O (could be more if the cluster needs more than one block). Totally: 60 I/O operations

9 Do you trust the optimizer of your DBMS? D. ChristozovINF 280 DB Systems Query Optimization: Cases 9 SELECT * FROM A INNER JOIN B ON A.A3 = B.B3 WHERE A1 = ‘ABCDEFGHIJ’; YES NO SELECT * FROM (SELECT * FROM A WHERE A1 = ‘ABCDEFGHIJ’) INNER JOIN B ON A.A3 = B.B3; This will ensure optimal order of execution and will guarantee use of indices in the two steps.

10 D. ChristozovINF 280 DB Systems Query Optimization: Cases 10 CREATE TABLE A ( A1 CHAR(10) NOT NULL UNIQUE, A2 CHAR(20) NOT NULL UNIQUE, A3 CHAR(30), A4 CHAR(20), PRIMARY KEY (A1)); CREATE TABLE B( B1 CHAR(10) NOT NULL UNIQUE, B2 CHAR(10) NOT NULL UNIQUE, B3 CHAR(40), B4 CHAR(30), PRIMARY KEY (B1)); CREATE INDEX A.A1; CREATE INDEX A.A2; CREATE INDEX A.A3; CREATE INDEX B.B1; CREATE INDEX B.B2; CREATE INDEX B.B3; Tables: A with 1,000,000 records, and B with 500,000 records: Disk: block-size – 512 B; size of physical address – 8 B; access time – 10μs Where, different values for A.A3 are 50,000; and for B.B3 – 10,000. SELECT * FROM B INNER JOIN A ON A.A1 = B.B2 WHERE A1 = ‘ABCDEFGHIJ’ CASE3CASE3 1.Explain the query execution plan 2.Evaluate the order and height of the B+ trees for index-files, which will be used in query execution. 3.Evaluate the time (in I/O operation and in seconds) for execution of the query. 4.Rewrite the query to guarantee optimal execution

11 D. ChristozovINF 280 DB Systems Query Optimization: Cases 11 1.Explain the query execution plan Step 1:select: using the primary index for A.A1 Step 2:join: using the primary index created for B.B1 2.Evaluate the order and height of the B+ trees for index-files, which will be used in query execution. A.A1: (m-1)*10 +m*8 <=512  m = 29; 50%m = 15 (it is ok if you use 14) Number of records in one block: 512/80 = 6; Number of blocks: 1,000,000/6 = 166,667 blocks in the Master file Leaf: 166,667/15 = 11,112; next =740; next = 49; next = 3; B+ tree will have 4 levels. B.B2: (m-1)*10 +m*8 <=512  m = 29; 50%m = 15 (it is ok if you use 14) Number of records in one block: 512/90 = 5; Number of blocks: 500,000/5 = 100,000 blocks in the Master file Leaf: 500,000/15 = 33,334; next =2,224; next = 148; next = 10; next 1 B+ tree will have 5 levels. 3.Evaluate the time (in I/O operation and in seconds) for execution of the query. Step 1: 4 + 1 = 5 I/O operation to select the record from A Step 2: to combine with the row in B (B.B2 is a secondary key – condition for equality return a single row) 5 (B+ tree) + 1 (master file) = 6 I/O. Totally: 11 I/O operations = 110μs

12 D. ChristozovINF 280 DB Systems Query Optimization: Cases 12 SELECT * FROM B INNER JOIN A ON A.A1 = B.B2 WHERE A1 = ‘ABCDEFGHIJ’ SELECT * FROM B INNER JOIN (SELECT * FROM A WHERE A1 = ‘ABCDEFGHIJ’) ON A.A1 = B.B2; Instead of


Download ppt "Query Optimization Cases. D. ChristozovINF 280 DB Systems Query Optimization: Cases 2 Executable Block 1 Algorithm using Indices (if available) Temporary."

Similar presentations


Ads by Google