改进此MySQL查询 - 选择作为子查询

时间:2013-08-07 09:28:38

标签: mysql subquery query-optimization

我有这个查询

  SELECT  
   shot.hole AS hole,
   shot.id AS id,
   (SELECT s.id FROM shot AS s 
      WHERE s.hole = shot.hole AND s.shot_number > shot.shot_number AND shot.round_id = s.round_id 
       ORDER BY s.shot_number ASC LIMIT 1) AS next_shot_id,
   shot.distance AS distance_remaining,
   shot.type AS hit_type,
   shot.area AS onto
  FROM shot 
  JOIN course ON shot.course_id = course.id
  JOIN round ON shot.round_id = round.id
  WHERE round.uID = 78

这会在大约0.7秒内返回900行。这是好的,但还有更多这样的行需要

(SELECT s.id FROM shot AS s 
 WHERE s.hole = shot.hole AND s.shot_number > shot.shot_number AND shot.round_id = s.round_id 
 ORDER BY s.shot_number ASC LIMIT 1) AS next_shot_id,

例如

   (SELECT s.id FROM shot AS s 
    WHERE s.hole = shot.hole AND s.shot_number < shot.shot_number AND shot.round_id = s.round_id 
    ORDER BY s.shot_number ASC LIMIT 1) AS past_shot_id,

添加它会将加载时间增加到10秒,这太长了,页面通常根本不加载,或者MySQL只是锁定,使用show processlist表示查询只是坐在那里{{ 1}}。

删除这些子查询中的sending data子句可将查询时间缩短至0.05秒,这要好得多。但是ORDER BY s.shot_number ASC需要确保返回下一行或过去的行(镜头),而不是任何旧的随机行。

如何改进此查询以使其运行更快并返回相同的结果。用于获取下一行和过去行的my approach可能是次优的,我需要查看返回下一行和上一行ID的不同方法吗?

编辑 - 其他背景信息

查询在我的测试域(子域)上很好。但是当移动到实时域时,问题就开始了。由于这些新的慢查询,整个网站几乎没有任何改变。主要说明:

  • 不同的域名
  • / var / www
  • 中的不同文件夹
  • 相同的数据库
  • 相同的数据库凭据
  • 相同代码
  • 添加索引以尝试修复 - 这没有帮助

这些都会影响加载时间吗?

3 个答案:

答案 0 :(得分:2)

这将在一分钟内被标记为“不是答案”,但它说明了一种可能的解决方案,而不是简单地将它交给你一个盘子......

 SELECT * FROM ints;
 +---+
 | i |
 +---+
 | 0 |
 | 1 |
 | 2 |
 | 3 |
 | 4 |
 | 5 |
 | 6 |
 | 7 |
 | 8 |
 | 9 |
 +---+

 SELECT x.i, MIN(y.i) FROM ints x LEFT JOIN ints y ON y.i > x.i GROUP BY x.i;
 +---+----------+
 | i | MIN(y.i) |
 +---+----------+
 | 0 |        1 |
 | 1 |        2 |
 | 2 |        3 |
 | 3 |        4 |
 | 4 |        5 |
 | 5 |        6 |
 | 6 |        7 |
 | 7 |        8 |
 | 8 |        9 |
 | 9 |     NULL |
 +---+----------+

答案 1 :(得分:1)

我想知道以下表现如何。它用字符串操作替换了连接操作。

  SELECT shot.hole AS hole, shot.id AS id,
         substring_index(substring_index(shots, ',', find_in_set(shot.id, ss.shots) + 1), ',', -1
                        ) as nextsi,
         substring_index(substring_index(shots, ',', find_in_set(shot.id, ss.shots) - 1), ',', -1
                        ) as prevsi,
         shot.distance AS distance_remaining, shot.type AS hit_type, shot.area AS onto
  FROM shot JOIN
       course
       ON shot.course_id = course.id JOIN
       round
       ON shot.round_id = round.id join
       (select s.round_id, s.hole, group_concat(s.id order by s.shot_number) as shots
        from shot s
        group by s.round_id, s.hole
       ) ss
       on ss.round_id = shot.round_id and ss.hole = shot.hole
  WHERE round.uID = 78

请注意,这不能完全发挥作用 - 它会在第一次和最后一次拍摄时产生错误的结果。在修复这些细节之前,我想知道性能如何。

答案 2 :(得分:1)

要扩展Strawberry的答案,为“预查询”执行额外的左连接以获取所有先前/下一个ID,然后加入以获取您需要的任何详细信息。

select
      Shot.ID,
      Shot.Hole,
      Shot.Distance as Distance_Remaining,
      Shot.Type as Hit_Type,
      Shot.Area as Onto
      PriorShot.Hole as PriorHole,
      PriorShot.Distance as PriorDistanceRemain,
      NextShot.Hole as NextHole,
      NextShot.Distance as NextDistanceRemain
   from
      ( SELECT 
              shot.id, 
              MIN(nextshot.id) as NextShotID,
              MAX(priorshot.id) as PriorShotID
           FROM 
              round 
                 JOIN shot 
                    on round.id = shot.round_id
                    LEFT JOIN shot nextshot
                       ON shot.round_id = nextshot.round_id
                       AND shot.hole = nextshot.hole
                       AND shot.shot_number < nextshot.shot_number
                    LEFT JOIN shot priorshot
                       ON shot.round_id = priorshot.round_id
                       AND shot.hole = priorshot.hole
                       AND shot.shot_number > priorshot.shot_number
           WHERE
              round.uID = 78
           GROUP BY 
              shot.id ) AllShots
         JOIN Shot
            on AllShots.id = Shot.ID
            LEFT JOIN shot PriorShot
               on AllShots.PriorShotID = PriorShot.ID
            LEFT JOIN shot NextShot
               on AllShots.NextShotID = NextShot.ID

内部查询仅获取round.uID = 78的内部查询,然后您可以根据需要加入下一个/前一个。我没有添加连接到课程和圆桌会议,因为没有显示结果列,但可以很容易地添加。