Explore BrainMass

Comparison of two select queries

Here's a query:

SELECT orders.order_date, orders.customer_numb
FROM orders INNER JOIN order_lines ON orders.order_numb = order_lines.order_numb
WHERE order_lines.isbn = '0-131-4966-9';

and here it is rewritten using a subquery:

SELECT order_date, customer_numb
FROM orders
WHERE order_numb IN (SELECT order_numb
FROM order_lines
WHERE isbn = '0-131-4966-9')

Assume that the tables orders and order_lines each have 500,000 rows/tuples.

Assume (somewhat artificially) that RAM can hold 200 rows and devotes half of that to reading one table and half to reading the other table and has "other places" to store the subquery/query results.

(i) How many reads from the hard drive will the original query take?
(ii) How many reads from the hard drive would the rewritten query take?

© BrainMass Inc. brainmass.com July 23, 2018, 8:08 am ad1c9bdddf

Solution Preview

Total no. of records in each table = 500,000
No. of groups of 100 rows in each table (since we read 100 rows at a time) = 500,000 / 100 = 5,000

(i) In the original query each record of first table has to be compared against every record of second table. ...

Solution Summary

Apart from computing the number of hard drive reads in both the cases, solution briefly explains why/how these numbers are different.