SQL 优化:连接表查询优化

SQL 优化:连接表查询优化

两天前,在项目中遇到一个需要优化SQL的情况,现在优化已完成,记录下方法。

问题:项目中某个页面的搜索功能耗时很长,需要重新编写SQL或者优化

解决:

一、分析原SQL:
查看原来的SQL,发现查询涉及到四个不同的表,使用一条SQL一次性查询出来,SQL中是先把每个每个表的数据查出来,用union all连接后,再使用where 条件进行帅选。
二、优化SQL
1.一开始想到的是使用索引,使用 EXPLAIN 对原SQL进行分析,发现没有使用索引,然后对每个表建立索引,再次测试,速度有所提高,但是还是没有达到想要的速度。
2.重新查看原SQL,然后无意中想到要是先把每个表中的符合条件的数据帅选出来,再把四个表帅选后的数据用 union all 连接起来,这样就可以避免查询一大堆本来不符合条件的数据出来,还可以直接连接查询出来的数据,不用再连接后再去帅选。
3.优化SQL后,再次测试,速度达到理想的状态,测试成功,优化结束。

原SQL

SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,RECIPIENT_MSISDN,SENT_TIME,CONTENT,STATUS,RESPONSE_TIME,DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MID FROM (

  (SELECT SO.ACCOUNT_NO,SO.USER_ID,SO.BATCH_ID,SO.PACK_ID,SO.DEST_ADDR AS RECIPIENT_MSISDN,SO.SUBMIT_TIME AS SENT_TIME,SO.CONTENT,SO.STATUS,SO.RESPONSE_TIME,SO.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SO.SRC_ADDR AS SENDER_NO,CONCAT('SMS',SO.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,SMS_ID AS MID FROM b3_SMS_OUTBOX SO LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'SMS' AS MESSAGE_TYPE FROM BATCH_SMS) s ON SO.ACCOUNT_NO=s.ACCOUNT_NO AND SO.USER_ID=s.USER_ID AND SO.BATCH_ID = s.BATCH_ID AND SO.PACK_ID = s.PACK_ID ) 

  UNION ALL 

  (SELECT MO.ACCOUNT_NO,MO.USER_ID,MO.BATCH_ID,1,MO.RECIPIENT_MSISDN,MO.SENT_TIME,MO.CONTENT,MO.STATUS,MO.DELIVERY_TIME,MO.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,MO.SENDER_NO,CONCAT('MMS',MO.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MMS_ID AS MID FROM b3_MMS_OUTBOX MO LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'MMS' AS MESSAGE_TYPE FROM BATCH_MMS) M ON MO.ACCOUNT_NO=M.ACCOUNT_NO AND MO.USER_ID=M.USER_ID AND MO.BATCH_ID = M.BATCH_ID ) 

  UNION ALL 

  (SELECT SOB.ACCOUNT_NO,SOB.USER_ID,SOB.BATCH_ID,SOB.PACK_ID,SOB.DEST_ADDR AS RECIPIENT_MSISDN,SOB.SUBMIT_TIME AS SENT_TIME,SOB.CONTENT,SOB.STATUS,SOB.RESPONSE_TIME,SOB.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SOB.SRC_ADDR AS SENDER_NO,CONCAT('SMS',SOB.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,SMS_ID AS MID FROM SMS_OUTBOX SOB LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'SMS' AS MESSAGE_TYPE FROM BATCH_SMS) s ON SOB.ACCOUNT_NO=s.ACCOUNT_NO AND SOB.USER_ID=s.USER_ID AND SOB.BATCH_ID = s.BATCH_ID AND SOB.PACK_ID = s.PACK_ID ) 

  UNION ALL 

  (SELECT MOB.ACCOUNT_NO,MOB.USER_ID,MOB.BATCH_ID,1,MOB.RECIPIENT_MSISDN,MOB.SENT_TIME,MOB.CONTENT,MOB.STATUS,MOB.DELIVERY_TIME,MOB.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,MOB.SENDER_NO,CONCAT('MMS',MOB.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MMS_ID AS MID FROM MMS_OUTBOX MOB LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'MMS' AS MESSAGE_TYPE FROM BATCH_MMS) M ON MOB.ACCOUNT_NO=M.ACCOUNT_NO AND MOB.USER_ID=M.USER_ID AND MOB.BATCH_ID = M.BATCH_ID )

  ) SEARCHSECTION  WHERE MOB.ACCOUNT_NO = 'b3' and STATUS in ('S','D','F')  AND SENT_TIME >= '2019-03-18 00:00:00' AND RECIPIENT_MSISDN = '85261234564' AND MOB.BATCH_ID IN (SELECT BATCH_ID FROM BATCH_MMS where ACCOUNT_NO = 'b3' AND TITLE LIKE '%sms 0314%') ORDER BY BATCH_ID DESC, PACK_ID, MID LIMIT 0,15

优化后的SQL

SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,RECIPIENT_MSISDN,SENT_TIME,CONTENT,STATUS,RESPONSE_TIME,DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MID FROM (

  (SELECT SO.ACCOUNT_NO,SO.USER_ID,SO.BATCH_ID,SO.PACK_ID,SO.DEST_ADDR AS RECIPIENT_MSISDN,SO.SUBMIT_TIME AS SENT_TIME,SO.CONTENT,SO.STATUS,SO.RESPONSE_TIME,SO.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SO.SRC_ADDR AS SENDER_NO,CONCAT('SMS',SO.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,SMS_ID AS MID FROM b3_SMS_OUTBOX SO LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'SMS' AS MESSAGE_TYPE FROM BATCH_SMS) s ON SO.ACCOUNT_NO=s.ACCOUNT_NO AND SO.USER_ID=s.USER_ID AND SO.BATCH_ID = s.BATCH_ID AND SO.PACK_ID = s.PACK_ID  WHERE SO.ACCOUNT_NO = 'b3' and STATUS in ('S','D','F')  AND SUBMIT_TIME >= '2019-03-18 00:00:00' AND DEST_ADDR = '85261234564' AND SO.BATCH_ID IN (SELECT BATCH_ID FROM BATCH_SMS where ACCOUNT_NO = 'b3' AND TITLE LIKE '%sms 0314%') ) 

  UNION ALL 

  (SELECT MO.ACCOUNT_NO,MO.USER_ID,MO.BATCH_ID,1,MO.RECIPIENT_MSISDN,MO.SENT_TIME,MO.CONTENT,MO.STATUS,MO.DELIVERY_TIME,MO.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,MO.SENDER_NO,CONCAT('MMS',MO.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MMS_ID AS MID FROM b3_MMS_OUTBOX MO LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'MMS' AS MESSAGE_TYPE FROM BATCH_MMS) M ON MO.ACCOUNT_NO=M.ACCOUNT_NO AND MO.USER_ID=M.USER_ID AND MO.BATCH_ID = M.BATCH_ID  WHERE MO.ACCOUNT_NO = 'b3' and STATUS in ('S','D','F')  AND SENT_TIME >= '2019-03-18 00:00:00' AND RECIPIENT_MSISDN = '85261234564' AND MO.BATCH_ID IN (SELECT BATCH_ID FROM BATCH_MMS where ACCOUNT_NO = 'b3' and TITLE LIKE '%sms 0314%') ) 

  UNION ALL 

  (SELECT SOB.ACCOUNT_NO,SOB.USER_ID,SOB.BATCH_ID,SOB.PACK_ID,SOB.DEST_ADDR AS RECIPIENT_MSISDN,SOB.SUBMIT_TIME AS SENT_TIME,SOB.CONTENT,SOB.STATUS,SOB.RESPONSE_TIME,SOB.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SOB.SRC_ADDR AS SENDER_NO,CONCAT('SMS',SOB.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,SMS_ID AS MID FROM SMS_OUTBOX SOB LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,PACK_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'SMS' AS MESSAGE_TYPE FROM BATCH_SMS) s ON SOB.ACCOUNT_NO=s.ACCOUNT_NO AND SOB.USER_ID=s.USER_ID AND SOB.BATCH_ID = s.BATCH_ID AND SOB.PACK_ID = s.PACK_ID  WHERE SOB.ACCOUNT_NO = 'b3' and STATUS in ('S','D','F')  AND SUBMIT_TIME >= '2019-03-18 00:00:00' AND DEST_ADDR = '85261234564' AND SOB.BATCH_ID IN (SELECT BATCH_ID FROM BATCH_SMS where ACCOUNT_NO = 'b3' AND TITLE LIKE '%sms 0314%') ) 

  UNION ALL 

  (SELECT MOB.ACCOUNT_NO,MOB.USER_ID,MOB.BATCH_ID,1,MOB.RECIPIENT_MSISDN,MOB.SENT_TIME,MOB.CONTENT,MOB.STATUS,MOB.DELIVERY_TIME,MOB.DONE_TIME,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,MOB.SENDER_NO,CONCAT('MMS',MOB.BATCH_ID) AS CAMPAIGN_ID,CAMPAIGN_TYPE,MESSAGE_TYPE,MMS_ID AS MID FROM MMS_OUTBOX MOB LEFT JOIN (SELECT ACCOUNT_NO,USER_ID,BATCH_ID,TITLE,SCHEDULED_TIME,SCHEDULING_STATUS,SENDER_NO,ALPHA_SENDER_NO,SMART_FILTERING,CAMPAIGN_TYPE,'MMS' AS MESSAGE_TYPE FROM BATCH_MMS) M ON MOB.ACCOUNT_NO=M.ACCOUNT_NO AND MOB.USER_ID=M.USER_ID AND MOB.BATCH_ID = M.BATCH_ID  WHERE MOB.ACCOUNT_NO = 'b3' and STATUS in ('S','D','F')  AND SENT_TIME >= '2019-03-18 00:00:00' AND RECIPIENT_MSISDN = '85261234564' AND MOB.BATCH_ID IN (SELECT BATCH_ID FROM BATCH_MMS where ACCOUNT_NO = 'b3' AND TITLE LIKE '%sms 0314%') )

  ) SEARCHSECTION ORDER BY BATCH_ID DESC, PACK_ID, MID LIMIT 0,15

版权声明:本文为博主原创文章,转载请注明出处。
https://blog.csdn.net/jim_LoveQ/article/details/88820347

你可能感兴趣的:(SQL)