在WITH子句中添加无关表是否会减慢PostgreSQL中的查询速度?

在WITH子句中添加无关表是否会减慢PostgreSQL中的查询速度?,第1张

在WITH子句中添加无关表是否会减慢PostgreSQL中的查询速度?

您可以使用Explain来显示查询优化器将如何处理您的查询。

http://www.postgresql.org/docs/9.2/static/sql-
explain.html

在上述情况下,PSQL应该看到未使用temp3,并且不包括它。

在一个我的数据库上使用上面的示例。

explain with temp1 as (select * from cidrs), temp2 as (select * from contacts), temp3 as ( select * from accounts )  select * from temp1 join temp2 on temp1.id = temp2.id;       QUERY PLAN--------------------------------------------------------------------- Hash Join  (cost=22.15..25.44 rows=20 width=4174)   Hash Cond: (temp1.id = temp2.id)   CTE temp1     ->  Seq Scan on cidrs  (cost=0.00..11.30 rows=130 width=588)   CTE temp2     ->  Seq Scan on contacts  (cost=0.00..10.20 rows=20 width=3586)   ->  CTE Scan on temp1  (cost=0.00..2.60 rows=130 width=588)   ->  Hash  (cost=0.40..0.40 rows=20 width=3586)         ->  CTE Scan on temp2  (cost=0.00..0.40 rows=20 width=3586)(9 rows)

您会发现没有提及temp3。在回答您的编辑为何不影响查询时间的问题时,优化器足够聪明,可以看到它没有被使用,也不会理会它。因此,它是优化程序的原因。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4990232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存