How do you measure the cost of query? Explain.
The cost of the query is the time taken by the query to hit the database and return the result. It involves query processing time i.e., the time taken to parse and translate the query, optimize it, evaluate, execute and return the result to the user is called the cost of the query. Executing the optimized query involves hitting the primary and secondary memory based on the file organization method. Depending on file organization and the indexes used, the time taken to retrieve the data may vary. Query cost considers the number of different resources that are listed below:
- The number of disk accesses / the number of disk block transfers / the size of the table
- Time is taken by the CPU for executing the query
For simplicity, we just use the number of block transfers from disk and the number of seeks as the cost measures of a query-evaluation plan. Suppose a query needs to seek S times to fetch a record and there are b blocks that need to be returned to the user. The disk I/O cost is calculated as below
Query Cost b* tr + S * ts
Where,
b-block transfer
S-seeks
tr- time to transfer one block
ts- time for one to seek
The values of tr and ts must be calibrated for the disk system used, if tr=0.1 ms, ts =4 ms, the block size is 4 KB, and its transfer rate is 40 MB per second. With this, we can easily calculate the estimated cost of the given query evaluation plan.
Generally, for estimating the cost, we consider the worst case that could happen.
Comments
Post a Comment