mysql区分度过低

今天运维说官网加载过慢,然后我看了一下,有个接口响应竟然达到5s,之后去用explan查看,发现因为价格好多一样的,然后导致区分度过低,也就是索引失效了

----原sql----
SELECT
	od.order_id AS orderId,
	s.signal_no AS signalNo,
	s.full_name AS fullName,
	s.picture_seq AS pictureSeq,
	om.eval_method AS evalMethod,
	oi.collection_stamp_id AS collectionStampId 
FROM
	digitization.`order` od
	JOIN digitization.order_main om ON od.order_main_id = om.order_main_id
	JOIN digitization.order_item oi ON od.order_id = oi.order_id
	JOIN appraisal.stamp s ON oi.stamp_id = s.stamp_id
	JOIN appraisal.evaluation_order eo ON eo.order_id = od.order_id 
WHERE
	od.order_status = 6 
	AND om.eval_method != 2 
	AND eo.evaluation_result = '真品/优良' 
GROUP BY
	od.order_id 
ORDER BY
	s.refer_price DESC,
	od.order_id DESC 
	LIMIT 9

解决办法,改用子查询,先把符合条件的查询出来,在符合条件的结果里进行排序

----修改之后的sql,采用子查询----
SELECT
	s1.*,
	s.signal_no AS signalNo,
	s.full_name AS fullName,
	s.picture_seq AS pictureSeq 
FROM
	(
	SELECT
		od.order_id AS orderId,
		om.eval_method AS evalMethod,
		oi.stamp_id stamp_id,
		oi.collection_stamp_id AS collectionStampId 
	FROM
		digitization.order_main om
		JOIN digitization.`order` od ON od.order_main_id = om.order_main_id
		JOIN digitization.order_item oi ON od.order_id = oi.order_id 
	WHERE
		od.order_status = 6 
		AND om.eval_method != 2 
	GROUP BY
		od.order_id 
	ORDER BY
		od.order_id DESC 
	) s1
	JOIN appraisal.stamp s ON s1.stamp_id = s.stamp_id
	JOIN appraisal.evaluation_order eo ON eo.order_id = s1.orderId 
WHERE
	eo.evaluation_result = '真品/优良' 
GROUP BY
	s1.orderId 
ORDER BY
	s.refer_price DESC,
	s1.orderId DESC 
	LIMIT 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值