使用OR运算符VS UNION运算符

时间:2016-06-09 13:39:18

标签: mysql sql

我有两个问题:(它们的结果相同)。它们之间的唯一区别是使用OR vs UNION ALL ..!

首先:

. . . WHERE h.id = :id1 OR h.related = :id2 . . .

第二

. . . FROM ((select q.* from quanda q where q.id = :id1) UNION ALL
            (select q.* from quanda q where q.related = :id2)
           ) LEFT JOIN . . .

我听说使用UNION会更快..但是测试表明使用OR的速度要快得多。为什么?我做错了吗?

首先:

SELECT h.id, 
       h.subject,
       h.body matnF,
       h.amount,
       h.keywords tags,
       h.closed,
       h.author_id author,
       h.AcceptedAnswer,
       h.type,
       h.visibility,
       h.date_time,
       v.value AS vote_value,
       u.reputation,
       u.user_fname,
       u.user_lname,
       u.avatar,
       (h.author_id = 29) as hasUserId,
       (select COALESCE(sum(vv.value),0) from votes vv
        where h.id = vv.post_id and vv.table_code = 15) as total_votes,
       (select count(1) from favorites ff
        where h.type = 0 and h.id = ff.post_id and ff.table_code = 15) as total_fav,
       CASE WHEN h.type = 1 THEN '0'
            WHEN h.amount IS NULL OR h.author_id = 29 THEN '1'
            ELSE EXISTS (select 1 from money m
                          where m.user_id = 29 and m.post_id = h.id)
       END paid,
       CASE WHEN h.type = 0 AND f.id IS NOT NULL THEN '2' ELSE '3'
       END AS favorite
FROM qanda h
LEFT JOIN votes v
     ON h.id = v.post_id AND v.user_id = 29 AND v.table_code = 15
LEFT JOIN favorites f
     ON h.type = 0 AND h.id = f.post_id AND f.user_id = 29 AND f.table_code = 15
LEFT JOIN users u
     ON h.author_id = u.id and h.visibility = 1
WHERE h.id = 9 OR h.related = 9                            -- this
ORDER BY h.type, h.AcceptedAnswer DESC, h.date_time

EXPLAIN的结果:

enter image description here

基准:

小型数据集的时间执行(包含30行的表格) - (运行500次):1.739413022995

第二

SELECT h.id, 
       h.subject,
       h.body matnF,
       h.amount,
       h.keywords tags,
       h.closed,
       h.author_id author,
       h.AcceptedAnswer,
       h.type,
       h.visibility,
       h.date_time,
       v.value AS vote_value,
       u.reputation,
       u.user_fname,
       u.user_lname,
       u.avatar,
       (h.author_id = 29) as hasUserId,
       (select COALESCE(sum(vv.value),0) from votes vv
        where h.id = vv.post_id and vv.table_code = 15) as total_votes,
       (select count(1) from favorites ff
        where h.type = 0 and h.id = ff.post_id and ff.table_code = 15) as total_fav,
       CASE WHEN h.type = 1 THEN '0'
            WHEN h.amount IS NULL OR h.author_id = 29 THEN '1'
            ELSE EXISTS (select 1 from money m
                          where m.user_id = 29 and m.post_id = h.id)
       END paid,
       CASE WHEN h.type = 0 AND f.id IS NOT NULL THEN '2' ELSE '3'
       END AS favorite
FROM ((SELECT q.* FROM qanda q WHERE q.id = 9)                -- this
        UNION ALL
      (SELECT q.* FROM qanda q WHERE q.related = 9)
     ) h
LEFT JOIN votes v
     ON h.id = v.post_id AND v.user_id = 29 AND v.table_code = 15
LEFT JOIN favorites f
     ON h.type = 0 AND h.id = f.post_id AND f.user_id = 29 AND f.table_code = 15
LEFT JOIN users u
     ON h.author_id = u.id and h.visibility = 1
ORDER BY h.type, h.AcceptedAnswer DESC, h.date_time

EXPLAIN的结果:

enter image description here

基准:

小型数据集的时间执行(包含30行的表格) - (运行500次):15.66437292099

0 个答案:

没有答案