1

我有这个查询

  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表明查询只是坐在那里sending data

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

如何改进此查询以使其运行更快并返回相同的结果。也许获取下一行和过去行的方法是次优的,我需要寻找一种不同的方式来返回那些下一行和上一行 ID?

编辑 - 附加背景信息

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

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

这些会影响加载时间吗?

4

3 回答 3

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 |
 +---+----------+
于 2013-08-07T10:48:31.157 回答
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

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

于 2013-08-07T12:57:31.363 回答
1

为了扩展草莓的答案,为“预查询”进行额外的左连接以获取所有先前/下一个 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 的那些,然后您可以根据需要加入下一个/前一个。我没有将连接添加到课程和圆桌会议,因为没有显示结果列,但可以轻松添加。

于 2013-08-07T16:37:51.653 回答