来源:自学PHP网 时间:2015-04-16 10:51 作者: 阅读:次
[导读] 这个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_i...
这个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
看来 这个IN确实需要谨慎使用,特别是在两表关联的时候,应该把in替换成内链接 |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com