Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>John Sansom <a href="http://www.johnsansom.com/index.php/2009/02/performance-comparison-of-select-top-1-verses-max/" rel="nofollow">covered</a> the performance characteristics of <code>MAX</code> vs. <code>TOP</code>, however his results didn't specifically answer your question.</p> <p>I think the answer lies in the fact that <code>MAX</code> is a general purpose aggregate function geared toward crunching pages and pages of data, where <code>TOP</code> is an operator geared toward only restricting the number of rows being fetched. </p> <p>In this one narrow use case, both example queries are capable of going after the same thing, and can return the same result. The query using <code>TOP</code> is benefiting from the specific optimizations afforded by using that method for this use case.</p> <p>I dumped out the XML plans for both queries, and the statement using <code>MAX</code> contained:</p> <pre class="lang-xml prettyprint-override"><code>&lt;DefinedValues&gt; &lt;DefinedValue&gt; &lt;ColumnReference Column="Expr1004" /&gt; &lt;ScalarOperator ScalarString="MAX([db].[dbo].[TheTablesJoined].[theDate])"&gt; &lt;Aggregate AggType="MAX" Distinct="false"&gt; &lt;ScalarOperator&gt; &lt;Identifier&gt; &lt;ColumnReference Database="[db]" Schema="[dbo]" Table="[TheTablesJoined]" Column="theDate" /&gt; &lt;/Identifier&gt; &lt;/ScalarOperator&gt; &lt;/Aggregate&gt; &lt;/ScalarOperator&gt; &lt;/DefinedValue&gt; &lt;/DefinedValues&gt; </code></pre> <p>The statement using <code>TOP</code> contained this in place of the XML defining what was being aggregated in the <code>MAX</code> query:</p> <pre class="lang-xml prettyprint-override"><code>&lt;TopExpression&gt; &lt;ScalarOperator ScalarString="(1)"&gt; &lt;Const ConstValue="(1)" /&gt; &lt;/ScalarOperator&gt; &lt;/TopExpression&gt; </code></pre> <p>There's a lot less going on in the execution plan when using <code>TOP</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload