gpt4 book ai didi

sql - 在 "UNION ALL" View 上查询缓慢

转载 作者:行者123 更新时间:2023-11-29 11:12:31 25 4
gpt4 key购买 nike

我有一个数据库 View ,它基本上由两个带有 UNION ALLSELECT 查询组成,如下所示:

CREATE VIEW v AS
SELECT time, etc. FROM t1 // #1...
UNION ALL
SELECT time, etc. FROM t2 // #2...

问题是表单的选择

SELECT ... FROM v WHERE time >= ... AND time < ...

执行起来真的很慢。

SELECT #1 和 #2 都相当快,索引正确等等:当我创建 View v1 和 v2 时:

CREATE VIEW v1 AS
SELECT time, etc. FROM t1 // #1...

CREATE VIEW v2 AS
SELECT time, etc. FROM t2 // #2...

同样的 SELECT,具有与上面相同的 WHERE 条件,可以单独对它们起作用。

关于问题可能出在哪里以及如何解决它有什么想法吗?

(顺便提一下,它是最新的 Postgres 版本之一。)

编辑:添加匿名查询计划(感谢@filiprem 提供指向一个很棒的工具的链接):

v1:

Aggregate  (cost=9825.510..9825.520 rows=1 width=53) (actual time=59.995..59.995 rows=1 loops=1)
-> Index Scan using delta on echo alpha (cost=0.000..9815.880 rows=3850 width=53) (actual time=0.039..53.418 rows=33122 loops=1)
Index Cond: (("juliet" >= 'seven'::uniform bravo_victor oscar whiskey) AND ("juliet" <= 'november'::uniform bravo_victor oscar whiskey))
Filter: ((NOT victor) AND ((bravo_sierra five NULL) OR ((bravo_sierra)::golf <> 'india'::golf)))

v2:

Aggregate  (cost=15.470..15.480 rows=1 width=33) (actual time=0.231..0.231 rows=1 loops=1)
-> Index Scan using yankee on six charlie (cost=0.000..15.220 rows=99 width=33) (actual time=0.035..0.186 rows=140 loops=1)
Index Cond: (("juliet" >= 'seven'::uniform bravo oscar whiskey) AND ("juliet" <= 'november'::uniform bravo oscar whiskey))
Filter: (NOT victor)

v:

Aggregate  (cost=47181.850..47181.860 rows=1 width=0) (actual time=37317.291..37317.291 rows=1 loops=1)
-> Append (cost=42.170..47132.480 rows=3949 width=97) (actual time=1.277..37304.453 rows=33262 loops=1)
-> Nested Loop Left Join (cost=42.170..47052.250 rows=3850 width=99) (actual time=1.275..37288.465 rows=33122 loops=1)
-> Hash Left Join (cost=42.170..9910.990 rows=3850 width=115) (actual time=1.123..117.797 rows=33122 loops=1)
Hash Cond: ((alpha_seven.two)::golf = (quebec_three.two)::golf)
-> Index Scan using delta on echo alpha_seven (cost=0.000..9815.880 rows=3850 width=132) (actual time=0.038..77.866 rows=33122 loops=1)
Index Cond: (("juliet" >= 'seven'::uniform bravo_victor oscar whiskey_two) AND ("juliet" <= 'november'::uniform bravo_victor oscar whiskey_two))
Filter: ((NOT victor) AND ((bravo_sierra five NULL) OR ((bravo_sierra)::golf <> 'india'::golf)))
-> Hash (cost=30.410..30.410 rows=941 width=49) (actual time=1.068..1.068 rows=941 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 75kB
-> Seq Scan on alpha_india quebec_three (cost=0.000..30.410 rows=941 width=49) (actual time=0.010..0.486 rows=941 loops=1)
-> Index Scan using mike on hotel quebec_sierra (cost=0.000..9.630 rows=1 width=24) (actual time=1.112..1.119 rows=1 loops=33122)
Index Cond: ((alpha_seven.zulu)::golf = (quebec_sierra.zulu)::golf)
-> Subquery Scan on "*SELECT* 2" (cost=34.080..41.730 rows=99 width=38) (actual time=1.081..1.951 rows=140 loops=1)
-> Merge Right Join (cost=34.080..40.740 rows=99 width=38) (actual time=1.080..1.872 rows=140 loops=1)
Merge Cond: ((quebec_three.two)::golf = (charlie.two)::golf)
-> Index Scan using whiskey_golf on alpha_india quebec_three (cost=0.000..174.220 rows=941 width=49) (actual time=0.017..0.122 rows=105 loops=1)
-> Sort (cost=18.500..18.750 rows=99 width=55) (actual time=0.915..0.952 rows=140 loops=1)
Sort Key: charlie.two
Sort Method: quicksort Memory: 44kB
-> Index Scan using yankee on six charlie (cost=0.000..15.220 rows=99 width=55) (actual time=0.022..0.175 rows=140 loops=1)
Index Cond: (("juliet" >= 'seven'::uniform bravo_victor oscar whiskey_two) AND ("juliet" <= 'november'::uniform bravo_victor oscar whiskey_two))
Filter: (NOT victor)

朱丽叶时间

最佳答案

这似乎是飞行员失误的情况。 “v”查询计划从至少 5 个不同的表中进行选择。

现在,您确定您已连接到正确的数据库吗?也许有一些时髦的 search_path 设置?也许 t1 和 t2 实际上是 View (可能在不同的模式中)?也许您在某种程度上是从错误的 View 中选择的?

澄清后编辑:

您正在使用一个名为“加入删除”的全新功能:http://wiki.postgresql.org/wiki/What%27s_new_in_PostgreSQL_9.0#Join_Removal

http://rhaas.blogspot.com/2010/06/why-join-removal-is-cool.html

当涉及 union all 时,该功能似乎不会启动。您可能必须仅使用所需的两个表来重写 View 。

另一个编辑:您似乎正在使用聚合(例如“select count(*) from v”与“select * from v”),在移除连接时可能会获得截然不同的计划。我想如果你不发布实际的查询、 View 和表定义以及使用的计划,我们就不会走得太远......

关于sql - 在 "UNION ALL" View 上查询缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com