忘了分数 發表於 2023-10-27 00:00:00

where 子句的执行顺序

<p>
        看代码:</p>
<p class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</p>
<p class="codebody">
        <br>
        set statistics io on<br>
        set statistics time on<br>
        go<br>
        set statistics profile on<br>
        go<br><br>
        use pubs<br>
        select * from authors<br>
        where (au_fname like 'S%' or au_fname like 'a%')<br>
        and (state like 'CA' or state like 'UT')<br>
        and (cast(zip as int) &gt; 90000)<br><br>
        select * from authors<br>
        where (au_fname like 'S%' or au_fname like 'a%')<br>
        and (cast(zip as int) &gt; 90000)<br>
        and (state like 'CA' or state like 'UT')<br><br>
        select * from authors<br>
        where (cast(zip as int) &gt; 90000)<br>
        and (au_fname like 'S%' or au_fname like 'a%')<br>
        and (state like 'CA' or state like 'UT')</p>
<p>
        <br>
        目的:想验证where语句的各个条件的查询顺序<br>
        环境:SQLServer 2005Express版本<br>
        步骤:显示查询计划<br>
        结果:无一例外,都被统一成了这样的代码</p>
<p class="codetitle">
        <span><u>复制代码</u></span> 代码如下:</p>
<p class="codebody">
        <br>
        |--Clustered Index Scan(OBJECT:(...), WHERE:(CONVERT(int,...,0)&gt;(90000) AND (... like 'S%' OR ... like 'a%') AND (... like 'CA' OR ... like 'UT')))</p>
<p>
        <br>
        结论:貌似在2005之前的版本中,where子句的顺序是从前往后的。但是又貌似在2005的版本中进行了优化,所有的顺序都被统一成了以过滤能力进行排序的语句。</p>
頁: [1]
查看完整版本: where 子句的执行顺序