1

我有以下查询,它按照他们的排行榜位置顺序选择玩家,并在旁边给出他们的最新帖子和一些涉及该玩家的最新故事,这些故事是 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 Fiddle 上: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
4

0 回答 0