sql中一些常用的函数

来源:互联网 发布:java jvm原理 编辑:程序博客网 时间:2024/06/03 01:33

1.in 与 exists

查询要求:查aa_than_notes表里ename字段值等于t172表里c809000007字段值。算出总和。

  select count(*)
    from aa_than_notes tt
   where exists
   (select r.c809000007 from t172 r where tt.ename = r.c809000007)

 

等于

   select count(*)
     from aa_than_notes tt
    where tt.ename in (select r.c809000007 from t172 r)

in是把外表与内表做hash连接,而exists是对外表做loop循环,每次循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的。

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

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

 

原创粉丝点击