小编Mat*_*ong的帖子

Same execution path, data and schematics; different query times

We have noticed some inconsistencies in our MySQL performance for query times that we feel cannot be explained by just server load. Some queries seem to be much efficient than others, despite having a similar setup.

Edit: Since opening this question, our database crashed (for unknown reasons at this moment, RDS teams are investigating), and after a reboot the issue is no longer reproducible and the queries are the same speed. I still would like to know what was wrong …

mysql query-optimization

9
推荐指数
1
解决办法
265
查看次数

优化慢速MySQL选择查询

编辑:在看了这里的一些答案和研究时间之后,我的团队得出结论,很可能没有办法优化这个比我们能够实现的4.5秒(除非可能在offers_clicks上进行分区,但是会有一些丑陋的副作用).最后,经过大量的头脑风暴,我们决定拆分两个查询,创建两组用户ID(一个来自users表,一个来自offers_clicks),并将它们与Python中的set进行比较.来自users表的id组仍然是从SQL中提取的,但我们决定将offers_clicks移动到Lucene并在其上添加了一些缓存,这样就可以从中获取另一组ID了.最终的结果是缓存下降到大约半秒,没有缓存时下降到0.9秒.

原始帖子开始:我无法优化查询.第一个版本的查询很好,但是在第二个查询中加入了offers_clicks,查询变得相当慢.Users表包含1000万行,offers_clicks包含5300万行.

可接受的表现:

SELECT count(distinct(users.id)) AS count_1
FROM users USE index (country_2)
WHERE users.country = 'US'
  AND users.last_active > '2015-02-26';
1 row in set (0.35 sec)
Run Code Online (Sandbox Code Playgroud)

坏:

SELECT count(distinct(users.id)) AS count_1
FROM offers_clicks USE index (user_id_3), users USE index (country_2)
WHERE users.country = 'US'
  AND users.last_active > '2015-02-26'
  AND offers_clicks.user_id = users.id
  AND offers_clicks.date > '2015-02-14'
  AND offers_clicks.ranking_score < 3.49
  AND offers_clicks.ranking_score > 0.24;
1 row in set (7.39 sec)
Run Code Online (Sandbox Code Playgroud)

以下是它的外观而不指定任何索引(甚至更糟):

SELECT count(distinct(users.id)) AS count_1
FROM offers_clicks, users
WHERE …
Run Code Online (Sandbox Code Playgroud)

mysql sql query-optimization

6
推荐指数
1
解决办法
409
查看次数

标签 统计

mysql ×2

query-optimization ×2

sql ×1