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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2: Graphical <strong>Execution</strong> <strong>Plans</strong> for Basic Queries<br />

Quite often, aggregations within queries can be expensive operations. About the only<br />

way to "speed" the performance of an aggregation via code is to ensure that you have a<br />

restrictive WHERE clause to limit the number of rows that need to be aggregated, thus<br />

reducing the amount of aggregation that needs to be done. You can also pre-aggregate<br />

data by using an indexed view.<br />

Filter<br />

If we add a simple HAVING clause to our previous query, our execution plan gets<br />

more complex.<br />

SELECT [City],<br />

COUNT([City]) AS CityCount<br />

FROM [Person].[Address]<br />

GROUP BY [City]<br />

HAVING COUNT([City]) > 1<br />

Listing 2.13<br />

The execution plan now looks as shown in Figure 2.31.<br />

Figure 2.31<br />

By adding the HAVING clause, the Filter operator appears in the execution plan. The<br />

Filter operator is applied to limit the output to those values of the column, City, that are<br />

greater than 1, in order to satisfy the HAVING clause. One useful bit of knowledge to take<br />

away from this plan is that the optimizer does not apply the HAVING clause until all the<br />

104

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

Saved successfully!

Ooh no, something went wrong!