欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

導(dǎo)致MySQL做全表掃描的幾種情況

 更新時(shí)間:2021年03月25日 11:56:56   作者:AsiaYe  
這篇文章主要介紹了導(dǎo)致MySQL做全表掃描的幾種情況,幫助大家更好的理解和學(xué)習(xí)使用MySQL,感興趣的朋友可以了解下

     這兩天看到了兩種可能會(huì)導(dǎo)致全表掃描的sql,這里給大家看一下,希望可以避免踩坑:

情況1:

強(qiáng)制類(lèi)型轉(zhuǎn)換的情況下,不會(huì)使用索引,會(huì)走全表掃描。

舉例如下:

首先我們創(chuàng)建一個(gè)表

 CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `age` int(11) DEFAULT NULL,
  `score` varchar(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `idx_score` (`score`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8

我們可以看到,這個(gè)表有三個(gè)字段,其中兩個(gè)int類(lèi)型,一個(gè)varchar類(lèi)型。varchar類(lèi)型的字段score是一個(gè)索引,而id是主鍵。

然后我們給這個(gè)表里面插入一些數(shù)據(jù),插入數(shù)據(jù)之后的表如下:

mysql:yeyztest 21:43:12>>select * from test;
+----+------+-------+
| id | age  | score |
+----+------+-------+
|  1 |    1 | 5     |
|  2 |    2 | 10    |
|  5 |    5 | 25    |
|  8 |    8 | 40    |
|  9 |    2 | 45    |
| 10 |    5 | 50    |
| 11 |    8 | 55    |
+----+------+-------+
7 rows in set (0.00 sec)

這個(gè)時(shí)候,我們使用explain語(yǔ)句來(lái)查看兩條sql的執(zhí)行情況,分別是:

explain select * from test where score ='10';

explain select * from test where score =10;

結(jié)果如下:

mysql:yeyztest 21:42:29>>explain select * from test where score ='10';
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | test  | NULL       | ref  | idx_score     | idx_score | 62      | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql:yeyztest 21:43:06>>explain select * from test where score =10;  
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | test  | NULL       | ALL  | idx_score     | NULL | NULL    | NULL |    7 |    14.29 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 3 warnings (0.00 sec)

    可以看到,如果我們使用的是varchar類(lèi)型的值,那么結(jié)果中掃描的行數(shù)rows就是1,而當(dāng)我們使用的是整數(shù)值10的時(shí)候,掃描行數(shù)變?yōu)榱?,證明,如果出現(xiàn)了強(qiáng)制類(lèi)型轉(zhuǎn)換,則會(huì)導(dǎo)致索引失效。

情況2:

   反向查詢不能使用索引,會(huì)導(dǎo)致全表掃描。

創(chuàng)建一個(gè)表test1,它的主鍵是score,然后插入6條數(shù)據(jù):

CREATE TABLE `test1` (
  `score` varchar(20) not null default '' ,
  PRIMARY KEY (`score`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql:yeyztest 22:09:37>>select * from test1;
+-------+
| score |
+-------+
| 111   |
| 222   |
| 333   |
| 444   |
| 555   |
| 666   |
+-------+
6 rows in set (0.00 sec)

    當(dāng)我們使用反向查找的時(shí)候,不會(huì)使用到索引,來(lái)看下面兩條sql:

explain select * from test1 where score='111';

explain select * from test1 where score!='111';
mysql:yeyztest 22:13:01>>explain select * from test1 where score='111';
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | test1 | NULL       | const | PRIMARY       | PRIMARY | 62      | const |    1 |   100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql:yeyztest 22:13:08>>explain select * from test1 where score!='111';
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                    |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
|  1 | SIMPLE      | test1 | NULL       | index | PRIMARY       | PRIMARY | 62      | NULL |    6 |   100.00 | Using where; Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

   可以看到,使用!=作為條件的時(shí)候,掃描的行數(shù)是表的總記錄行數(shù)。因此如果想要使用索引,我們就不能使用反向匹配規(guī)則。

情況3:

  某些or值條件可能導(dǎo)致全表掃描。

首先我們創(chuàng)建一個(gè)表,并插入幾條數(shù)據(jù):

CREATE TABLE `test4` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  KEY `idx_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql--dba_admin@127.0.0.1:yeyztest 22:23:44>>select * from test4;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    2 | bbb  |
|    3 | ccc  |
|    4 | yeyz |
| NULL | yeyz |
+------+------+
5 rows in set (0.00 sec)

   其中表test4包含兩個(gè)字段,id字段是一個(gè)索引,而name字段是varchar類(lèi)型,我們來(lái)看下面三個(gè)語(yǔ)句的掃描行數(shù):

explain select * from test4 where id=1;

explain select * from test4 where id is null;

explain select * from test4 where id=1 or id is null;
mysql:yeyztest 22:24:12>>explain select * from test4 where id is null;
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key    | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | test4 | NULL       | ref  | idx_id        | idx_id | 5       | const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql:yeyztest 22:24:17>>explain select * from test4 where id=1;                      
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key    | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | test4 | NULL       | ref  | idx_id        | idx_id | 5       | const |    1 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql:yeyztest 22:24:28>>explain select * from test4 where id=1 or id is null;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | test4 | NULL       | ALL  | idx_id        | NULL | NULL    | NULL |    5 |    40.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

   可以看到單獨(dú)使用id=1和id is null,都只會(huì)掃描一行記錄,而使用or將二者連接起來(lái)就會(huì)導(dǎo)致掃描全表而不使用索引。

簡(jiǎn)單總結(jié)一下:

1.強(qiáng)制類(lèi)型轉(zhuǎn)換的情況下,不會(huì)使用索引,會(huì)走全表掃描

2.反向查詢不能使用索引,會(huì)導(dǎo)致全表掃描。

3.某些or值條件可能導(dǎo)致全表掃描。

以上就是導(dǎo)致MySQL做全表掃描的幾種情況的詳細(xì)內(nèi)容,更多關(guān)于MySQL 全表掃描的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • mysql 協(xié)議的ping命令包及解析詳解及實(shí)例

    mysql 協(xié)議的ping命令包及解析詳解及實(shí)例

    這篇文章主要介紹了mysql 協(xié)議的ping命令包及解析詳解及實(shí)例的相關(guān)資料,這里附有簡(jiǎn)單實(shí)例代碼并附下載源碼,需要的朋友可以參考下
    2017-01-01
  • 當(dāng)面試官問(wèn)mysql中char與varchar的區(qū)別

    當(dāng)面試官問(wèn)mysql中char與varchar的區(qū)別

    這篇文章主要以聊天形式圖片的添加,將面試官面試真實(shí)場(chǎng)景體現(xiàn)出來(lái),好奇的朋友不要錯(cuò)過(guò)奧
    2021-08-08
  • Navicat修改MySQL數(shù)據(jù)庫(kù)密碼的多種方法

    Navicat修改MySQL數(shù)據(jù)庫(kù)密碼的多種方法

    這篇文章主要介紹了Navicat修改MySQL數(shù)據(jù)庫(kù)密碼,需要的朋友可以參考下
    2018-09-09
  • Spring中的InitializingBean和SmartInitializingSingleton的區(qū)別詳解

    Spring中的InitializingBean和SmartInitializingSingleton的區(qū)別詳解

    這篇文章主要介紹了Spring中的InitializingBean和SmartInitializingSingleton的區(qū)別詳解,InitializingBean只有一個(gè)接口方法afterPropertiesSet(),在BeanFactory初始化完這個(gè)bean,并且把bean的參數(shù)都注入成功后調(diào)用一次afterPropertiesSet()方法,需要的朋友可以參考下
    2024-01-01
  • mysql的單列多值存儲(chǔ)實(shí)例詳解

    mysql的單列多值存儲(chǔ)實(shí)例詳解

    數(shù)據(jù)庫(kù)市場(chǎng)需要細(xì)分,行式數(shù)據(jù)庫(kù)不再滿足所有的需求,而有很多需求需要,下面這篇文章主要給大家介紹了關(guān)于mysql單列多值存儲(chǔ)的相關(guān)資料,文中通過(guò)示例代碼介紹介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • MySql連接查詢方式詳解

    MySql連接查詢方式詳解

    這篇文章主要介紹了mysql連接查詢,當(dāng)查詢結(jié)果的列來(lái)源于多張表時(shí),需要將多張表連接成一個(gè)大的數(shù)據(jù)集,再選擇合適的列返回,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-09-09
  • Navicat如何遠(yuǎn)程連接云服務(wù)器數(shù)據(jù)庫(kù)

    Navicat如何遠(yuǎn)程連接云服務(wù)器數(shù)據(jù)庫(kù)

    這篇文章主要介紹了Navicat如何遠(yuǎn)程連接云服務(wù)器數(shù)據(jù)庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 分析MySQL拋出異常的幾種常見(jiàn)解決方式

    分析MySQL拋出異常的幾種常見(jiàn)解決方式

    在本文中,總結(jié)了開(kāi)發(fā)過(guò)程中最為常見(jiàn)的幾種 MySQL 拋出的異常以及如何解決,包括高版本驅(qū)動(dòng)的問(wèn)題、時(shí)區(qū)配置問(wèn)題、SSL 連接問(wèn)題等。于我個(gè)人而言,這一篇足以解決目前項(xiàng)目中所有遇到的MySQL問(wèn)題。同時(shí),也希望本文能對(duì) MySQL 數(shù)據(jù)庫(kù)初學(xué)者有一定的引導(dǎo)入門(mén)作用。
    2021-05-05
  • DROP TABLE在不同數(shù)據(jù)庫(kù)中的寫(xiě)法整理

    DROP TABLE在不同數(shù)據(jù)庫(kù)中的寫(xiě)法整理

    這篇文章主要介紹了DROP TABLE在不同數(shù)據(jù)庫(kù)中的寫(xiě)法整理的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 詳解grep獲取MySQL錯(cuò)誤日志信息的方法

    詳解grep獲取MySQL錯(cuò)誤日志信息的方法

    在本篇文章中小編給大家整理了關(guān)于通過(guò)grep 獲取MySQL錯(cuò)誤日志信息的方法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2018-09-09

最新評(píng)論