exists

Exists与In效率分析

被刻印的时光 ゝ 提交于 2020-03-30 05:38:16
A: In:是把外表和内表做Hash 连接,而exists 是对外表作loop 循环,每次loop循环再对内表进行查询。 当查询两个表的大小相当时,用In 和 exists差别不大。 如果两个表中一个表较小,一个表较大,那么子查询表大的用exists,子查询表小的用In,效率会高的。 也就是说 IN 适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况,这样效率会高的 例如 :表a(小表),表b(大表) 1.select * from a where aid in (select aid from b) --->效率低:全程扫描b表,用到 a 表上的aid的索引,因为a表小,b表大 上面in的语句可以理解为: select * from a, ( select distinct aid from b) b1 where a.aid = b1.aid. select * from a where exists (select aid from b where b.aid = a.aid) --->效率高: 全程扫描a表,用到 b 表上的aid的索引。因为b表大。 上面的exists的语句可以理解为: for aid in ( select * from a) loop if ( exists ( select aid from b where b.aid= a.aid

oracle中in 和exists的使用效率

。_饼干妹妹 提交于 2020-03-28 08:41:54
有两个简单例子,以说明 “exists”和“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) 的查询效率高。 exists 用法: 请注意 1)句中的有颜色字体的部分 ,理解其含义; 其中 “select 1 from T2 where T1.a=T2.a” 相当于一个关联表查询,相当于 “select 1 from T1,T2 where T1.a=T2.a” 但是,如果你当当执行 1) 句括号里的语句,是会报语法错误的,这也是使用exists需要注意的地方。 “exists(xxx)”就表示括号里的语句能不能查出记录,它要查的记录是否存在。 因此“select 1”这里的 “1”其实是无关紧要的,换成“*”也没问题,它只在乎括号里的数据能不能查找出来,是否存在这样的记录,如果存在,这 1) 句的where 条件成立。 in 的用法: 继续引用上面的例子 “2) select * from T1 where T1.a in

Django Exists() / ~Exists() return if there is no matching data?

五迷三道 提交于 2020-03-25 18:56:13
问题 EDIT: As per schillingt's answer below I have switched to using Case/When: context['db_orders'] = Order.objects.filter( retailer_code=self.object.retailer_code).annotate( in_db=Case(When(Q(Subquery(self.object.suppliers.filter( supplier_code=(OuterRef('supplier_code'))) ), then=Value(True), default=Value(False), output_field=NullBooleanField())))) However I'm now struggling with an errror: FieldError at /retailers/A001/ Cannot resolve expression type, unknown output_field Original question: I

I need an JPA/SQL expert: EXISTS query on an Inner Join returns wrong result

微笑、不失礼 提交于 2020-03-25 16:11:59
问题 I have three tables and want to: Select all students from the first table, that have at least one connection to the school in district '999' in the second table and at least one connection to the teacher with social_number '101' and at least one to the teacher with number '103' in the third table. The tables are connected through the second table. I created an online sql compiler to show the problem: http://tpcg.io/FIoO79xi This query works fine and as expected, until I add the third EXISTS

I need an JPA/SQL expert: EXISTS query on an Inner Join returns wrong result

ぐ巨炮叔叔 提交于 2020-03-25 16:07:40
问题 I have three tables and want to: Select all students from the first table, that have at least one connection to the school in district '999' in the second table and at least one connection to the teacher with social_number '101' and at least one to the teacher with number '103' in the third table. The tables are connected through the second table. I created an online sql compiler to show the problem: http://tpcg.io/FIoO79xi This query works fine and as expected, until I add the third EXISTS

oracle中in和exists的区别

爱⌒轻易说出口 提交于 2020-03-23 00:07:55
in 和 exists区别 in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。 一直以来认为exists比in效率高的说法是不准确的。 如果查询的两个表大小相当,那么用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列的索引。 带in的 关联子查询 是多余的,因为in子句和子查询中相关的操作的功能是一样的。如: select staff_name from staff_member where staff_id in (select staff_id

SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别

孤街浪徒 提交于 2020-03-22 22:40:37
SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别: IN:确定给定的值是否与子查询或列表中的值相匹配。 IN 关键字使您得以选择与列表中的任意一个值匹配的行。 当要获得居住在 California、Indiana 或 Maryland 州的所有作者的姓名和州的列表时,就需要下列查询: SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID = 1 OR CategoryID = 4 OR CategoryID = 5 然而,如果使用 IN,少键入一些字符也可以得到同样的结果: SELECT ProductID, ProductName FROM Northwind.dbo.Products WHERE CategoryID IN (1, 4, 5) IN 关键字之后的项目必须用逗号隔开,并且括在括号中。 下列查询在 titleauthor 表中查找在任一种书中得到的版税少于 50% 的所有作者的 au_id,然后从 authors 表中选择 au_id 与 titleauthor 查询结果匹配的所有作者的姓名: SELECT au_lname, au_fname FROM authors WHERE au_id IN (SELECT au_id FROM

not in和not exists区别

有些话、适合烂在心里 提交于 2020-03-22 22:40:14
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引; 而not extsts 的子查询依然能用到表上的索引。 所以无论那个表大,用not exists都比not in要快。 也就是说,in和exists需要具体情况具体分析,not in和not exists就不用分析了,尽量用not exists就好了。 典型的连接类型共有3种: 排序 - - 合并连接(Sort Merge Join (SMJ) ) 嵌套循环(Nested Loops (NL) ) 哈希连接(Hash Join) 嵌套循环和哈希连接的算法还是有不同,在理论上哈希连接要快过排序和nl,当然实际情况比理论上有复杂的多,不过两者还是有差异的. 1 关联子查询与非关联子查询 关联子查询需要在内部引用外部表,而非关联子查询不要引用外部表。对于父查询中处理的记录来说,一个关联子查询是每行计算一次,然而一个非关联子查询只会执行一次,而且结果集被保存在内存中(如果结果集比较小),或者放在一张oracle临时数据段中(如果结果集比较大)。一个“标量”子查询是一个非关联子查询,返回唯一记录。如果子查询仅仅返回一个记录,那么oracle优化器会将结果缩减为一个常量,而且这个子查询只会执行一次。 / select from emp where deptno in (select deptno from dept

Oracle 中 not exists (select 'X' ...) 的含义

北城余情 提交于 2020-03-22 22:39:59
select a.col1,a.col2 from temp1 a where not exists (select 'X' from temp2 b where b.col2 = a.col1); select 'X' 可以理解成存在(exists)不存在(not exists)的含义。 如上面 找到a表中 col1的字段值不与b表中col2字段值相等的数据 从效率来看: 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。 执行方式: 通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。

IN和EXISTS的详解

送分小仙女□ 提交于 2020-03-22 22:39:30
从效率来看: 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。 执行方式: 通过使用EXISTS,Oracle会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。 in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询。 not exists:做NL,对子查询先查,有个虚表,有确定值,所以就算子查询有NULL最终也有值返回 not in:做hash,对子查询表建立内存数组,用外表匹配,那子查询要是有NULL那外表没的匹配最终无值返回。