搜索

首页  >  问答  >  正文

mysql case when 查询一个效率问题

第一种:

SELECT
    `user`.username,
    `user`.email,
    `user`.id AS user_id,
    user_class.level_id,
    user_class.class_id AS cid,
    user_class.create_time AS bontime,
    class.graduation_time,
    class.offline_graduation_time,
    class.is_baidan,
    class. NAME AS class,
    class.period_id,
    class.offline_period_id,
    company. NAME AS branch,
    company.id AS bid
FROM
    `user_class`
INNER JOIN `user` ON user_class.user_id = `user`.id
INNER JOIN class ON user_class.class_id = class.id
LEFT JOIN company ON class.company_id = company.id
LEFT JOIN period ON class.period_id = period.id
WHERE
    class. DISABLE = 1
AND class.is_baidan IN ('1', '2')
AND `user_class`.level_id IN ('10', '12', '14')
AND CASE
WHEN is_baidan = 1 THEN
    class.graduation_time=0 
    OR class.graduation_time > 1469980800
WHEN is_baidan = 2 THEN
    class.offline_graduation_time = 0
    OR class.offline_graduation_time > 1469980800
END;

第二种:

SELECT
    `user`.username,
    `user`.email,
    `user`.id AS user_id,
    user_class.level_id,
    user_class.class_id AS cid,
    user_class.create_time AS bontime,
    class.graduation_time AS eontime,
    class.offline_graduation_time,
    class.is_baidan,
    class. NAME AS class,
    class.period_id,
    class.offline_period_id,
    company. NAME AS branch,
    company.id AS bid
FROM
    `user_class`
INNER JOIN `user` ON user_class.user_id = `user`.id
INNER JOIN class ON user_class.class_id = class.id
LEFT JOIN company ON class.company_id = company.id
LEFT JOIN period ON class.period_id = period.id
WHERE
    class. DISABLE = 1
AND `user_class`.level_id IN ('10', '12', '14')
AND (
    (
        class.is_baidan = 1 AND 
        (class.graduation_time = 0 OR class.graduation_time > 1469980800)
    )
    OR (
        class.is_baidan = 2 AND 
        (class.offline_graduation_time=0 OR class.offline_graduation_time >1469980800)
    )
);

这两个sql语句查询结果相同,想问问到底是哪个好一点

黄舟黄舟2786 天前680

全部回复(1)我来回复

  • 怪我咯

    怪我咯2017-04-17 15:23:55

    看mysql的执行计划,你这个sql语句不执行执行计划看,我觉得谁也告知不了你准确的答案,因为他们没有非常明显的性能差异。

    回复
    0
  • 取消回复