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

您的位置:首頁技術文章
文章詳情頁

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

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

問題描述

問題解答

回答1:

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

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,好了,直接貼我改造后的結果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)建復合索引(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篩選的結果集較少的話,可在union子查詢中不用加AND record_global_id < 5407938 ORDER BY record_global_id DESC LIMIT 0 , 20

主站蜘蛛池模板: 亚洲女人网 | 男女午夜爽爽 | 色欲麻豆国产福利精品 | 免费一级毛片在线播放视频 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 国产精品国产国产aⅴ | 毛片免费观看网址 | 久久成人午夜 | 怡红院免费全部视频在线视频 | 国产成人夜间影院在线观看 | 亚洲伊人色| 久视频在线观看 | 深夜福利视频网站 | 国产无限制自拍 | 免费一级网站免费 | 视频二区在线 | 久久99久久精品免费思思6 | 中国一级毛片免费观看 | 欧美美女色 | 美女视频黄视大全视频免费网址 | 91福利精品老师国产自产在线 | 国产真实生活伦对白 | 久草网站在线 | 亚洲加勒比久久88色综合 | 国产成人精品综合 | 婷婷色九月综合激情丁香 | 国产孕妇孕交视频 | 一级黄色毛片免费看 | 国产三级日本三级美三级 | 成人午夜免费视频 | a级特黄毛片免费观看 | 欧美成人精品一区二区三区 | 日本农村寡妇一级毛片 | 欧美一级永久免费毛片在线 | 黄色三级免费 | 成人午夜毛片在线看 | 免费国产成人高清无线看软件 | 国产精品久久久久久久久免费观看 | 欧美毛片日韩一级在线 | 国产精品99久久久久久宅男 | 久久久久依人综合影院 |