Optimization issues

Although the query optimizer can efficiently optimize most queries, these issues may effect the optimizer’s efficiency:

These situations highlight the need to follow some best practices that allow the query optimizer to perform at its full potential:

Creating search arguments

Follow these guidelines when you write search arguments for your queries:

Use of SQL derived tables

Queries expressed as a single SQL statement exploit the query processor better than queries expressed in two or more SQL statements. SQL-derived tables enable you to express, in a single step, what might otherwise require several SQL statements and temporary tables, especially where intermediate aggregate results must be stored. For example:

select dt_1.* from 
   (select sum(total_sales) 
        from titles_west group by total_sales)
               dt_1(sales_sum),
   (select sum(total_sales) 
         from titles_east group by total_sales)
               dt_2(sales_sum)
where dt_1.sales_sum = dt_2.sales_sum

Here, aggregate results are obtained from the SQL derived tables dt_1 and dt_2, and a join is computed between the two SQL derived tables. Everything is accomplished in a single SQL statement.

For more information, see Chapter 9, “SQL Derived Tables,” in the Transact-SQL User's Guide.

Tuning according to object sizes

To understand query and system behavior, know the sizes of your tables and indexes. At several stages of tuning work, you need size data to:

See the System Administration Guide: Volume 2 for more information on sizing.