MySQL高級-索引-使用規則-覆蓋索引回表查詢

文章目錄

  • 1、覆蓋索引
    • 1.1、查看索引
    • 1.2、刪除單列索引 idx_user_pro
    • 1.3、查詢 profession='軟件工程' and age=31 and status='0'
    • 1.4、執行計劃 profession='軟件工程' and age=31 and status='0'
    • 1.5、執行計劃 select id,profession,age,status
    • 1.6、執行計劃 select id,profession,age,status,name
  • 2、思考題

1、覆蓋索引

盡量使用覆蓋索引(查詢使用了索引,并且需要返回的列,在該索引中已經全部能夠找到),減少select * 。

1.1、查看索引

mysql> show index from tb_user;
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table   | Non_unique | Key_name             | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| tb_user |          0 | PRIMARY              |            1 | id          | A         |          24 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            1 | profession  | A         |          16 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            2 | age         | A         |          22 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            3 | status      | A         |          24 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro         |            1 | profession  | A         |          16 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
5 rows in set (0.00 sec)mysql>

1.2、刪除單列索引 idx_user_pro

mysql> drop index idx_user_pro on tb_user;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> show index from tb_user;
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table   | Non_unique | Key_name             | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| tb_user |          0 | PRIMARY              |            1 | id          | A         |          24 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            1 | profession  | A         |          16 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            2 | age         | A         |          22 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| tb_user |          1 | idx_user_pro_age_sta |            3 | status      | A         |          24 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+---------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
4 rows in set (0.01 sec)mysql>

1.3、查詢 profession=‘軟件工程’ and age=31 and status=‘0’

mysql> select * from tb_user where profession='軟件工程' and age=31 and status='0';
+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+
| id | name | phone       | email             | profession | age  | gender | status | createtime          |
+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+
| 16 | 妲己 | 17799990015 | 2783238293@qq.com | 軟件工程   |   31 | 2      | 0      | 2001-01-30 00:00:00 |
+----+------+-------------+-------------------+------------+------+--------+--------+---------------------+
1 row in set (0.00 sec)mysql>

1.4、執行計劃 profession=‘軟件工程’ and age=31 and status=‘0’

mysql> explain select * from tb_user where profession='軟件工程' and age=31 and status='0';
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
| id | select_type | table   | partitions | type | possible_keys        | key                  | key_len | ref               | rows | filtered | Extra                 |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
|  1 | SIMPLE      | tb_user | NULL       | ref  | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | const,const,const |    1 |   100.00 | Using index condition |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)mysql>

1.5、執行計劃 select id,profession,age,status

mysql> explain select id,profession,age,status from tb_user where profession='軟件工程' and age=31 and status='0';
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+--------------------------+
| id | select_type | table   | partitions | type | possible_keys        | key                  | key_len | ref               | rows | filtered | Extra                    |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+--------------------------+
|  1 | SIMPLE      | tb_user | NULL       | ref  | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | const,const,const |    1 |   100.00 | Using where; Using index |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)mysql>

using where;using index:查找使用了索引,但是需要的數據都在索引列中能找到,所以不需要回表查詢數據

1.6、執行計劃 select id,profession,age,status,name

mysql> explain select id,profession,age,status,name from tb_user where profession='軟件工程' and age=31 and status='0';
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
| id | select_type | table   | partitions | type | possible_keys        | key                  | key_len | ref               | rows | filtered | Extra                 |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
|  1 | SIMPLE      | tb_user | NULL       | ref  | idx_user_pro_age_sta | idx_user_pro_age_sta | 54      | const,const,const |    1 |   100.00 | Using index condition |
+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------------------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)mysql>

using index condition:查找使用了索引,但是需要回表查詢數據

2、思考題

一張表,有四個字段(id,username,password,status),由于數據量大,需要對一下SQL語句進行優化,該如何進行才是最優方案:
select id,username,password from tb_user where username=‘csdn’

select id,username,password from tb_user where username='csdn';

答案:針對于 username,password 建立聯合索引,sql 為:create index idx_user_name_pass on tb_user(username,password);這樣就可以避免上述的SQL語句,在查詢的過程中,出現回表查詢。

