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 5: Controlling <strong>Execution</strong> <strong>Plans</strong> with Hints<br />

Original (Hash)<br />

Table 'Illustration'. Scan count 1, logical reads 273<br />

Table 'ProductModelIllustration'. Scan count 1, logical reads 183<br />

Table 'Worktable'. Scan count 0, logical reads 0<br />

Table 'ProductModel'. Scan count 1, logical reads 14<br />

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

Loop<br />

Table 'Illustration'. Scan count 1, logical reads 273<br />

Table 'ProductModelIllustration'. Scan count 1, logical reads 183<br />

Table 'Product'. Scan count 1, logical reads 555<br />

Table 'ProductModel'. Scan count 1, logical reads 14<br />

Merge<br />

Table 'Illustration'. Scan count 1, logical reads 273<br />

Table 'Worktable'. Scan count 0, logical reads 0<br />

Table 'ProductModelIllustration'. Scan count 1, logical reads 2<br />

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

Table 'ProductModel'. Scan count 1, logical reads 14<br />

This shows us that the Hash Match and Nested Loops joins required almost exactly the<br />

same number of reads as the Merge Join to arrive at the required data set. The differences<br />

arise when we see that, in order to support the Nested Loops join, 555 reads were required<br />

instead of 15 for both the Merge Join and Hash Match. The key difference is the number<br />

of reads of the ProductModelIllustration table in the Merge Join; it's only 2, as<br />

opposed to the 183 in the other two queries.<br />

This illustrates the point that the optimizer does not always choose an optimal plan.<br />

It's always best to look at the properties of the Select operator to see if full optimization<br />

occurred and if there was a timeout. Neither was the case here; it was a full optimization<br />

and the optimizer thought it found the best possible plan in the time allotted. Based on<br />

the statistics in the index and the amount of time it had to calculate its results, it must<br />

have decided that the Hash Match would perform faster.<br />

Although we've positively influenced query performance for the time being, the downside<br />

is that, as the data changes over time within the tables, it's possible that the Merge Join<br />

will cease to function better. However, because we've hard coded the join, no new plan<br />

will be generated by the optimizer as the data changes, as would normally be the case.<br />

217

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

Saved successfully!

Ooh no, something went wrong!