1. 程式人生 > >sql在資料庫執行很快 放在程式碼中就相當慢

sql在資料庫執行很快 放在程式碼中就相當慢

 select d.*,c.name AS couponName from  pe_a d
left join pe_b r  on r.id = d.ruleId
left join pe_c c on d.coupon_id = c.id
 where 1=1 and r.`enable` = 1 and r.delete_flag = 0
and  r.expity_start < now() and r.expity_end > now()  
and  d.status = 1 and d.expity_start < '2019-04-11 20:36:58' and d.expity_end > '2019-04-11 20:36:58' 
and  ( 1=2  or d.allocate_phone_number = '15201733860' or d.allocate_user_id = '420832473243164672' )  
  order by d.expity_end  limit 0,100 

資料庫執行0.03秒,程式碼執行5秒

-------------------------原因-----------------------

builder.append(" or d.allocate_user_id = ?");
				args.add(user.getId());

結果是這樣的:allocate_user_id 的型別引數是long,資料庫型別是string,所以型別不對,導致慢了
 select d.*,c.name AS couponName from  pe_a d
left join pe_b r  on r.id = d.ruleId
left join pe_c c on d.coupon_id = c.id
 where 1=1 and r.`enable` = 1 and r.delete_flag = 0
and  r.expity_start < now() and r.expity_end > now()  
and  d.status = 1 and d.expity_start < '2019-04-11 20:36:58' and d.expity_end > '2019-04-11 20:36:58' 
and  ( 1=2  or d.allocate_phone_number = '15201733860' or d.allocate_user_id = 420832473243164672')  
  order by d.expity_end  limit 0,100