繁体   English   中英

如何改进/加速 SQL 查询

[英]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
  • locationpurchase_datecost_id上创建索引
  • 如果可能,避免like '%...'
  • 避免or - 替换为union
  • 如果 Informix 支持,则用窗口函数替换子选择(使用row_number() over (partition by location, cost_id order by cost_num desc)等于 1 提取行)
  • 子选择中的别名表和列,至少为了可读性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM