【发布时间】:2021-02-04 14:08:13
【问题描述】:
以下是查询在 where 条件下有 3 个过滤器,哪个过滤器首先应用于表?过滤器是否按照查询中从右到左的顺序应用于表。
我希望先过滤掉 product_key,然后过滤掉剩余的过滤器。
select *
from order_history
where product_key in (select product_key from products
where status = 'avilable' )
and province = 'Texas'
and category_key in (3,4,5)
【问题讨论】:
-
优化器决定顺序。
-
我们不能强制执行,还是我们可以选择先使用 product_key,因为我想在过滤后的 product_key 中使用 category_key 和省
-
我相信下面是一种方法,我们可以先过滤product_key
select * from order_history h inner join (select product_key from products where status = 'avilable' ) p on h.product_key =p.product_key where province = 'Texas' and category_key in (3,4,5) -
相信优化器,不要试图愚弄它。编写简单的 SQL 以便于优化。
-
确保统计数据是最新的。
标签: sql postgresql query-optimization