我有以下查询,根据排行榜的位置选择玩家,并提供最新的帖子和一些最新的故事涉及该玩家,这是group_concatted所以我每个玩家有一行,每行有多个故事。
SELECT players.player_name,
posts. * ,
GROUP_CONCAT( DISTINCT stories.story_title
ORDER BY stories.story_published DESC
SEPARATOR '[sep]' )
AS headlines,
GROUP_CONCAT( DISTINCT stories.story_link
ORDER BY stories.story_published DESC
SEPARATOR '[sep]' )
AS links
FROM players, player_stories, stories, posts, leaderboard
WHERE cs_player_id = player_id
AND cs_story_id = story_id
AND post_player = player_id
AND leaderboard.name = players.player_name
GROUP BY players.player_name
ORDER BY leaderboard.1 DESC , leaderboard.3 DESC , leaderboard.6 DESC , leaderboard.144 DESC
我在所有_id字段上都有主键,并且我在连接中使用的所有其他字段上都有索引。
但它的加载速度很慢 - 每次查询大约需要3秒钟。
所有字段都是固定长度(VARCHAR或INT)
有没有办法优化此查询?
编辑:这是在SQL小提琴:http://sqlfiddle.com/#!2/b55cf/1/0
值得注意的是,在我的实际实现中,每个表都有1000到3000行。
EXPLAIN EXTENDED提供:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE player_stories index PRIMARY PRIMARY 8 NULL 1169 Using index; Using temporary; Using filesort
1 SIMPLE players eq_ref PRIMARY PRIMARY 4 player_stories.cs_player_id 1
1 SIMPLE leaderboard ref Bothnames,name name 152 func 1 Using where
1 SIMPLE posts ref uniqueLink,post_playeruniqueLink 4 players.player_id 24 Using where
1 SIMPLE stories eq_ref PRIMARY PRIMARY 4 player_stories.cs_story_id 1
Variable_name Value
bulk_insert_buffer_size 8388608
innodb_buffer_pool_awe_mem_mb 0
innodb_buffer_pool_size 8388608
innodb_log_buffer_size 1048576
join_buffer_size 131072
key_buffer_size 8384512
myisam_sort_buffer_size 8388608
net_buffer_length 16384
preload_buffer_size 32768
read_buffer_size 131072
read_rnd_buffer_size 262144
sort_buffer_size 2097144