- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个按周分区的表。 start_time 上有一个索引当我运行它时 -
EXPLAIN SELECT COUNT(uniq_id) FROM call_fact WHERE CAST(start_time AS DATE) = '2018-01-23' AND source_key=2;
它扫描每个分区表,而不是转到正确的子表。查询优化器不应该足够聪明以选择正确的子表吗?有没有办法强制 postgres 去正确的子表?
EXPLAIN(analyze, buffers) SELECT COUNT(uniq_id) FROM call_fact WHERE CAST(start_time AS DATE) = '2018-01-23' AND source_key=2;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=649579.52..649579.53 rows=1 width=26) (actual time=845.998..845.998 rows=1 loops=1)
Buffers: shared hit=34839 read=1312
-> Append (cost=0.12..647969.68 rows=643937 width=26) (actual time=175.416..792.387 rows=457658 loops=1)
Buffers: shared hit=34839 read=1312
-> Index Scan using call_fact_cast_start_time_idx on call_fact (cost=0.12..8.14 rows=1 width=26) (actual time=0.006..0.006 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Bitmap Heap Scan on call_fact_2017w42 (cost=259.52..43804.31 rows=13604 width=26) (actual time=16.957..16.957 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w42_cast_start_time_idx (cost=0.00..256.12 rows=13825 width=0) (actual time=16.954..16.954 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w43 (cost=258.00..43186.24 rows=13425 width=26) (actual time=14.656..14.656 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w43_cast_start_time_idx (cost=0.00..254.64 rows=13628 width=0) (actual time=14.653..14.653 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w44 (cost=272.45..45811.42 rows=14264 width=26) (actual time=5.930..5.930 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w44_cast_start_time_idx (cost=0.00..268.88 rows=14460 width=0) (actual time=5.921..5.921 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w45 (cost=258.80..43538.05 rows=13534 width=26) (actual time=10.631..10.631 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w45_cast_start_time_idx (cost=0.00..255.41 rows=13731 width=0) (actual time=10.630..10.630 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w46 (cost=258.38..43396.98 rows=13479 width=26) (actual time=14.289..14.289 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w46_cast_start_time_idx (cost=0.00..255.01 rows=13678 width=0) (actual time=14.281..14.281 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w47 (cost=203.86..34194.57 rows=10556 width=26) (actual time=13.386..13.386 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w47_cast_start_time_idx (cost=0.00..201.22 rows=10772 width=0) (actual time=13.384..13.384 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w48 (cost=279.31..47044.41 rows=14662 width=26) (actual time=10.149..10.149 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w48_cast_start_time_idx (cost=0.00..275.64 rows=14828 width=0) (actual time=10.143..10.143 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w49 (cost=270.62..45091.94 rows=14064 width=26) (actual time=8.687..8.687 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w49_cast_start_time_idx (cost=0.00..267.10 rows=14223 width=0) (actual time=8.684..8.684 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w50 (cost=251.05..41960.30 rows=13073 width=26) (actual time=18.181..18.181 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w50_cast_start_time_idx (cost=0.00..247.78 rows=13247 width=0) (actual time=18.175..18.175 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w51 (cost=238.23..39983.46 rows=12434 width=26) (actual time=10.728..10.728 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w51_cast_start_time_idx (cost=0.00..235.12 rows=12625 width=0) (actual time=10.726..10.726 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2017w52 (cost=231.37..38822.25 rows=12069 width=26) (actual time=5.907..5.907 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2017w52_cast_start_time_idx (cost=0.00..228.36 rows=12257 width=0) (actual time=5.905..5.905 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2018w01 (cost=272.97..46008.85 rows=14313 width=26) (actual time=13.909..13.909 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2018w01_cast_start_time_idx (cost=0.00..269.39 rows=14528 width=0) (actual time=13.908..13.908 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2018w02 (cost=259.65..43842.27 rows=13642 width=26) (actual time=12.807..12.807 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2018w02_cast_start_time_idx (cost=0.00..256.24 rows=13841 width=0) (actual time=12.806..12.806 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Bitmap Heap Scan on call_fact_2018w03 (cost=245.87..41477.94 rows=12905 width=26) (actual time=10.540..10.540 rows=0 loops=1)
Recheck Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared read=3
-> Bitmap Index Scan on call_fact_2018w03_cast_start_time_idx (cost=0.00..242.64 rows=13095 width=0) (actual time=10.538..10.538 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Buffers: shared read=3
-> Index Scan using call_fact_2018w04_cast_start_time_idx on call_fact_2018w04 (cost=0.43..49688.01 rows=457898 width=26) (actual time=8.644..562.016 rows=457658 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Rows Removed by Filter: 5782
Buffers: shared hit=34822 read=1270
-> Index Scan using call_fact_2018w05_cast_start_time_idx on call_fact_2018w05 (cost=0.42..4.44 rows=1 width=26) (actual time=0.039..0.039 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=3
-> Index Scan using call_fact_2018w06_cast_start_time_idx on call_fact_2018w06 (cost=0.14..8.16 rows=1 width=98) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w07_cast_start_time_idx on call_fact_2018w07 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w08_cast_start_time_idx on call_fact_2018w08 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w09_cast_start_time_idx on call_fact_2018w09 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w10_cast_start_time_idx on call_fact_2018w10 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w11_cast_start_time_idx on call_fact_2018w11 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w12_cast_start_time_idx on call_fact_2018w12 (cost=0.14..8.16 rows=1 width=98) (actual time=0.002..0.002 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w13_cast_start_time_idx on call_fact_2018w13 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w14_cast_start_time_idx on call_fact_2018w14 (cost=0.14..8.16 rows=1 width=98) (actual time=0.002..0.002 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w15_source_key_idx on call_fact_2018w15 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: (source_key = 2)
Filter: ((start_time)::date = '2018-01-23'::date)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w16_source_key_idx on call_fact_2018w16 (cost=0.14..8.16 rows=1 width=98) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: (source_key = 2)
Filter: ((start_time)::date = '2018-01-23'::date)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w17_cast_start_time_idx on call_fact_2018w17 (cost=0.14..8.16 rows=1 width=98) (actual time=0.004..0.004 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
-> Index Scan using call_fact_2018w18_cast_start_time_idx on call_fact_2018w18 (cost=0.14..8.16 rows=1 width=98) (actual time=0.005..0.005 rows=0 loops=1)
Index Cond: ((start_time)::date = '2018-01-23'::date)
Filter: (source_key = 2)
Buffers: shared hit=1
Planning time: 68.906 ms
Execution time: 846.648 ms
最佳答案
start_time
的类型是什么?我假设它是 timestamp
,而不是 date
。
表达式 CAST(start_time AS DATE)
阻止优化器在 start_time
上使用索引。通常,列上的任何类型的表达式都会阻止优化器使用索引。
编写此类查询的常用方法是比较原始列值,而不将其包装到具有闭-开区间的表达式中。
SELECT COUNT(uniq_id)
FROM call_fact
WHERE
source_key=2
AND start_time >= '2018-01-23'
AND start_time < '2018-01-24'
;
Postgres 应该足够聪明,可以将 date
类型的常量表达式 2018-01-23
隐式转换为 timestamp
类型。 (而不是将 start_time
列的值转换为 date
类型。将什么类型转换为什么类型由隐式类型转换规则控制。在 SQL Server 中它被称为“类型优先”,但我在 Postgres 文档中找不到相关页面。
为了使其显式而不依赖于类型优先级和隐式规则,我将以 timestamp
类型与列类型相匹配的方式显式编写日期/时间文字。
SELECT COUNT(uniq_id)
FROM call_fact
WHERE
source_key=2
AND start_time >= '2018-01-23 00:00:00'
AND start_time < '2018-01-24 00:00:00'
;
当以这种方式编写查询时,没有什么可以阻止优化器在 start_time
上使用索引。
关于sql - Postgres 分区 - 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48451859/
在这段令人惊叹的视频 ( https://www.youtube.com/watch?v=udix3GZouik ) 中,Alex Blom 谈到了 Ember 在移动世界中的“黑客攻击”。 在 22
我们希望通过我们的应用收集使用情况统计信息。因此,我们希望在服务器端的某个地方跟踪用户操作。 就性能而言,哪个选项更合适: 在 App Engine 请求日志中跟踪用户操作。即为每个用户操作写入一个日
在针对对象集合的 LINQ 查询的幕后究竟发生了什么?它只是语法糖还是发生了其他事情使其更有效的查询? 最佳答案 您是指查询表达式,还是查询在幕后的作用? 查询表达式首先扩展为“普通”C#。例如: v
我正在构建一个简单的照片库应用程序,它在列表框中显示图像。 xaml 是:
对于基于 Web 的企业应用程序,使用“静态 Hashmap 存储对象” 和 apache java 缓存系统有何优缺点?哪一个最有利于性能并减少堆内存问题 例如: Map store=Applica
我想知道在性能方面存储类变量的最佳方式是什么。我的意思是,由于 Children() 函数,存储一个 div id 比查找所有其他类名更好。还是把类名写在变量里比较好? 例如这样: var $inne
我已经阅读了所有这些关于 cassandra 有多快的文章,例如单行读取可能需要大约 5 毫秒。 到目前为止,我不太关心我的网站速度,但是随着网站变得越来越大,一些页面开始需要相当多的查询,例如一个页
最近,我在缓存到内存缓存之前的查询一直需要很长时间才能处理!在这个例子中,它花费了 10 秒。在这种情况下,我要做的就是获得 10 个最近的点击。 我感觉它加载了所有 125,592 行然后只返回 1
我找了几篇文章(包括SA中的一些问题),试图找到基本操作的成本。 但是,我尝试制作自己的小程序,以便自己进行测试。在尝试测试加法和减法时,我遇到了一些问题,我用简单的代码向您展示了这一点
这个问题在这里已经有了答案: Will Java app slow down by presence of -Xdebug or only when stepping through code? (
我记得很久以前读过 with() 对 JavaScript 有一些严重的性能影响,因为它可能对范围堆栈进行非确定性更改。我很难找到最近对此的讨论。这仍然是真的吗? 最佳答案 与其说 with 对性能有
我们有一个数据仓库,其中包含非规范化表,行数从 50 万行到 6 多万行不等。我正在开发一个报告解决方案,因此出于性能原因我们正在使用数据库分页。我们的报告有搜索条件,并且我们已经创建了必要的索引,但
我有一条有效的 SQL 语句,但需要很长时间才能处理 我有一个 a_log 表和一个 people 表。我需要在 people 表中找到给定人员的每个 ID 的最后一个事件和关联的用户。 SELECT
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
通常当我建立一个站点时,我将所有的 CSS 放在一个文件中,并且一次性定义与一组元素相关的所有属性。像这样: #myElement { color: #fff; background-
两者之间是否存在任何性能差异: p { margin:0px; padding:0px; } 并省略最后的分号: p { margin:0px; padding:0px } 提前致谢!
我的应用程序 (PHP) 需要执行大量高精度数学运算(甚至可能出现一共100个数字) 通过这个论坛的最后几篇帖子,我发现我必须使用任何高精度库,如 BC Math 或 GMP,因为 float 类型不
我一直在使用 javamail 从 IMAP 服务器(目前是 GMail)检索邮件。 Javamail 非常快速地从服务器检索特定文件夹中的消息列表(仅 id),但是当我实际获取消息(仅包含甚至不包含
我非常渴望开发我的第一个 Ruby 应用程序,因为我的公司终于在内部批准了它的使用。 在我读到的关于 Ruby v1.8 之前的所有内容中,从来没有任何关于性能的正面评价,但我没有发现关于 1.9 版
我是 Redis 的新手,我有一个包含数百万个成员(member) ID、电子邮件和用户名的数据集,并且正在考虑将它们存储在例如列表结构中。我认为 list 和 sorted set 可能最适合我的情
我是一名优秀的程序员,十分优秀!