Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询运行缓慢瓶颈是什么_Mysql_Sql_Stored Procedures - Fatal编程技术网

MySQL查询运行缓慢瓶颈是什么

MySQL查询运行缓慢瓶颈是什么,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我有一个运行了22秒的MySQL查询,我为所有select和where条件添加了索引。“使用率”表有大约79000条记录。标签表有602173条记录。我试图通过添加索引来优化它。是join语句使它变慢了。或者我应该实现的任何索引来提高存储过程的速度。但它仍然运行缓慢。我可以知道瓶颈在哪里以及如何改进它吗 CREATE PROCEDURE `SelectUsage2`( IN p_ids MEDIUMTEXT , IN p_locationIDs MEDI

我有一个运行了22秒的MySQL查询,我为所有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

很抱歉,您是为优化查询XD而付费的吗?旁白,我个人不能帮助太多,但你需要考虑的是:1)太多的关键字可能是一件坏事,2)了解什么样的查询试图实现,它是最短的吗?3) 偷看一下查询计划如果是我,我会重新开始。考虑到这一点,请参阅@草莓谢谢,我将编辑这个问题。查询计划将有助于决定从何处开始,但我会仔细考虑case语句中嵌套的查询——有没有更有效的方法来获取这些信息?