这个sql运行很慢,4秒钟
SELECT
(op.cash_payment+op.pos_payment+op.online_payment+op.charge_card_payment) AS real_payment,op.*
FROM db_order.`order_payment` AS op WHERE op.order_payment_id in
(SELECT oi.order_payment_id FROM db_order.`order_info` AS oi WHERE oi.batch_order_id = '123456')
ORDER BY op.insert_time DESC
我让朋友把explain结果帖下,如下图所示:
我看到where后面还有in,估计是这个in引起的,不过也许有数据量的问题,所以就再次确认下表的数据量,得到信息
order_info 44588
order_payment 74601
数据量很小,所以估计很大可能是in的问题。
所以把sql改成如下:
SELECT
(op.cash_payment+op.pos_payment+op.online_payment+op.charge_card_payment) AS real_payment,op.*
FROM db_order.`order_payment` AS op,db_order.`order_info` oi WHERE op.order_payment_id=oi.order_payment_id and oi.batch_order_id = '123456'
ORDER BY op.insert_time DESC
发给朋友,朋友说相当快,在0.011S。
看来 这个IN确实需要谨慎使用,特别是在两表关联的时候,应该把in替换成内链接