mysql联合优化

时间:2013-03-20 11:59:20

标签: mysql performance union

QUERY 1

(
SELECT 
    a.image_name, a.member_id, b.username, c.image_alt , c.img_id, 
a.total_views, c.island_id, c.personal, c.date_upload, COUNT(d.img_id) AS numComm  
FROM 
    members_stats_images a, members b, members_images c, members_images_comments d  
WHERE 
    c.island_id='$island_id'  AND 
    c.personal='N' AND 
    c.approved='Y' AND
    c.is_other<>'Y' AND   
    c.member_id=b.member_id AND 
    a.member_id=b.member_id AND 
    a.image_name=c.image_name AND 
    d.img_id=c.img_id 
GROUP BY 
    d.img_id 
)
UNION 
(
SELECT 
    a.image_name, a.member_id, b.username, c.image_alt , c.img_id, 
a.total_views, c.island_id, c.personal, c.date_upload, 0 AS numComm  
FROM 
    members_stats_images a, members b, members_images c 
WHERE 
    c.island_id='$island_id' AND 
    c.personal='N' AND 
    c.approved='Y' AND 
    c.is_other<>'Y' AND 
    c.member_id=b.member_id AND 
    a.member_id=b.member_id AND 
    a.image_name=c.image_name AND 
    c.img_id NOT IN (SELECT img_id FROM members_images_comments GROUP BY img_id) 
)           

ORDER BY 
    numComm DESC, date_upload DESC 
LIMIT 
    $set_limit, $li

QUERY 2

(
SELECT 
    a.image_name, a.member_id, b.username, c.image_alt , c.img_id, 
a.total_views, c.island_id, c.personal, c.date_upload, COUNT(e.img_id) AS numComm  
FROM 
    members_stats_images a, members b, members_images c, members_photo_see_do d,
 members_images_comments e  
WHERE 
    c.island_id='$island_id' AND 
    c.personal='N' AND 
    c.approved='Y' AND 
    c.is_other<>'Y' AND 
    c.member_id=b.member_id AND 
    a.member_id=b.member_id AND 
    a.image_name=c.image_name AND 
    c.img_id=d.img_id AND 
    d.village_id='$village_id' AND 
    e.img_id=c.img_id 
GROUP BY 
    e.img_id 
)
UNION 
(
SELECT 
    a.image_name, a.member_id, b.username, c.image_alt , c.img_id, 
a.total_views, c.island_id, c.personal, c.date_upload, 0 AS numComm  
FROM 
    members_stats_images a, members b, members_images c, members_photo_see_do d 
WHERE 
    c.island_id='$island_id' AND 
    c.personal='N' AND 
    c.approved='Y' AND 
    c.is_other<>'Y' AND 
    c.member_id=b.member_id AND 
    a.member_id=b.member_id AND 
    a.image_name=c.image_name AND 
    c.img_id=d.img_id AND 
    d.village_id='$village_id' AND 
    c.img_id NOT IN (SELECT img_id FROM members_images_comments GROUP BY img_id)
)           

ORDER BY 
    numComm DESC, date_upload DESC 
LIMIT 
    $set_limit, $limit

如您所见,这两个查询非常相似。

第二个查询在几秒钟内执行,而第一个查询在两分钟内执行。

任何人都可以建议改进第一个查询吗?

注意:所有涉及的表都在所有id字段中都有索引。

修改 根据@ G-Nugget的建议,我用JOIN编写了上述查询,如:

    SELECT  
        c.img_id, c.image_name, c.member_id, c.image_alt, c.island_id, c.personal, c.date_upload, b.username,  c.island_id, c.personal, c.date_upload, 
        a.total_views,  
        COUNT(e.img_id) AS numComm 
    FROM members_images AS c 
    LEFT JOIN members_stats_images AS a ON c.image_name=a.image_name AND c.member_id=a.member_id
    LEFT JOIN members AS b ON c.member_id=b.member_id 
    LEFT JOIN members_photo_see_do AS d ON c.img_id=d.img_id 
    LEFT JOIN members_images_comments AS e ON c.img_id=e.img_id 
    WHERE 
    c.island_id='$island_id' AND 
    c.personal='N' AND 
    c.approved='Y' AND 
    c.is_other<>'Y' AND 
    d.see_id='$see_id' 
    GROUP BY c.img_id   

它现在完美无损地执行。

1 个答案:

答案 0 :(得分:3)

由于两个查询之间的唯一区别是添加了members_photo_see_do表,因此显然缺少一个索引来支持从members_images的连接。

如果没有关于查询和表定义的更多信息,就不可能说更多。

此外,您会发现使用现代JOIN语法可以大大减轻对查询的阅读和思考。如果你采用它,你也会发现这里的会员更愿意帮助你。请在回电时使用它来撰写查询。