update tbl_order_sub ts set ts.pay_status = 9 WHERE ts.id = ( select tse.order_sub_id from tbl_order_sub_expand tse where ts.out_order_id = '10030311646136' and ts.pay_status = 30 and tse.pre_sale_order = 1 and ts.id = tse.order_sub_id )索引: ts.id , ts.out_order_id
受影响的行: 1
时间: 81.230ms sql
把ts.id = ()修改成ts.id in ()执行结果时间没有改变 优化
update tbl_order_sub ts set ts.pay_status = 9 WHERE ts.id in ( select ts1.id from tbl_order_sub ts1,tbl_order_sub_expand tse where ts1.out_order_id = '10030311646136' and ts1.id = tse.order_sub_id and ts1.pay_status = 30 and tse.pre_sale_order = 1 )[Err] 1093 - You can't specify target table 'ts' for update in FROM clause
update tbl_order_sub ts set ts.pay_status = 9 WHERE ts.out_order_id = '10030311646136' and ts.id = ( select ts.id from tbl_order_sub_expand tse where ts.out_order_id = '10030311646136' and ts.id = tse.order_sub_id and ts.pay_status = 30 and tse.pre_sale_order = 1 LIMIT 1 )受影响的行: 1
where条件中每一次where查询,都会先把子查询执行一遍,因此若是能在子查询外进行条件割舍就割舍。 spa
----------------------------------------------------------------------------------- code
update tbl_order_sub_expand ts set ts.balance_due_date = now() where ts.order_sub_id = ( select tos.id from tbl_order_sub tos where tos.out_order_id = '10030311646136' and tos.id = ts.order_sub_id limit 1 )受影响的行: 1
这种状况还不如先把order_sub_id的值取出来再进行update操做。在DB端若是是长链接,两次得到connection也不会浪费太多的时间。 索引
----------------------------------------------------------------------------------- ci
update tbl_order_main om set om.prod_total_amt = 100, om.online_pay_amount = 100 where om.id = ( select os.order_main_id from tbl_order_sub os where os.order_main_id = om.id and os.id = '8b76d19ace50484ca6d14790b5c6f5ba' )耗时80s
update tbl_order_main om ,tbl_order_sub os set om.prod_total_amt = 100, om.online_pay_amount = 100 where os.order_main_id = om.id and os.id = '8b76d19ace50484ca6d14790b5c6f5ba'可能会要给两个表都进行加锁操做
update tbl_order_main om set om.prod_total_amt = 100, om.online_pay_amount = 100 where om.id = ( select os.order_main_id from tbl_order_sub os where os.id = '8b76d19ace50484ca6d14790b5c6f5ba' limit 1 )受影响的行: 0
减小了一个条件: get
where os.order_main_id = om.id而正是这个条件让sql解释器无法跳过优化,须要每次都执行完整的sql
//exist效率怎么样 it
select * from tbl_order_main om where EXISTS( select 1 from tbl_order_sub os where os.order_main_id = om.id and os.original_order_no is NULL and os.payment != 2 ) LIMIT 10 ;受影响的行: 0
select * from tbl_order_main om,tbl_order_sub os where os.order_main_id = om.id and os.original_order_no is NULL and os.payment != 2 LIMIT 10受影响的行: 0 时间: 0.124ms