create index idx_user_name_pass on tb_user(username,password);

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/37727.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/37727.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/37727.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Transformer教程之多頭自注意力機制

大家好,今天我們要聊一聊Transformer中的一個核心組件——多頭自注意力機制。無論你是AI領域的新手,還是深度學習的老鳥,這篇文章都會幫助你更深入地理解這個關鍵概念。我們會從基礎開始,逐步深入,最終讓你對多頭自注意…

軟考《信息系統運行管理員》-1.3信息系統運維的發展

1.3信息系統運維的發展 我國信息系統運維的發展總體現狀 呈現三個“二八現象” 從時間周期看(開發流程)從信息系統效益看(消息體現為“用好”)從資金投入看(重開發,輕服務) 信息系統運維的發…

Codeforces Beta Round 32 (Div. 2, Codeforces format) D. Constellation 題解 枚舉

Constellation 題目描述 A star map in Berland is a checked field n m nm nm squares. In each square there is or there is not a star. The favorite constellation of all Berland’s astronomers is the constellation of the Cross. This constellation can be for…

JAVA高級進階13單元測試、反射、注解

第十三天、單元測試、反射、注解 單元測試 介紹 單元測試 就是針對最小的功能單元(方法),編寫測試代碼對其進行正確性測試 咱們之前是如何進行單元測試的? 有啥問題 ? 只能在main方法編寫測試代碼,去調用其他方法進行測試。 …

頁面開發感想

頁面開發 1、 前端預覽 2、一些思路 2.1、首頁自定義element-plus的走馬燈 :deep(.el-carousel__arrow){border-radius: 0%;height: 10vh; }需要使用:deep(標簽)才能修改樣式 或者 ::v-deep 標簽 2.2、整體設計思路 <template><div class"card" style&…

【ChatBI】text2sql-不需要訪問數據表-超輕量Python庫Vanna快速上手,對接oneapi

oneapi 準備 首先確保你有oneapi &#xff0c;然后申請 kimi的api 需要去Moonshot AI - 開放平臺 然后添加一個api key 然后打開oneapi的渠道界面&#xff0c;添加kimi。 然后點擊 測試&#xff0c; 如果能生成響應時間&#xff0c;就是配置正確。 然后創建令牌 http:…

Vllm Offline 啟動

Vllm Offline 啟動 Vllm Offline 啟動&#xff0c;設置環境變量&#xff0c; TRANSFORMERS_OFFLINE1reference: https://github.com/vllm-project/vllm/discussions/1405

Linux shell編程學習筆記60:touch命令

0 前言 在csdn技能樹Linux入門的練習題中&#xff0c;touch是最常見的一條命令。這次我們就來研究它的用法。 1 touch命令的功能、格式和選項說明 我們可以使用touch --help命令查看touch命令的幫助信息。 [purpleendurer bash ~ ]touch --help Usage: touch [OPTION]... …

MATLAB-NGO-CNN-SVM,基于NGO蒼鷹優化算法優化卷積神經網絡CNN結合支持向量機SVM數據分類(多特征輸入多分類)

NGO-CNN-SVM&#xff0c;基于NGO蒼鷹優化算法優化卷積神經網絡CNN結合支持向量機SVM數據分類(多特征輸入多分類) 1.數據均為Excel數據&#xff0c;直接替換數據就可以運行程序。 2.所有程序都經過驗證&#xff0c;保證程序可以運行。 3.具有良好的編程習慣&#xff0c;程序均…

【Android面試八股文】Activity A跳轉B,B跳轉C,A不能直接跳轉到C,A如何傳遞消息給C?

文章目錄 1. 使用Intent傳遞消息2. 使用全局單例類(Singleton)3. 使用靜態變量4. 使用Application全局靜態變量5. 使用 Android系統剪切板(Clipboard)6. 本地化存儲方式6.1 使用SharedPreferences6.2 使用File文件存儲方式傳遞消息6.3 使用SQLite數據庫方式傳遞消息7. 使用廣…

【Spring Boot】Java 的數據庫連接模板:JDBCTemplate

