为什么mysql查询发送数据慢

时间:2013-09-26 10:51:42

标签: mysql performance

我的查询

      SELECT Info.InfoID,
      SessionInfo.SessionInfoID,
      SessionInfo.ANI,
      ANumber.ANumber,
      tmfInfo.PTime,
      tmfInfo.PTry,
      SessionInfo.CardID,
      tmfInfo.Status
 FROM tmfInfo,
      SessionInfo,
      ANumber ,
      ANumberLog,
      ANumberGroup,
      ANumberGroupLog
WHERE (tmfInfo.IVRSessionInfoID = SessionInfo.IVRSessionInfoID)
  AND (SessionInfo.ANumberLogID = ANumber.ANumberLogID)
  AND (ANumber.AccessNumberLogID = ANumberLog.ANumberLogID)
  AND (ANumberrLog.ANumberGroupID = ANumberGroup.ANumberGroupID)
  AND (ANumberGroup.ANumberGroupLogID = ANumberGroupLog.ANumberGroupLogID)
  AND (SessionInfo.SessionCallTime >= '2013-08-01 00:00:00'
  AND (SessionInfo.SessionCallTime <= '2013-08-01 23:59:59')
  AND (ANumberLog.IsDeleted = '0')
  AND (ANumberLog.IsActive = '1')
  AND (ANumberGroupLog.IsDeleted = '0')
  AND (ANumberGroupLog.IsActive = '1')
 ORDER BY SessionInfo.SessionCallTime,tmfInfo.PTime DESC;

结果给出1237行(10秒)

当iam执行explain cmd

  id    select_type table   type    possible_keys   key key_len ref rows    Extra
   1    SIMPLE  SessionInfo range   PRIMARY,SessionCallTime,ANumberLogID    SessionCallTime 4       57536   Using where; Using temporary; Using filesort
  1       SIMPLE    Anumber ref AnumberLogID    AnumberLogID    5               SessionInfo.ANumberLogID    1   Using where
  1     SIMPLE  AnumberLog  eq_ref  PRIMARY,ANumberGroupID,IsActive,IsDeleted   PRIMARY 4   SessionInfo.ANumberLogID    1   Using where
  1 SIMPLE  AnumberGroup    eq_ref  PRIMARY,ANumberGroupLogID   PRIMARY 4   AnumberLog.ANumberGroupID   1   
  1 SIMPLE  AnumberGroupLog eq_ref  PRIMARY,IsActive,IsDeleted  PRIMARY 4   AnumberGroup.ANumberGroupLogID  1   Using where
 1  SIMPLE  tmfInfo ref IVRSessionInfoID    IVRSessionInfoID    8   SessionInfo.IVRSessionInfoID    1   

这里查询serching超过57536行 这需要更多时间来执行

发送数据解析:__

                      Status    Duration
                     starting   0.000009
 Waiting for query cache lock   0.000003
  checking query cache for query    0.000087
          checking permissions  0.000003
         checking permissions   0.000002
         checking permissions   0.000002
         checking permissions   0.000002
         checking permissions   0.000002
         checking permissions   0.000003
                 Opening tables 0.000037
                 System lock    0.000007
 Waiting for query cache lock   0.000053
                        init    0.000039
                  optimizing    0.000018
                  statistics    0.000156
                          preparing 0.000033
           Creating tmp table   0.000029
                   executing    0.000002
          Copying to tmp table  0.353
              Sorting result    0.000238
                Sending data    7.763123
                          end   0.000009
            removing tmp table  0.000011
                           end  0.000004
                       query end    0.000004
                closing tables  0.000014
                freeing items   0.000018
            logging slow query  0.000003
          logging slow query    0.000025
                 cleaning up    0.000005

1 个答案:

答案 0 :(得分:1)

试试这个......

  1. 我将会话信息放在派生表子查询中,以尝试限制您在tmfInfo上加入的行数。

  2. 编辑:我正在将日期范围转换为UNIX_TIMESTAMP以提高时间戳字段的性能。这是一个很大的!!!!

  3. 以下是查询:

    SELECT 
        ssnInfo.*
        tmfInfo.PTime,
        tmfInfo.PTry,
        tmfInfo.Status
    
    FROM (
        SELECT
            Info.InfoID,
            SessionInfo.SessionInfoID,
            SessionInfo.ANI,
            ANumber.ANumber,
            SessionInfo.CardID
        FROM
            SessionInfo
            INNER JOIN ANumber AS
                ON ANumber.ANumberLogID = SessionInfo.ANumberLogID,
            INNER JOIN ANumberLog
                ON ANumberLog.ANumberLogID = ANumber.AccessNumberLogID
                AND ANumberLog.IsDeleted = 0 /* Do not put this in quotes unless it's stored as a string */
                AND ANumberLog.IsActive = 1 /* Do not put this in quotes unless it's stored as a string */
            INNER JOIN ANumberGroup
                ON ANumberGroup.ANumberGroupID = ANumberLog.ANumberGroupID
            INNER JOIN ANumberGroupLog
                ON ANumberGroupLog.ANumberGroupLogID = ANumberGroup.ANumberGroupLogID
                AND ANumberGroupLog.IsDeleted = 0 /* Do not put this in quotes unless it's stored as a string */
                AND ANumberGroupLog.IsActive = 1 /* Do not put this in quotes unless it's stored as a string */
        WHERE 
            SessionInfo.SessionCallTime 
                BETWEEN UNIX_TIMESTAMP('2013-08-01 00:00:00') 
                AND UNIX_TIMESTAMP('2013-08-15 23:59:59')
        ) AS ssnInfo
    INNER JOIN tmfInfo
        ON ssnInfo.IVRSessionInfoID = tmfInfo.IVRSessionInfoID