我有一个MySQL查询,运行了22秒,我为所有select和where条件添加了索引。使用表有大约79000条记录。标签表具有602173条记录。我试图通过添加索引来优化它。是join语句使它变慢了。或者我应该实施任何索引来提高存储过程的速度。但是它仍然运行缓慢。我可以知道瓶颈在哪里以及如何改善它吗?
CREATE PROCEDURE `SelectUsage2`(
IN p_ids MEDIUMTEXT
, IN p_locationIDs MEDIUMTEXT
, IN p_indicatorIDs MEDIUMTEXT
)
BEGIN
select
, case when (select count(lbl.id) from `labels` as lbl where lbl.ObjectID = i.id and lbl.ObjectName = 'indicators' and lbl.ColumnName = 'IndicatorName' and lbl.LanguageCode = p_language) > 0 then
(select content from `labels` as lbl where lbl.ObjectID = i.id and lbl.ObjectName = 'indicators' and lbl.ColumnName = 'IndicatorName' and lbl.LanguageCode = p_language limit 1)
else
i.IndicatorName
end as IndicatorName
u.*
from
`usage` as u
left join `locations` as l on u.LocationID = l.id
left join `Indicators` as i on u.IndicatorID = i.id
left join `Indicators` as i2 on i.ParentID = i2.id
where
and (p_ids is null or FIND_IN_SET(u.id, p_ids))
and (p_locationIDs is null or FIND_IN_SET(u.LocationID, p_locationIDs))
and (p_indicatorIDs is null or FIND_IN_SET(u.IndicatorID, p_indicatorIDs))
order by
i.IndicatorName, l.LocationName
;
END