Java 的數據庫連接模板&#xff1a;JDBCTemplate 1.JDBCTemplate 初識1.1 JDBC1.2 JDBCTemplate 2.JDBCTemplate 實現數據的增加、刪除、修改和查詢2.1 配置基礎依賴2.2 新建實體類2.3 操作數據2.3.1 創建數據表2.3.2 添加數據2.3.3 查詢數據2.3.4 查詢所有記錄2.3.5 修改數據2…

【ai】tx2 nx:ubuntu18.04 yolov4-triton-tensorrt 成功部署server 運行

isarsoft / yolov4-triton-tensorrt運行發現插件未注冊? 【ai】tx2 nx: jetson Triton Inference Server 部署YOLOv4 【ai】tx2 nx: jetson Triton Inference Server 運行YOLOv4 對main 進行了重新構建 【ai】tx2 nx :ubuntu查找NvInfer.h 路徑及哪個包、查找符號【ai】tx2…

深度學習實戰81-基于大模型的Chatlaw法律問答中的知識圖譜融合思路,數據集說明、以及知識圖譜對ChatLaw的影響介紹

大家好,我是微學AI,今天給大家介紹一下深度學習實戰81-基于大模型的Chatlaw法律問答中的知識圖譜融合思路,數據集說明、以及知識圖譜對ChatLaw的影響介紹。基于大模型的Chatlaw法律問答系統融合了知識圖譜,以提高法律咨詢服務的可靠性和準確性。Chatlaw通過結合知識圖譜與人…

AES加密算法及AES-CMAC原理白話版系統解析

本文框架 前言1. AES加密理論1.1 不同AES算法區別1.2 加密過程介紹1.2.1 加密模式和填充方案選擇1.2.2 密鑰擴展1.2.3分組處理1.2.4多輪加密1.2.4.1字節替換1.2.4.2行移位1.2.4.3列混淆1.2.4.4輪密鑰加1.3 加密模式1.3.1ECB模式1.3.2CBC模式1.3.3CTR模式1.3.4CFB模式1.3.5 OFB模…

redis 單節點數據如何平滑遷移到集群中

目的 如何把一個redis單節點的數據遷移到 redis集群中 方案&#xff1a; 使用命令redis-cli --cluster import 導入數據至集群 --cluster-from <arg>--cluster-from-user <arg> 數據源用戶--cluster-from-pass <arg> 數據源密碼--cluster-from-askpass--c…

css_22_過渡動畫

一.過渡 transition-property 作用&#xff1a;定義哪個屬性需要過渡。結構&#xff1a; transition-property: all; 常用值&#xff1a; 1.none&#xff1a;不過渡任何屬性。 2.all&#xff1a;過渡所有能過渡的屬性。 3&#xff0e;具體某個屬性名&#xff0c;例如&#xf…

駕校預約小程序系統的設計

管理員賬戶功能包括&#xff1a;系統首頁&#xff0c;個人中心&#xff0c;學員管理&#xff0c;教練管理&#xff0c;駕校信息管理&#xff0c;駕校車輛管理&#xff0c;教練預約管理&#xff0c;考試信息管理 微信端賬號功能包括&#xff1a;系統首頁&#xff0c;駕校信息&a…

Java基礎——五、繼承

五、繼承 簡要 1、說明 繼承(Inheritance)是面向對象編程(OOP)的一個核心概念&#xff0c;它允許一個類(子類)繼承另一個類(父類)的屬性和方法&#xff0c;從而實現代碼重用和結構化組織。通過繼承&#xff0c;子類可以擴展父類的功能或者對父類的方法進行重寫。 父類(超類…

基于docker安裝redis服務

Redis是我們在項目中經常需要使用的緩存數據庫&#xff0c;安裝redis的方式也有很多&#xff0c;本文主要是給大家講解如何基于docker進行redis服務的安裝&#xff0c;主要介紹&#xff0c;如何拉取redis鏡像、如何掛載redis的數據以及使用redis的配置文件和開啟認證等功能&…

steam社區載入失敗、加載不出來、打不開?

隨著steam夏季大促的到來&#xff0c;最近steam在線用戶越來越多了&#xff0c;很多玩家在自己喜歡的游戲社區里看最新的玩法、攻略和玩家的游戲心得。不過有不少玩家表示有時候會打不開游戲社區或是社區加載失敗等問題。根據大家遇到的問題&#xff0c;這里總結了幾種解決方法…