• SQL IN ANY ,(all any) 区别


    EXITS 和 IN 的区别:


    从效率来看:

    1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ;

    T1数据量小而T2数据量非常大时,T1<<T2 时,1) 的查询效率高。

    2) select * from T1 where T1.a in (select T2.a from T2) ;

    T1数据量非常大而T2数据量小时,T1>>T2 时,2) 的查询效率高。

    简而言之,一般式:外表大,用IN;内表大,用EXISTS。


    如果查询的两个表大小相当,那么用in和exists差别不大。

    如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:

    例如:表A(小表),表B(大表)
    1:
    select * from A where cc in (select cc from B)
    效率低,用到了A表上cc列的索引;
    select * from A where exists(select cc from B where cc=A.cc)
    效率高,用到了B表上cc列的索引。

    相反的
    2:
    select * from B where cc in (select cc from A)
    效率高,用到了B表上cc列的索引;
    select * from B where exists(select cc from A where cc=B.cc)
    效率低,用到了A表上cc列的索引。

    EXISTS与IN的使用效率的问题,通常情况下采用exists要比in效率高,因为IN不走索引,但要看实际情况具体使用:
    IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。


    (4)sql语句中 < any 和 <all 的区别


    1、查找年龄比15、16、22、21、17、18、19中任意一个都小的学生记录就有如下代码:
    select *
    from student
    where age<any(15,16,22,21,17,18,19)

    2、查找年龄比15、16、22、21、17、18、19中任意一个都大的学生记录就有如下代码:
    select *
    from student
    where age>any(15,16,22,21,17,18,19)

    从上面来看. ANY的意义不就和ALL一样了.
    ANY是小于集合里任何一个.也就是比最小的还小.
    ALL比所有的都小.不也是比最小的还小.

    二者的区别在哪.用在什么地方会有不同?

  • 相关阅读:
    HDU1879 kruscal 继续畅通工程
    poj1094 拓扑 Sorting It All Out
    (转)搞ACM的你伤不起
    (转)女生应该找一个玩ACM的男生
    poj3259 bellman——ford Wormholes解绝负权问题
    poj2253 最短路 floyd Frogger
    Leetcode 42. Trapping Rain Water
    Leetcode 41. First Missing Positive
    Leetcode 4. Median of Two Sorted Arrays(二分)
    Codeforces:Good Bye 2018(题解)
  • 原文地址:https://www.cnblogs.com/aspirant/p/5720249.html
Copyright © 2020-2023  润新知