30.06.2013 Views

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SQL Server Execution Plans - Red Gate Software

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: Understanding More Complex Query <strong>Plans</strong><br />

The introduction of this new functionality changes the execution plan substantially, as<br />

shown in Figure 4.6.<br />

Figure 4.6<br />

The TOP statement is now be applied row by row within the control of the APPLY<br />

function, so the second Index Scan against the ProductListPriceHistory table,<br />

and the Merge Join that joined the tables together, are no longer needed. Furthermore,<br />

only the Index Seek and Top operations are required to provide data back for the<br />

Nested Loops operation.<br />

So, which method of writing this query do you think is the most efficient? One way<br />

to find out is to run each query with the SET STATISTICS IO option set to ON. With<br />

this option set, <strong>SQL</strong> <strong>Server</strong> displays I/O statistics as part of the Messages returned by<br />

the query.<br />

When we run the first query, which uses the sub-select, the results are:<br />

(293 row(s) affected)<br />

Table 'ProductListPriceHistory'. Scan count 396, logical reads 795<br />

Table 'Product'. Scan count 1, logical reads 15, physical reads 0<br />

If we run the query using a derived table, the results are:<br />

(293 row(s) affected)<br />

Table 'ProductListPriceHistory'. Scan count 504, logical reads 1008<br />

Table 'Product'. Scan count 1, logical reads 15, physical reads 0,<br />

146

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!