0

请在优化此查询时遇到困难。我试图实现的是连接大约 8 个表,其中只有大约 3 个表包含大数据(1.5m 记录)。此查询返回预期的记录,但需要 1 分钟才能运行,这很糟糕。我知道可以对其进行优化以更好地执行,我需要各位专家的帮助。我已经对用于连接的字段进行了索引。

SELECT topic_id, 
   topic_title, 
   unit_name_abbrev, 
   sch_name_abbrev, 
   picture_small_url    AS thumbnail, 
   profile_pix_upload_path, 
   first_name, 
   last_name, 
   topic_poster, 
   topic_replies, 
   topic_views, 
   topic_last_post_time AS topic_post_time, 
   sch_sub_forum_id 
FROM   (_sch_forum_topics 
    INNER JOIN _users 
            ON ( _users.userid = _sch_forum_topics.topic_poster ) 
    INNER JOIN _profile 
            ON _profile.userid = _users.userid 
    INNER JOIN _class 
            ON _users.classid = _class.classid 
    INNER JOIN _level 
            ON _class.level_id = _level.id 
    INNER JOIN _unit 
            ON _class.unitid = _unit.unitid 
    INNER JOIN _department 
            ON _unit.deptid = _department.deptid 
    INNER JOIN _faculty 
            ON _department.facid = _faculty.facid 
    INNER JOIN _university 
            ON _faculty.schid = _university.schid) 
WHERE  _sch_forum_topics.sch_sub_forum_id = 4 
ORDER  BY _sch_forum_topics.topic_last_post_time DESC 
LIMIT  0, 15 
4

1 回答 1

0

在进行 JOIN 之前尝试过滤。

SELECT topic_id, 
   topic_title, 
   unit_name_abbrev, 
   sch_name_abbrev, 
   picture_small_url    AS thumbnail, 
   profile_pix_upload_path, 
   first_name, 
   last_name, 
   topic_poster, 
   topic_replies, 
   topic_views, 
   topic_last_post_time AS topic_post_time, 
   sch_sub_forum_id
FROM
 ( select  * FROM  sch_forum_topics WHERE sch_sub_forum_id = 4
ORDER BY topic_last_post_time DESC 
LIMIT  0, 15 ) main
    INNER JOIN _users 
            ON ( _users.userid = main.topic_poster ) 
    INNER JOIN _profile 
            ON _profile.userid = _users.userid 
    INNER JOIN _class 
            ON _users.classid = _class.classid 
    INNER JOIN _level 
            ON _class.level_id = _level.id 
    INNER JOIN _unit 
            ON _class.unitid = _unit.unitid 
    INNER JOIN _department 
            ON _unit.deptid = _department.deptid 
    INNER JOIN _faculty 
            ON _department.facid = _faculty.facid 
    INNER JOIN _university 
            ON _faculty.schid = _university.schid);
于 2013-10-14T12:08:25.160 回答