为什么STRAIGHT_JOIN消耗更多的CPU?

时间:2015-10-27 12:55:11

标签: mysql database-performance query-performance

为什么STRAIGHT_JOIN比常规连接消耗更多的CPU?你有什么主意吗?

当我在我的一个查询中使用straight_join时,它会将查询速度从12秒加速到3秒。但它耗费了这么多CPU?可能是关于服务器配置还是别的什么?

您可能需要在此评论/ 主题ID正常后检查代码,获取设备... /
在此行之前,有一些关于将topic_ids填充到临时表的代码。

以下是查询:

CREATE PROCEDURE `DevicesByTopic`(IN platform TINYINT, IN application TINYINT, IN topicList TEXT, IN page_no MEDIUMINT UNSIGNED)
BEGIN

    DECLARE m_index INT DEFAULT 0;
    DECLARE m_topic VARCHAR(255);
    DECLARE m_topic_id BIGINT UNSIGNED DEFAULT NULL;
    DECLARE m_session_id VARCHAR(40) CHARSET utf8 COLLATE utf8_turkish_ci;

    -- Session Id 
    SET m_session_id = replace(uuid(), '-', '');    

    -- Temp table
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_topics(
        topic_slug VARCHAR(100) COLLATE utf8_turkish_ci
        ,topic_id BIGINT UNSIGNED
        ,session_id VARCHAR(40) COLLATE utf8_turkish_ci
        ,INDEX idx_tmp_topic_session_id (session_id)
        ,INDEX idx_tmp_topic_id (topic_id)
    ) CHARSET=utf8 COLLATE=utf8_turkish_ci; 

    -- Filling topics in a loop
    loop_topics: LOOP
        SET m_index = m_index + 1;      

        SET m_topic_id = NULL;      
        SET m_topic= SPLIT_STR(topicList,',', m_index);                     

        IF m_topic = '' THEN
            LEAVE loop_topics;      
        END IF;         

        SELECT t.topic_id INTO m_topic_id FROM topic AS t WHERE t.application = application AND (t.slug_hashed = UNHEX(MD5(m_topic)) AND t.slug = m_topic) LIMIT 1;

        -- Fill temp table
        IF m_topic_id IS NOT NULL AND m_topic_id > 0 THEN           
            INSERT INTO tmp_topics 
                (topic_slug, topic_id, session_id)           
            VALUES
                (m_topic, m_topic_id, m_session_id);            
        END IF;

    END LOOP loop_topics;       

    /* Topic Ids are OK, Getting Devices... */

  SELECT 
    dr.device_id, dr.platform, dr.application, dr.unique_device_id, dr.amazon_arn   
  FROM 
    device AS dr
  INNER JOIN (
            SELECT STRAIGHT_JOIN      
            DISTINCT
                d.device_id
            FROM
                device AS d
            INNER JOIN 
                device_user AS du ON du.device_id = d.device_id             
            INNER JOIN 
                topic_device_user AS tdu ON tdu.device_user_id = du.device_user_id
            INNER JOIN
                tmp_topics AS tmp_t ON tmp_t.topic_id = tdu.topic_id
            WHERE
                ((platform IS NULL OR d.platform = platform) AND d.application = application)
                AND d.page_no = page_no     
        AND d.status = 1
                AND du.status = 1               
                AND tmp_t.session_id = m_session_id COLLATE utf8_turkish_ci 
  ) dFiltered ON dFiltered.device_id = dr.device_id
  WHERE
        ((platform IS NULL OR dr.platform = platform) AND dr.application = application)
        AND dr.page_no = page_no        
        AND dr.status = 1;

    -- Delete rows fFill temp table
    DELETE FROM tmp_topics WHERE session_id = m_session_id;

END;

使用STRAIGHT_JOIN这个查询大约需要3秒钟,但是耗费了90%的CPU,但是如果我删除关键字“STRAIGHT_JOIN”,则需要12秒但消耗12%的CPU。

MySQL 5.6.19a - innodb

可能是什么原因?

最好的问候。

1 个答案:

答案 0 :(得分:3)

当您需要覆盖MySQL的优化程序时,使用STRAIGHT_JOIN。您告诉它忽略它自己的优化执行路径,而是依赖于您在查询中编写它们的顺序读取表。

99%的时间你不想使用straight_join。只需依靠MySQL来完成它的工作并为您优化执行路径。毕竟,任何值得它的RDBMS在优化方面都会相当不错。

当您已经针对给定查询测试了MySQL的优化并发现它缺乏时,您应该使用straight_join几次。对于使用此查询的情况,显然使用straight_join进行手动优化并不比MySQL在优化中更好。