- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下表结构:
AdPerformance
id
ad_id
impressions
Targeting
value
AdActions
app_starts
Ad
id
name
parent_id
AdTargeting
id
targeting_
ad_id
Targeting
id
name
value
AdProduct
id
ad_id
name
我需要通过限制产品名称的定位来聚合数据,因此我编写了以下查询:
SELECT ad_performance.ad_id, targeting.value AS targeting_value,
sum(impressions) AS impressions,
sum(app_starts) AS app_starts
FROM ad_performance
LEFT JOIN ad on ad.id = ad_performance.ad_id
LEFT JOIN ad_actions ON ad_performance.id = ad_actions.ad_performance_id
RIGHT JOIN (
SELECT ad_id, value from targeting, ad_targeting
WHERE targeting.id = ad_targeting.id and targeting.name = 'gender'
) targeting ON targeting.ad_id = ad.parent_id
WHERE ad_performance.ad_id IN
(SELECT ad_id FROM ad_product WHERE product = 'iphone')
GROUP BY ad_performance.ad_id, targeting_value
然而,上述 ANALYZE
命令中的查询对于约 1M 条记录大约需要 5 秒。
有什么办法可以改善吗?
我确实有外键索引
已更新
查看分析的输出
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=5787.28..5789.87 rows=259 width=254) (actual time=3283.763..3286.015 rows=5998 loops=1)
Group Key: adobject_performance.ad_id, targeting.value
Buffers: shared hit=3400223
-> Nested Loop Left Join (cost=241.63..5603.63 rows=8162 width=254) (actual time=10.438..2774.664 rows=839720 loops=1)
Buffers: shared hit=3400223
-> Nested Loop (cost=241.21..1590.52 rows=8162 width=250) (actual time=10.412..703.818 rows=839720 loops=1)
Join Filter: (adobject.id = adobject_performance.ad_id)
Buffers: shared hit=36755
-> Hash Join (cost=240.78..323.35 rows=9 width=226) (actual time=10.380..20.332 rows=5998 loops=1)
Hash Cond: (ad_product.ad_id = ad.id)
Buffers: shared hit=190
-> HashAggregate (cost=128.98..188.96 rows=5998 width=4) (actual time=3.788..6.821 rows=5998 loops=1)
Group Key: ad_product.ad_id
Buffers: shared hit=39
-> Seq Scan on ad_product (cost=0.00..113.99 rows=5998 width=4) (actual time=0.011..1.726 rows=5998 loops=1)
Filter: ((product)::text = 'ft2_iPhone'::text)
Rows Removed by Filter: 1
Buffers: shared hit=39
-> Hash (cost=111.69..111.69 rows=9 width=222) (actual time=6.578..6.578 rows=5998 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 241kB
Buffers: shared hit=151
-> Hash Join (cost=30.26..111.69 rows=9 width=222) (actual time=0.154..4.660 rows=5998 loops=1)
Hash Cond: (adobject.parent_id = adobject_targeting.ad_id)
Buffers: shared hit=151
-> Seq Scan on adobject (cost=0.00..77.97 rows=897 width=8) (actual time=0.009..1.449 rows=6001 loops=1)
Buffers: shared hit=69
-> Hash (cost=30.24..30.24 rows=2 width=222) (actual time=0.132..0.132 rows=2 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 1kB
Buffers: shared hit=82
-> Nested Loop (cost=0.15..30.24 rows=2 width=222) (actual time=0.101..0.129 rows=2 loops=1)
Buffers: shared hit=82
-> Seq Scan on targeting (cost=0.00..13.88 rows=2 width=222) (actual time=0.015..0.042 rows=79 loops=1)
Filter: (name = 'age group'::targeting_name)
Rows Removed by Filter: 82
Buffers: shared hit=1
-> Index Scan using advertising_targeting_pkey on adobject_targeting (cost=0.15..8.17 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=79)
Index Cond: (id = targeting.id)
Buffers: shared hit=81
-> Index Scan using "fki_advertising_peformance_advertising_entity_id -> advertising" on adobject_performance (cost=0.42..89.78 rows=4081 width=32) (actual time=0.007..0.046 rows=140 loops=5998)
Index Cond: (ad_id = ad_product.ad_id)
Buffers: shared hit=36565
-> Index Scan using facebook_advertising_actions_pkey on facebook_adobject_actions (cost=0.42..0.48 rows=1 width=12) (actual time=0.001..0.002 rows=1 loops=839720)
Index Cond: (ad_performance.id = ad_performance_id)
Buffers: shared hit=3363468
Planning time: 1.525 ms
Execution time: 3287.324 ms
(46 rows)
最佳答案
在这里盲目射击,因为我们还没有得到 EXPLAIN 的结果,但是,如果你在 CTE 中取出你的 targeting
表,Postgres 应该更好地处理这个查询:
WITH targeting AS
(
SELECT ad_id, value from targeting, ad_targeting
WHERE targeting.id = ad_targeting.id and targeting.name = 'gender'
)
SELECT ad_performance.ad_id, targeting.value AS targeting_value,
sum(impressions) AS impressions,
sum(app_starts) AS app_starts
FROM ad_performance
LEFT JOIN ad on ad.id = ad_performance.ad_id
LEFT JOIN ad_actions ON ad_performance.id = ad_actions.ad_performance_id
RIGHT JOIN targeting ON targeting.ad_id = ad.parent_id
WHERE ad_performance.ad_id IN
(SELECT ad_id FROM ad_product WHERE product = 'iphone')
GROUP BY ad_performance.ad_id, targeting_value
A useful property of WITH queries is that they are evaluated only once per execution of the parent query, even if they are referred to more than once by the parent query or sibling WITH queries. Thus, expensive calculations that are needed in multiple places can be placed within a WITH query to avoid redundant work. Another possible application is to prevent unwanted multiple evaluations of functions with side-effects.
关于sql - 提高 SQL 查询的运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894248/
考虑以下示例代码: void main() { List array = []; for (int i = 0; i newList = array.where( (value) => v
我在java中实现了textrank,但它看起来很慢。有谁知道它的预期性能吗? 如果预计速度不会很慢,则可能是以下任一问题: 1) 似乎没有办法在 JGraphT 时间内创建一条边并同时为其添加权重,
我正在尝试提交解决方案(使用一些具有编译时间限制的在线编译器)来对数组进行排序 - 这是我的代码片段 - class TSORT { public static void main(Strin
只是一个困惑......部分C++示例代码如下 我只是重新编辑了整个帖子。抱歉造成任何混淆 int i, j; i = 0; // c1 j = 0; // c2 while (i
我正在使用 OpenMP 运行一些并行工作,但发现加速无法很好地扩展。然后我发现随着线程数量的增加,完成相同工作量的时间也会增加。这是示例: void fillingVec(vector& vec)
我目前正在分析 maximum subarray problem 用于强力算法和分而治之算法(递归)。 使用蛮力算法,最坏情况下的运行时间为 O(n^2)。使用递归算法,最坏情况下的运行时间为 O(n
如果我有两个数组都有 10000 个项目,现在我想将它们合并到一个数组中,所以我用 concat 来完成: array1=array1.concat(array2); 但是有人知道运行时间是多少吗?
{ clock_t t1, t2; double time1 = 0; t1 = clock(); bubbleSort(data, n); t2 = cloc
我在某处读到有人可以在运行时访问配置值,但不能在设计时访问。在这种情况下,运行时和设计时有什么区别? 最佳答案 设计时间是有人用愉快的“看起来不错!”在我们的 Word 文档和 UML 图表上签字的时
我正在比较我们一个项目的两个分支的性能,一个比另一个慢得多。我注意到其中一个的 GC 运行计数更高(见下图)。 更有趣的是,运行时间要长很多倍,远远超过额外运行所能解释的时间。什么可以解释运行次数增加
我想以可读的方式格式化 log4j 经过的运行时间,%r 参数: [00:36:25.844] 和 [01 13:35:25.844] [时:分:ss.SSS] ... [dd 时:分:ss.SSS]
如何计算此代码的大 O 运行时效率?我的直觉告诉我它是 O(n^3),但我不确定,因为我不确定循环是独立的还是相关的。 for (i=1; i<=n; i++) for (j=1; j<=n;
确定这些不同循环的大 O 运行时间? for i = 1 to n { ... for j = 1 to 2*i { ... k = j; while (k>=0)
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
斯内普的《Unfriendly Algorithms for Wizards》教科书声称合并的运行时间排序是 O(n^4)。这种说法是否正确? 解决方案:是的。这个说法在技术上是正确的,因为 O(n^
我有一系列问题需要反馈和答案。我会评论我的想法,这不是家庭作业而是准备为了我的考试。 我的主要问题是确定不同情况下循环的迭代。试图弄清楚这一点会如何? 评估运行时间。 Q2。 for(int i =
我试图找到一个合理的机会来测量我的 java 程序/程序部分的速度,例如测试两种方法中哪一种执行得更快。注释掉这两种方法中的一种,确定运行时间,最后比较时间。我不想在我的程序中使用任何代码,例如: p
好吧,这些都是非常简单的方法,而且有几个,所以当它们都是同一件事时,我不想只创建多个问题。 BigO 是我的弱点。我只是想不通他们是如何得出这些答案的。无论如何,您是否可以让我深入了解您对分析其中一些
如何计算此给定代码的 Theta 运行时间: void f(int n) { for (int i=3; i
使用 leiningen 创建 uberjar 并使用 java -jar foo-uberjar.jar 运行该 jar 后程序运行良好,最后一行代码执行得相当快,但程序在关闭前挂了大约一分钟。这是
我是一名优秀的程序员,十分优秀!