- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在执行以下两个查询时,我注意到查询计划存在严重差异。这是为什么?
select * from table1
where id = 'dummy' or id in (select id from table2 where id = 'dummy')
查询计划
Seq Scan on table1 (cost=8.30..49611.63 rows=254478 width=820) (actual time=535.477..557.431 rows=1 loops=1)
Filter: (((code)::text = 'dummy'::text) OR (hashed SubPlan 1))
Rows Removed by Filter: 510467
SubPlan 1
-> Index Scan using idx on table2 (cost=0.29..8.30 rows=1 width=8) (actual time=0.009..0.012 rows=0 loops=1)
Index Cond: ((id)::text = 'dummy'::text)
Planning Time: 0.165 ms
Execution Time: 557.517 ms
select * from table1
where id = 'dummy'
union
select * from table1
where id in (select id from table2 where id = 'dummy')
Unique (cost=25.22..25.42 rows=2 width=5818) (actual time=0.045..0.047 rows=1 loops=1)
-> Sort (cost=25.22..25.23 rows=2 width=5818) (actual time=0.045..0.046 rows=1 loops=1)
Sort Method: quicksort Memory: 25kB
-> Append (cost=0.42..25.21 rows=2 width=5818) (actual time=0.016..0.026 rows=1 loops=1)
-> Index Scan using id on table1 (cost=0.42..8.44 rows=1 width=820) (actual time=0.015..0.016 rows=1 loops=1)
Index Cond: ((id)::text = 'dummy'::text)
-> Nested Loop (cost=0.71..16.74 rows=1 width=820) (actual time=0.009..0.009 rows=0 loops=1)
-> Index Scan using idx on table2 (cost=0.29..8.30 rows=1 width=8) (actual time=0.008..0.008 rows=0 loops=1)
Index Cond: ((id)::text = 'dummy'::text)
-> Index Scan using pkey on table1 (cost=0.42..8.44 rows=1 width=820) (never executed)
Index Cond: (id = table2.id)
Planning Time: 0.753 ms
Execution Time: 0.131 ms
所以您可以看到的主要区别是第一个查询返回 254478 行,而第二个查询只返回 2 行。这是为什么?
最佳答案
请再做一次测试——运行这两个查询——它们给出的结果是否与未经我更改的查询的结果相同?
select * from table1
where table1.id = 'dummy' or
table1.id in (select table2.id from table2 where table2.id = 'dummy')
select * from table1
where table1.id = 'dummy'
union
select * from table1
where table1.id in (select table2.id from table2 where table2.id = 'dummy')
我不认为您是在与我们分享您的实际代码——因为您编写的代码毫无意义——您在子查询中返回一个等于“dummy”的 ID 列表——所以您将只需多次获取虚拟列表即可。
请注意这些评论是不正确的,因为它们对结果没有影响——操作顺序按预期工作
这样做会得到什么结果:
select * from table1
where (id = 'dummy') or id in (select id from table2 where id = 'dummy')
您的查询给出更多结果的原因是因为它从表 1 中选择记录,其中 id 等于 dummy 或 id = id。原始帖子中的查询为您提供了所有记录。 OR 应用于第一个表达式而不拆分两个表达式。
关于postgresql - 为什么 union 与 "or"运算符的查询计划有很大差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72887103/
有没有一个简单的答案:为什么 GHC 这么大? OCaml:2MB Python:15MB SBCL:9MB OpenJRE - 26MB GHC:113MB 对“如果 Haskell 是正确的工具,
我发现我的 access_log 占用了我的大部分硬盘。它的大小超过 200 GB。我怎样才能重置它? 我在装有 Plesk 的 CentOS 服务器上使用 Apache 2.2.3。 谢谢你们 !
我正在使用 Java 中的 BeanShell 解释器来解决字符串计算问题。问题是我自己做了一些解释器无法完成的解析并将部分结果存储在 BigInteger 中。然后我将所有内容拼凑起来并交给解释器来
我在我的 android 项目中使用 OpenCV native 库。它是一个带有一个 CameraScreen 的 hello world 项目,它已经有 40mb。我怎样才能减少 apk 的大小,
我使用基于 Laravel 和 Vue.js 的 Laravue Dashboard,在在线服务器上运行,而不是本地主机,它是全新安装,没有进行任何编辑。 我的问题是: 运行 npm run watc
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我广泛使用了 Kendo DataSourceResult ToDataSourceResult(this IQueryable enumerable, DataSourceRequest reque
我使用 phonegap 构建 html\css 应用程序陪审团手机给我 div: 并且模拟器中的最小高度太大,我在底部看到黑线像这样: 如果我在 css 文件中更改 min-height: 736
以下工作正常但速度太慢。只需要知道B表中有匹配的记录,有什么技巧吗? (奇怪的是相反的搜索:找到不加入的记录(IS NULL)非常快) SELECT TableA.id FROM TableA
我正在使用 Vue.js,我的项目中只有 4 个组件。 我只导入了bootstrap、jquery 和lodash: import { map } from 'lodash'; import 'boo
我有 2 个字谜检测功能;一个使用排序和比较,另一个跟踪每个字母字符出现的次数。 这里假设传递给函数的两个字符串是相同的,第一个随机生成(未排序),第二个 = 给第一个,这样两个函数都“一路”执行并返
我正在尝试编写一个脚本,该脚本将通过 HTTP 同时下载最多 N 个文件。 我以前用过 AnyEvent::Worker::Pool管理阻塞任务池。我也用过 AnyEvent::HTTP结合AnyEv
我是一名优秀的程序员,十分优秀!