Home >Database >Mysql Tutorial >【MySQL帮助】帮朋友优化SQL的过程--)从4S到0.011秒_MySQL

【MySQL帮助】帮朋友优化SQL的过程--)从4S到0.011秒_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:26:44829browse

bitsCN.com 这个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替换成内链接

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn