【问题标题】:In Postgres, is filtering two tables before a join performantly equal to joining two tables, then filtering?在 Postgres 中,在连接之前过滤两个表是否等于连接两个表,然后过滤?
【发布时间】:2022-11-14 08:20:21
【问题描述】:

我试图了解我在 postgres 中编写查询的方式是否不能大规模执行(因为我如何使用视图来组织 DRY 代码)。

我认为这归结为在加入表格之前过滤表格是否等同于加入表格,然后进行过滤。

这是一个示例:有人可以告诉我选项 1 和选项 2 的性能是否相同吗?

选项1

with filteredTable1 as 
    (select *
    from table1
    where table1.id = 1),
   filteredtTable2 as
   (select *
    from table2
    where table2.id = 1) 
select * 
from filteredTable1
inner join filteredTable2 filteredTable1.id = filteredTable2.id

选项 2

with joinedTables as
    (select *
     from table1
     inner join table2 on table1.id = table2.id)
select *
from joinedTables
where id1 = 1

谢谢!

【问题讨论】:

    标签: postgresql performance


    【解决方案1】:

    第一个选项由 3 个选择和 1 个连接组成,另一方面,第二个选项 - 2 个选择和 1 个连接。我想这就是答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多