成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

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

mysql優化 - mysql慢查詢問題

瀏覽:82日期:2022-06-20 11:41:33

問題描述

表結構(表的數據量在100萬左右)

CREATE TABLE `t_user_notification` ( `notify_id` bigint(20) NOT NULL, `user_id` bigint(20) NOT NULL, `notify` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `priority` tinyint(1) NOT NULL DEFAULT ’0’, `insert_time` datetime DEFAULT NULL, PRIMARY KEY (`notify_id`), KEY `idx_user_notification__priority_user_id` (`user_id`,`priority`), KEY `idx_userid_notifyid` (`user_id`,`notify_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_cisql語句一:

select notify_id, notify from t_user_notification where user_id = 1 and notify_id > 1 and priority = 1 order by notify_id asc limit 20G

這條語句執行大概花了10s,explain結果如下:

*************************** 1. row *************************** id: 1 select_type: SIMPLEtable: t_user_notification_0399 type: index_mergepossible_keys: PRIMARY,idx_user_notification__priority_user_id,idx_userid_notifyid key: idx_user_notification__priority_user_id,PRIMARY key_len: 17,8 ref: NULL rows: 22629Extra: Using intersect(idx_user_notification__priority_user_id,PRIMARY); Using where; Using filesort語句二:

SELECT notify_id, notify, priority FROM t_user_notificationWHERE user_id = 1AND ((priority = 1 AND notify_id > 123) OR (priority = 0 AND notify_id > 345))ORDER BY notify_id ASC LIMIT 20G

這條語句執行時間不定,偶爾出現超時

問題

如何優化索引使sql執行速度加快

問題解答

回答1:

第一個語句:從sql語句本身來說,是單表查詢,已經使用了索引,且索引中選擇性較高的字段userid也放到了前面,感覺沒什么可以優化的地方。從業務邏輯來說,可以考慮是否能增加insert_time的條件,并創建(userid, insert_time)的索引,這樣通過索引能夠過濾掉的記錄更多。還有一個思路就是拿空間換時間,創建一個包括查詢結果和條件所有字段的索引,如(user_id, priority, notify_id, notify),這樣只用查詢索引就能得到結果,避免了回表的查詢成本。另外就是看業務上是否允許不排序,這樣可以減少mysql做排序的成本。

第二個語句:除了第一個語句的優化思路外,可以考慮重寫sql,讓mysql使用索引idx_userid_notifyid

SELECT notify_id, notify, priority FROM t_user_notificationWHERE user_id = 1AND notify_id > 123AND (priority = 1 OR (priority = 0 AND notify_id > 345))ORDER BY notify_id ASC LIMIT 20回答2:

語句1:

select notify_id, notify from t_user_notification where user_id = 1 and priority = 1 and notify_id > 1 order by notify_id asc limit 20G

語句2:不懂怎么優化,但應該盡量避免用OR。

回答3:

比較奇怪,為什么不用自增主鍵?然后看樣子應該用了分區吧,按user_id分嗎?

這個索引:

KEY `idx_user_notification__priority_user_id` (`user_id`,`priority`)

改成這樣

KEY `idx_user_notification__priority_user_id` (`user_id`,`priority`,`notify_id`)

select t1.notify_id, t1.notify from t_user_notification t1, (select notify_id from t_user_notification where user_id = 1 and notify_id > 1 and priority = 1 order by notify_id asc limit 20) t2where t1.notify_id = t2.notify_id;回答4:

語句2改成這樣試試

SELECT notify_id, notify, priorityFROM t_user_notificationJOIN(SELECT notify_id FROM t_user_notificationWHERE user_id = 1AND notify_id > 123AND (priority = 1 OR (priority = 0 AND notify_id > 345))ORDER BY notify_id ASC LIMIT 20)as tmp using(notify_id)回答5:

強制使用索引吧,你這里涉及到索引合并 select * from xxx FORCE INDEX(primary_key)

相關文章:
主站蜘蛛池模板: 日韩成人免费在线视频 | 国产日韩一区二区三区在线观看 | 亚洲免费视频网址 | 免费在线观看a级毛片 | 欧美在线高清视频播放免费 | 日韩一级特黄 | 国产一级一片免费播放 | 国产萌白酱在线一区二区 | 中文字幕或区 | 久久成年视频 | 国产午夜精品久久理论片小说 | 免费一级成人免费观看 | 中文字幕最新中文字幕中文字幕 | 极品美女写真菠萝蜜视频 | 欧美另类孕交免费观看 | 国产三级成人 | 亚洲 [12p]| 欧美日韩高清观看一区二区 | 91手机看片国产福利精品 | 国产成人免费高清视频 | 一级做a级爰片性色毛片视频 | 久久91精品国产一区二区 | 一个人的视频日本免费 | 在线亚洲成人 | 国产毛片一区二区三区精品 | a三级黄色片 | 色老久久 | 男女乱配视频免费观看 | 精品视频在线看 | 91久久在线 | 欧美午夜在线观看理论片 | 欧美成人手机视频免费播放 | 国产区高清 | 18video9ex欧美生活片 | 国产成人在线网址 | 精品久久久久久中文字幕网 | 国产激情久久久久久影院 | 欧美色成人tv在线播放 | 成年女人免费观看视频 | 日韩成人在线视频 | 日韩免费观看一级毛片看看 |