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 2: Graphical <strong>Execution</strong> <strong>Plans</strong> for Basic Queries<br />

If we change the query as shown in Listing 2.11, we get the execution plan shown in<br />

Figure 2.29.<br />

SELECT *<br />

FROM Production.ProductInventory<br />

ORDER BY ProductID<br />

Listing 2.11<br />

Figure 2.29<br />

Although this query is almost identical to the previous query, and it includes an ORDER<br />

BY clause, we don't see a Sort operator in the execution plan. This is because the column<br />

we are sorting by has changed, and this new column has a clustered index on it, which<br />

means that the returned data does not have to be sorted again, as it is already sorted as a<br />

byproduct of being the clustered index. The query optimizer is smart enough to recognize<br />

that the data, as retrieved through the index, is already ordered, and does not have to be<br />

ordered again.<br />

If you have no choice but to sort a lot of data, you should consider using trace events,<br />

Performance:Showplan XML, to see if any Sort Warnings are generated. To boost<br />

performance, <strong>SQL</strong> <strong>Server</strong> attempts to perform sorting in memory instead of on disk,<br />

since sorting in RAM is much faster than sorting on disk. However, if the Sort operation<br />

is large, <strong>SQL</strong> <strong>Server</strong> may have to write data to the tempdb database and sort on disk.<br />

Whenever this occurs, <strong>SQL</strong> <strong>Server</strong> generates a Sort Warning event, which we can<br />

capture using trace events. If your server is performing many Sorts, and generating many<br />

Sort Warnings, then you may need to add more RAM to your server, or to speed up<br />

tempdb access.<br />

102

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

Saved successfully!

Ooh no, something went wrong!