给 magapp 数据库及使用 magapp 的 Discuz! 数据库加索引
本文发布于 6 年前,部分内容可能已经失去参考价值。
Discuz! 数据库加索引
待优化的 SQL:(pre_forum_thread 表有 150 万条数据)
SELECT * FROM pre_forum_thread WHERE `fid`='62' AND `displayorder` IN('0','1','2','3','4') ORDER BY displayorder DESC, dateline DESC LIMIT 20, 20
加索引前,
EXPLAIN 结果 Extra 为:Using index condition; Using where; Using filesort
> 时间: 0.915s
加索引后:`fid`, `displayorder`, `dateline`
EXPLAIN 结果 Extra 为:Using where 或 Using index condition
> 时间: 0.001s
magapp 数据库加索引
待优化的 SQL:(mag_score_action_log 表有 200 万条数据)
SELECT COUNT(*) AS tp_count
FROM `mag_score_action_log`
WHERE action_id = 20
AND user_id = 650070
AND create_time >= 1534953600
AND create_time < 1535040000
LIMIT 1
加索引前,
EXPLAIN 结果 Extra 为:?????
> 时间: 70s
加索引后:`action_id`, `user_id`, `create_time`
EXPLAIN 结果 Extra 为:Using where; Using index
> 时间: 0.073s
待优化的 SQL:(mag_score_mission_log 表有约 55 万条数据)
SELECT COUNT(*) AS tp_count
FROM `mag_score_mission_log`
WHERE mission_id = 7
AND user_id = 650070
AND create_time >= 1534953600
AND create_time < 1535040000
LIMIT 1
加索引前,
EXPLAIN 结果 rows 为:549178
> 时间: 17.719s
加索引后:`mission_id`, `user_id`, `create_time`
EXPLAIN 结果 rows 为:1
> 时间: 0.025s
待优化的 SQL:(mag_score_mission_user 表有约 28 万条数据)
SELECT *
FROM `mag_score_mission_user`
WHERE `user_id` = 431779
AND `mission_id` = 5
AND `create_time` >= 1534953600
ORDER BY complete_count DESC
LIMIT 1
不加索引
1SIMPLEmag_score_mission_userALL282436Using where; Using filesort
> 时间: 7.325s
`user_id`, `mission_id`
1SIMPLEmag_score_mission_userrefix_us_miix_us_mi10const,const7Using where; Using filesort
时间: 0.014s
`user_id`, `mission_id`, `create_time`
1SIMPLEmag_score_mission_userrangeix_us_miix_us_mi151Using index condition; Using filesort
时间: 0.023s
`user_id`, `mission_id`, `complete_count`
1SIMPLEmag_score_mission_userrefix_us_miix_us_mi10const,const7Using where
> 时间: 0.014s
`user_id`, `mission_id`, `complete_count`, `create_time`
1SIMPLEmag_score_mission_userrefix_us_miix_us_mi10const,const7Using where
> 时间: 0.028s
`user_id`, `mission_id`, `create_time`, `complete_count`
1SIMPLEmag_score_mission_userrangeix_us_miix_us_mi151Using index condition; Using filesort
> 时间: 0.025s
其它就不一一举例了,根据 SHOW FULL PROCESSLIST 的慢查询自行加索引就行了。