mysql case when 查询一个效率问题
黄舟
黄舟 2017-04-17 15:21:55
0
1
679

第一种:

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语句查询结果相同,想问问到底是哪个好一点

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全員に返信(1)
刘奇

mysql の実行計画を見てください。SQL ステートメントが実行計画を実行しない場合、それらの間に明らかなパフォーマンスの違いがないため、正確な答えを言える人はいないと思います。

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート