[英]How to improve/speed up SQL query
我有一个有效的查询,但需要很长时间才能运行。 有没有更有效的方法来实现这一目标?
本质上,我想获取位置为 106 的所有行,purchase_date >= today - 300,assigned_worker 包括 Alex 或 Carol。 这将生成具有相同 cost_id 的行。 我只希望每个 cost_id 有一行,并希望该行是 cost_num 最高的那一行。
select distinct t.cost_id, t.column_a, t.column_b
from mytable t
where t.location in (106) and t.purchase_date >= today - 300 and (t.assigned_worker like '%Alex%' or d.assigned_worker like '%Carol%' )
and t.cost_num in (select max(cost_num) from mytable where cost_id = t.cost_id
and location in (106));
如果不了解索引和解释计划(以及 - 在我的个人情况下 - Informix),很难找到缓慢的根本原因。 很少(注意 - 可能会产生误导!)想法:
t.location = 106
替换t.location in (106)
的t.location = 106
location
或purchase_date
或cost_id
上创建索引like '%...'
or
- 替换为union
row_number() over (partition by location, cost_id order by cost_num desc)
等于 1 提取行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.