亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

數據庫 - MySQL 單表500W+數據,查詢超時,如何優化呢?

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

問題描述

問題解答

回答1:

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

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

回答2:

拆成幾條SQL分開查詢。

回答3:

根據題主的問題,你那條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;

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

回答4:

創建復合索引(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;

如果根據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

主站蜘蛛池模板: 91短视频免费 | 激情影院在线视频永久观看 | 爱爱网网站免费观看 | 亚洲精品中文字幕无乱码 | 欧美日韩国产一区三区 | 很黄很色的小视频在线网站 | 麻豆影视在线观看 | 亚瑟福利视频在线观看 | 欧美精品一区二区三区免费 | 欧美黄视频网站 | 美女久久久久久久久久久 | 中文字幕亚洲精品第一区 | 中文字幕高清在线 | 欧美成人禁片在线观看网址 | 久久久精品成人免费看 | 亚洲毛片在线看 | www.亚洲一区二区三区 | 国产精品免费精品自在线观看 | 香蕉免费一级视频在线观看 | 看免费的黄色片 | 夜夜精品视频一区二区 | 国产一二三四区在线观看 | 久久夜色tv网站免费影院 | 一级做a爰性色毛片 | 国产男女交性视频播放 | 狠狠五月天 | 亚洲福利视频一区二区三区 | 欧美精品一区二区三区在线 | a一级视频 | 国产高清色视频免费看的网址 | 亚洲国产激情一区二区三区 | 手机视频在线观看 | 91久久国产成人免费观看资源 | 欧美 日产 国产精品 | 欧美一级片黄色 | 国产羞羞视频在线观看 | 国产黄页在线观看 | 99久久国语对白精品露脸 | 在线观看国产片 | 国产精品视频免费视频 | 色播亚洲精品网站 亚洲第一 |