Sql 添加第二个连接条件会以指数方式增加查询时间
所以,我使用的是红移(基于postgres)。不幸的是,我不能分享我的数据(原因显而易见),但这更像是一个概念性的问题。当然,我会分享我的代码 此查询几乎立即返回:Sql 添加第二个连接条件会以指数方式增加查询时间,sql,postgresql,amazon-redshift,Sql,Postgresql,Amazon Redshift,所以,我使用的是红移(基于postgres)。不幸的是,我不能分享我的数据(原因显而易见),但这更像是一个概念性的问题。当然,我会分享我的代码 此查询几乎立即返回: select count(*) from table_one as c inner join table_two as z on regexp_replace(c.telephone_number, '[^0-9]', '') = regexp_replace(z.affected_phone_num
select
count(*)
from
table_one as c
inner join
table_two as z
on
regexp_replace(c.telephone_number, '[^0-9]', '') = regexp_replace(z.affected_phone_number, '[^0-9]', '');
但这一个将运行数小时:
select
count(*)
from
table_one as c
inner join
table_two as z
on
regexp_replace(c.telephone_number, '[^0-9]', '') = regexp_replace(z.affected_phone_number, '[^0-9]', '')
or c.email = z.requester_email;
为什么用或添加第二个连接条件会导致此问题
(我可以使用联盟解决这个问题,但我有兴趣在这里学习…)
运行解释如果有帮助
问题查询的查询计划:
QUERY PLAN
XN Aggregate (cost=159728183882.77..159728183882.77 rows=1 width=0)
-> XN Nested Loop DS_BCAST_INNER (cost=0.00..159726036322.85 rows=859023969 width=0)
Join Filter: ((regexp_replace(("inner".telephone_number)::text, '[^0-9]'::text, ''::text, 1) = regexp_replace(("outer".affected_phone_number)::text, '[^0-9]'::text, ''::text, 1)) OR (("inner".email)::text = ("outer".requester_email)::text))
-> XN Seq Scan on table_two z (cost=0.00..4447.40 rows=444740 width=36)
-> XN Seq Scan on table_one c (cost=0.00..3853.89 rows=385389 width=32)
----- Nested Loop Join in the query plan - review the join predicates to avoid Cartesian products -----
QUERY PLAN
XN Aggregate (cost=62358556140.01..62358556140.01 rows=1 width=0)
-> XN Hash Join DS_BCAST_INNER (cost=4817.36..62356413666.21 rows=856989520 width=0)
Hash Cond: (regexp_replace(("outer".affected_phone_number)::text, '[^0-9]'::text, ''::text, 1) = regexp_replace(("inner".telephone_number)::text, '[^0-9]'::text, ''::text, 1))
-> XN Seq Scan on table_two z (cost=0.00..4447.40 rows=444740 width=12)
-> XN Hash (cost=3853.89..3853.89 rows=385389 width=8)
-> XN Seq Scan on table_one c (cost=0.00..3853.89 rows=385389 width=8)
无问题查询的查询计划:
QUERY PLAN
XN Aggregate (cost=159728183882.77..159728183882.77 rows=1 width=0)
-> XN Nested Loop DS_BCAST_INNER (cost=0.00..159726036322.85 rows=859023969 width=0)
Join Filter: ((regexp_replace(("inner".telephone_number)::text, '[^0-9]'::text, ''::text, 1) = regexp_replace(("outer".affected_phone_number)::text, '[^0-9]'::text, ''::text, 1)) OR (("inner".email)::text = ("outer".requester_email)::text))
-> XN Seq Scan on table_two z (cost=0.00..4447.40 rows=444740 width=36)
-> XN Seq Scan on table_one c (cost=0.00..3853.89 rows=385389 width=32)
----- Nested Loop Join in the query plan - review the join predicates to avoid Cartesian products -----
QUERY PLAN
XN Aggregate (cost=62358556140.01..62358556140.01 rows=1 width=0)
-> XN Hash Join DS_BCAST_INNER (cost=4817.36..62356413666.21 rows=856989520 width=0)
Hash Cond: (regexp_replace(("outer".affected_phone_number)::text, '[^0-9]'::text, ''::text, 1) = regexp_replace(("inner".telephone_number)::text, '[^0-9]'::text, ''::text, 1))
-> XN Seq Scan on table_two z (cost=0.00..4447.40 rows=444740 width=12)
-> XN Hash (cost=3853.89..3853.89 rows=385389 width=8)
-> XN Seq Scan on table_one c (cost=0.00..3853.89 rows=385389 width=8)
我们只能猜测为什么它在没有访问数据库的情况下速度很慢
猜测不是性能优化的合适工具
使用该语句可以查看postgres实际如何处理这两条语句 您是否在表架构中使用sortkey
如果不是,或者如果不是在适当的字段中,数据将按插入顺序在节点中排序。这将导致你所说的循环
在指定表架构时,请确保包含最常用的sortkey
,记住可以有多个sortkey
:
CREATE TABLE schemaex.a1.account_revenue (
account_id varchar(30) NOT NULL,
date date NOT NULL distkey,
registration_date timestamp,
revenue float(8),
cost varchar(8),
)
compound sortkey(account_id, date);
当将sortkey中的字段分别用作连接键和连接条件时,这将大大减少连接和聚合的执行时间
您的“表2”有哪些索引。。在(请求者的电子邮件、受影响的电话号码)上有一个复合索引可能会有所帮助。您还应该在问题中添加表定义。作为MPP数据库,物理设计至关重要。DS\u BCAST\u INNER:将整个内部表的副本广播到所有计算节点
。所有网络流量加上嵌套循环联接。难怪它的速度是指数级的慢解释信息添加。。。显然,嵌套循环联接是个问题,但我正在寻找一个理论解释,解释为什么查询会导致嵌套循环联接,以及除了使用联合
之外,是否有其他方法可以修复它。。。