Chapter: Fundamentals of Database Systems - Query Processing and Optimization, and Database Tuning - Algorithms for Query Processing and Optimization

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Examples of Cost Functions for SELECT

Algorithm S9 involves an intersection of record pointers after they have been retrieved by some other means, such as algorithm S6, and so the cost function will be based on the cost for S6.

Examples of Cost Functions for SELECT

 

We now give cost functions for the selection algorithms S1 to S8 discussed in Previous Section 19.3.1 in terms of number of block transfers between memory and disk. Algorithm S9 involves an intersection of record pointers after they have been retrieved by some other means, such as algorithm S6, and so the cost function will be based on the cost for S6. These cost functions are estimates that ignore computation time, storage cost, and other factors. The cost for method Si is referred to as CSi block accesses.

 

        S1—Linear search (brute force) approach. We search all the file blocks to retrieve all records satisfying the selection condition; hence, CS1a = b. For an equality condition on a key attribute, only half the file blocks are searched on the average before finding the record, so a rough estimate for CS1b = (b/2) if the record is found; if no record is found that satisfies the condition, CS1b = b.


        S2—Binary search. This search accesses approximately CS2 = log2b + (s/bfr) 1 file blocks. This reduces to log2b if the equality condition is on a unique (key) attribute, because s = 1 in this case.

 

        S3a—Using a primary index to retrieve a single record. For a primary index, retrieve one disk block at each index level, plus one disk block from the data file. Hence, the cost is one more disk block than the number of index levels: CS3a = x + 1.

 

        S3b—Using a hash key to retrieve a single record. For hashing, only one

 

disk block needs to be accessed in most cases. The cost function is approxi-mately CS3b = 1 for static hashing or linear hashing, and it is 2 disk block accesses for extendible hashing (see Section 17.8).

 

        S4—Using an ordering index to retrieve multiple records. If the comparison condition is >, >=, <, or <= on a key field with an ordering index,

 

roughly half the file records will satisfy the condition. This gives a cost function of CS4 = x + (b/2). This is a very rough estimate, and although it may be correct on the average, it may be quite inaccurate in individual cases. A more accurate estimate is possible if the distribution of records is stored in a histogram.

S5—Using a clustering index to retrieve multiple records. One disk block is accessed at each index level, which gives the address of the first file disk block in the cluster. Given an equality condition on the indexing attribute, s  records will satisfy the condition, where s is the selection cardinality of the indexing attribute. This means that (s/bfr) file blocks will be in the cluster of file blocks that hold all the selected records, giving CS5 = x + (s/bfr) .

 

            S6—Using a secondary (B+-tree) index. For a secondary index on a key (unique) attribute, the cost is x + 1 disk block accesses. For a secondary index on a nonkey (nonunique) attribute, s records will satisfy an equality condition, where s is the selection cardinality of the indexing attribute. However, because the index is nonclustering, each of the records may reside on a differ-ent disk block, so the (worst case) cost estimate is CS6a = x + 1 + s. The additional 1 is to account for the disk block that contains the record pointers after the index is searched (see Figure 18.5). If the comparison condition is >, >=, <, or <= and half the file records are assumed to satisfy the condition, then (very roughly) half the first-level index blocks are accessed, plus half the file records via the index. The cost estimate for this case, approximately, is CS6b = x + (bI1/2) + (r/2). The r/2 factor can be refined if better selectivity estimates are available through a histogram. The latter method CS6b can be very costly.

 

        S7—Conjunctive selection. We can use either S1 or one of the methods S2 to S6 discussed above. In the latter case, we use one condition to retrieve the records and then check in the main memory buffers whether each retrieved record satisfies the remaining conditions in the conjunction. If multiple indexes exist, the search of each index can produce a set of record pointers (record ids) in the main memory buffers. The intersection of the sets of record pointers (referred to in S9) can be computed in main memory, and then the resulting records are retrieved based on their record ids.

 

        S8—Conjunctive selection using a composite index. Same as S3a, S5, or S6a, depending on the type of index.

 

Example of Using the Cost Functions. In a query optimizer, it is common to enumerate the various possible strategies for executing a query and to estimate the costs for different strategies. An optimization technique, such as dynamic program-ming, may be used to find the optimal (least) cost estimate efficiently, without hav-ing to consider all possible execution strategies. We do not discuss optimization algorithms here; rather, we use a simple example to illustrate how cost estimates may be used. Suppose that the EMPLOYEE file in Figure 3.5 has rE = 10,000 records stored in bE = 2000 disk blocks with blocking factor bfrE = 5 records/block and the following access paths:

 

1.   A clustering index on Salary, with levels xSalary = 3 and average selection car-dinality sSalary = 20. (This corresponds to a selectivity of slSalary = 0.002).

2.   A secondary index on the key attribute Ssn, with xSsn = 4 (sSsn = 1, slSsn = 0.0001).

3.        A secondary index on the nonkey attribute Dno, with xDno = 2 and first-level index blocks bI1Dno = 4. There are dDno = 125 distinct values for Dno, so the selectivity of Dno is slDno = (1/dDno) = 0.008, and the selection cardinality is sDno = (rE * slDno) = (rE/dDno) = 80.

4.   A secondary index on Sex, with xSex = 1. There are dSex = 2 values for the Sex attribute, so the average selection cardinality is sSex = (rE /dSex) = 5000. (Note that in this case, a histogram giving the percentage of male and female employees may be useful, unless they are approximately equal.)

 

We illustrate the use of cost functions with the following examples:


The cost of the brute force (linear search or file scan) option S1 will be estimated as CS1a = bE = 2000 (for a selection on a nonkey attribute) or CS1b = (bE /2) = 1000 (average cost for a selection on a key attribute). For OP1 we can use either method

S1 or method S6a; the cost estimate for S6a is CS6a = xSsn + 1 = 4 + 1 = 5, and it is chosen over method S1, whose average cost is CS1b = 1000. For OP2 we can use

either method S1 (with estimated cost CS1a = 2000) or method S6b (with estimated cost CS6b = xDno + (bI1Dno/2) + (rE /2) = 2 + (4/2) + (10,000/2) = 5004), so we choose the linear search approach for OP2. For OP3 we can use either method S1 (with estimated cost CS1a = 2000) or method S6a (with estimated cost CS6a = xDno + sDno = 2 + 80 = 82), so we choose method S6a.

 

Finally, consider OP4, which has a conjunctive selection condition. We need to esti-mate the cost of using any one of the three components of the selection condition to retrieve the records, plus the linear search approach. The latter gives cost estimate CS1a = 2000. Using the condition (Dno = 5) first gives the cost estimate CS6a = 82. Using the condition (Salary > 30,000) first gives a cost estimate CS4 = xSalary + (bE /2) 3 + (2000/2) = 1003. Using the condition (Sex = ‘F’) first gives a cost estimate CS6a xSex + sSex = 1 + 5000 = 5001. The optimizer would then choose method S6a on the secondary index on Dno because it has the lowest cost estimate. The condition (Dno = 5) is used to retrieve the records, and the remaining part of the conjunctive condition (Salary > 30,000 AND Sex = ‘F’) is checked for each selected record after it is retrieved into memory. Only the records that satisfy these additional conditions are included in the result of the operation.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.