栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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。在回答您的编辑为何不影响查询时间的问题时,优化器足够聪明,可以看到它没有被使用,也不会理会它。因此,它是优化程序的原因。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号