小编fab*_*tag的帖子

如何优化结合了 INNER JOIN、DISTINCT 和 WHERE 的 SQL 查询?

SELECT DISTINCT options.id, options.foo_option_id, options.description
FROM vehicles 
INNER JOIN vehicle_options     ON vehicle_options.vehicle_id = vehicles.id 
INNER JOIN options             ON options.id = vehicle_options.option_id 
INNER JOIN discounted_vehicles ON vehicles.id = discounted_vehicles.vehicle_id 
WHERE discounted_vehicles.discount_id = 4;
Run Code Online (Sandbox Code Playgroud)

上面的查询返回 2067 行,它在 1.7 秒内在本地运行。我想知道它是否尽可能快,或者我是否可以以某种方式进一步调整它,因为这个数据集会随着时间的推移而快速增长。

我在速度不变的情况下尝试过的事情:

1 - 更改连接顺序,从最小的表连接到最大的表。

2 - 向discounted_vehicles.discount_id 添加索引。

sql postgresql postgresql-performance

0
推荐指数
1
解决办法
2217
查看次数

标签 统计

postgresql ×1

postgresql-performance ×1

sql ×1