色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

數(shù)據(jù)庫 - MySQL 單表500W+數(shù)據(jù),查詢超時,如何優(yōu)化呢?

瀏覽:108日期:2022-06-13 14:39:08

問題描述

問題解答

回答1:

原因是你對record_global_id這個屬性做篩選,但條件不是等于,所以復(fù)合索引后面的部分就用不上了。

status列的區(qū)分度如何?加上索引(status, record_global_id)試試看。

回答2:

拆成幾條SQL分開查詢。

回答3:

根據(jù)題主的問題,你那條SQL條件那么多,但是只能用到一個索引,豈不可惜,WHERE條件很明顯的一處:如下的那個’OR’:

(( ((`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3)OR (`from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1) ) AND `type` = 2 AND `qa_id` = 0)OR ------------------- 此處這個OR ----------------------------------(`type` = 3 AND `to_uid` = 52494 AND `to_type` = 3 AND `from_uid` = 5017446 AND `from_type` = 1 AND `module` IN (’community.doctor:appointment:notice’ , ’community.doctor:transfer.treatment’, ’community.doctor:transfer.treatment.pay’, ’community.doctor:weiyi.guahao.to.user’, ’community.doctor:weiyi.prescription.to.patient’, ’community.doctor:user.buy.prescription’)) ) AND `status` = 1 AND `record_global_id` < 5407938

可以將整體的大的WHERE分拆開來,思路就是 UNION,好了,直接貼我改造后的結(jié)果SQL,如果有作用望采納呦^_^

改造后SQL:

(SELECT `record_global_id`, `type`, `mark`, `from_uid`, `from_type`, `to_uid`, `to_type`, `send_method`, `action`, `module`, `send_time`, `content`FROM `im_data_record`WHERE ((`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3)OR (`from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1) ) AND `type` = 2 AND `qa_id` = 0 AND `status` = 1 AND `record_global_id` < 5407938)UNION(SELECT `record_global_id`, `type`, `mark`, `from_uid`, `from_type`, `to_uid`, `to_type`, `send_method`, `action`, `module`, `send_time`, `content`FROM `im_data_record`WHERE `type` = 3 AND `to_uid` = 52494 AND `to_type` = 3 AND `from_uid` = 5017446 AND `from_type` = 1 AND `module` IN (’community.doctor:appointment:notice’ , ’community.doctor:transfer.treatment’, ’community.doctor:transfer.treatment.pay’, ’community.doctor:weiyi.guahao.to.user’, ’community.doctor:weiyi.prescription.to.patient’, ’community.doctor:user.buy.prescription’) AND `status` = 1 AND `record_global_id` < 5407938)ORDER BY `record_global_id` DESCLIMIT 0 , 20;

如有作用能將執(zhí)行計劃截圖發(fā)到評論里嗎?我想驗證下我的猜想,謝謝!

回答4:

創(chuàng)建復(fù)合索引(from_uid,to_uid,from_type,to_type,type,status,record_global_id)修改sql為union如下:

select * from ((SELECT `record_global_id`, `type`, `mark`, `from_uid`, `from_type`, `to_uid`, `to_type`, `send_method`, `action`, `module`, `send_time`, `content`FROM `im_data_record`WHERE`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3 AND `type` = 2 AND `qa_id` = 0 AND `status` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20) union(SELECT `record_global_id`, `type`, `mark`, `from_uid`, `from_type`, `to_uid`, `to_type`, `send_method`, `action`, `module`, `send_time`, `content`FROM `im_data_record`WHERE`from_uid` = 52494 AND `from_type` = 3 AND `to_uid` = 5017446 AND `to_type` = 1 AND `type` = 2 AND `qa_id` = 0 AND `status` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20) union(SELECT `record_global_id`, `type`, `mark`, `from_uid`, `from_type`, `to_uid`, `to_type`, `send_method`, `action`, `module`, `send_time`, `content`FROM `im_data_record`WHERE`from_uid` = 5017446 AND `from_type` = 1 AND `to_uid` = 52494 AND `to_type` = 3 AND `type` = 3 AND `module` IN (’community.doctor:appointment:notice’ , ’community.doctor:transfer.treatment’, ’community.doctor:transfer.treatment.pay’, ’community.doctor:weiyi.guahao.to.user’, ’community.doctor:weiyi.prescription.to.patient’, ’community.doctor:user.buy.prescription’)AND `status` = 1 AND `record_global_id` < 5407938 ORDER BY `record_global_id` DESC LIMIT 0 , 20)) aa ORDER BY `record_global_id` DESC LIMIT 0 , 20;

如果根據(jù)from_uid,to_uid,from_type,to_type,type,status篩選的結(jié)果集較少的話,可在union子查詢中不用加AND record_global_id < 5407938 ORDER BY record_global_id DESC LIMIT 0 , 20

主站蜘蛛池模板: 大学生久久香蕉国产线观看 | 国产欧美日韩在线不卡第一页 | 久久精品国产99久久72 | 午夜免费福利网站 | 成人怡红院 | 亚洲成a人不卡在线观看 | 中文字幕乱码中文乱码综合 | 国产欧美日韩另类 | 欧美精品18videos性欧美 | 美女视频网站黄色 | 九色愉拍自拍 | 亚洲国内精品自在线影视 | 免费看黄色片的网站 | 欧美视频精品一区二区三区 | 欧美日韩在线视频免费完整 | 蜜臀91精品国产高清在线观看 | a级毛片免费 | 99久久香蕉国产线看观香 | 欧美精品videosbestsex另类 | 成年午夜性爽快免费视频不卡 | 国产一区二区三区视频在线观看 | 欧美亚洲日本 | 国产精品单位女同事在线 | 欧美国产成人免费观看永久视频 | 国产成人在线视频 | caoporen在线视频入口 | 国产午夜a理论毛片在线影院 | 日本高清免费视频www | 国产精品免费视频一区 | 亚洲一区不卡 | 请看一下欧美一级毛片 | 国产97视频 | 日本视频在线免费观看 | 成人国产精品 | 一级片观看 | 91av福利视频| 久久免费国产精品一区二区 | 欧美透逼 | 亚洲精品国精品久久99热 | 国产精品成人免费视频 | 日本一级特黄a大片 |