zoukankan      html  css  js  c++  java
  • Sql中的Exists和in

    最近学习数据库的分页算法,提到第一种

    SELECT TOP 页大小 *
    FROM table1
    WHERE id NOT IN
              (
              
    SELECT TOP 页大小*(-1) id FROM table1 ORDER BY id
              )
    ORDER BY id

    HumanResources.Employee是Adventrureworks里的表,一共290条数据

          SELECT top 10 *  FROM HumanResources.Employee a where EmployeeID not in (

                  SELECT TOP 200 EmployeeID order by EmployeeID) order by EmployeeID ;

    执行结果正确,EmployeeID为201到210的数据查出来了

    想到很多人不少EXISTS效率高吗?改一下

    SELECT top 10 *  FROM HumanResources.Employee a where not EXISTS (
         SELECT TOP 200 EmployeeID FROM HumanResources.Employee where a.EmployeeID = EmployeeID order by EmployeeID )
     order by EmployeeID;

    结果一条没有。很奇怪,找了好久,看了http://www.2cto.com/database/201109/104960.html才知道“in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询”。一直以来认为exists比in效率高的说法是不准确的。






  • 相关阅读:
    Python2 cmp() 函数
    Python round() 函数
    Python floor() 函数
    Python ceil() 函数
    Python abs() 函数
    Python oct() 函数
    Python ord() 函数
    Python hex() 函数
    Python2 unichr() 函数
    Android--------工具类StatusBarUtil实现完美状态栏
  • 原文地址:https://www.cnblogs.com/SimpleCode/p/3165861.html
Copyright © 2011-2022 走看看