您的位置:首页 > 数据库

sql优化的一些经验

2018-03-12 00:00 155 查看
/* 优化前 2.078s*/
SELECT 	COUNT(t.x_id)
FROM
order_detail t
WHERE
t.user_sid = 'xiaoming'
AND t.order_type NOT IN (
'A',
'B',
'C',
'D'
);

/* 优化后 0.091s */

SELECT COUNT(a.x_id) FROM (
SELECT DISTINCT(t.x_id)
FROM order_detail t
WHERE t.user_sid = 'xiaoming'
AND t.order_type='N'
UNION ALL
(
SELECT DISTINCT(t.x_id)
FROM order_detail t
WHERE t.user_sid = 'xiaoming'
AND t.order_type IS NULL
)
UNION ALL
(
SELECT DISTINCT(t.x_id)
FROM order_detail t
WHERE t.user_sid = 'xiaoming'
AND t.order_type = ''
)
) a

not in转换为对应的 in,然后用union all 来代替 in,速度能提高几十倍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: