我用 memcached 缓存了慢查询,它工作得很好,但有时我们在 mysql 中遇到很多慢查询,好像 memcached 已经停止,然后我们的网站就关闭了。
慢查询图:http: //i.imgur.com/ReyWe.png
此时我们在 30 秒内收到了大约 100 个慢查询。会是什么?我们的查询:
# Query_time: 5.942602 Lock_time: 0.010214 Rows_sent: 10000 Rows_examined: 493139
SET timestamp=1335194149;
SELECT story_id FROM dug_stories d WHERE d.story_is_permanent=0 AND
(
(d.story_time>1335190543 AND (d.story_pluses-d.story_minuses) > 5)
OR
( ( (d.story_pluses-d.story_minuses) > 12 OR (d.story_pluses >= 10 AND d.story_minuses<=0) ) AND d.story_cat!=48 AND d.st
ory_cat!=131 AND d.story_cat!=55 AND d.story_cat!=44 AND d.story_cat!=126 AND d.story_cat!=53 AND d.story_cat!=370 AND d.story_cat!=381 AND d.stor
y_cat!=304 AND d.story_cat!=497) OR (d.story_cat=48 AND (d.story_pluses-d.story_minuses) > 9) OR (d.story_cat=131 AND (d.story_pluses-d.story_min
uses) > 8) OR (d.story_cat=55 AND (d.story_pluses-d.story_minuses) > 8) OR (d.story_cat=44 AND (d.story_pluses-d.story_minuses) > 9) OR (d.story_c
at=126 AND (d.story_pluses-d.story_minuses) > 13) OR (d.story_cat=53 AND (d.story_pluses-d.story_minuses) > 8) OR (d.story_cat=370 AND (d.story_pl
uses-d.story_minuses) > 8) OR (d.story_cat=381 AND (d.story_pluses-d.story_minuses) > 8) OR (d.story_cat=304 AND (d.story_pluses-d.story_minuses)
> 8) OR (d.story_cat=497 AND (d.story_pluses-d.story_minuses) > 9)
)
ORDER BY d.story_rating DESC, d.story_time DESC LIMIT 0, 10000;
稍微调整了查询(而不是所有 != 类别,我更改为 NOT Category IN (要排除的类别列表),但格式化以便于阅读...另外,请注意,查询中甚至没有使用 SET TIMESTAMP 变量...这是一个硬编码的值。
SET timestamp=1335194149;
SELECT
story_id
FROM
dug_stories d
WHERE
d.story_is_permanent = 0
AND ( ( d.story_time > 1335190543 AND d.story_pluses - d.story_minuses > 5 )
OR ( ( d.story_pluses - d.story_minuses > 12
OR ( d.story_pluses >= 10 AND d.story_minuses <= 0 )
)
AND NOT d.story_cat IN ( 44, 48, 53, 55, 126, 131, 304, 370, 381, 497 )
)
OR ( d.story_cat = 44 AND d.story_pluses - d.story_minuses > 9 )
OR ( d.story_cat = 48 AND d.story_pluses - d.story_minuses > 9 )
OR ( d.story_cat = 53 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 55 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 126 AND d.story_pluses - d.story_minuses > 13 )
OR ( d.story_cat = 131 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 304 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 370 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 381 AND d.story_pluses - d.story_minuses > 8 )
OR ( d.story_cat = 497 AND d.story_pluses - d.story_minuses > 9 )
)
ORDER BY
d.story_rating DESC,
d.story_time DESC
LIMIT
0, 10000;
非常感谢您的帮助!对不起我糟糕的英语:)