溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

模式匹配like'%XXX%'優(yōu)化

發(fā)布時間:2020-08-05 07:49:11 來源:ITPUB博客 閱讀:206 作者:StevenBeijing 欄目:MySQL數(shù)據(jù)庫
 在MySQL里,like'XXX%可以用到索引,但like '%XXX%'卻不行,比如,以下這個案例:
 查看測試表行數(shù):

點擊(此處)折疊或打開

  1. mysql> select count(*) from test03;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 117584   |
  6. +----------+
  兩次like匹配對比:

點擊(此處)折疊或打開

  1. mysql> explain select count(*) from test03 where username like '1%';
  2. +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  3. | id | select_type | table  | type  | possible_keys   | key             | key_len | ref  | rows  | Extra                    |
  4. +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  5. | 1  | SIMPLE      | test03 | range | idx_test03_name | idx_test03_name | 302     | NULL | 58250 | Using where; Using index |
  6. +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  7. 1 row in set (0.03 sec)

  8. mysql> explain select count(*) from test03 where username like '%1%';
  9. +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  10. | id | select_type | table | type   | possible_keys | key             | key_len | ref  | rows   | Extra                    |
  11. +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  12. | 1  | SIMPLE      | test03| index  | NULL          | idx_test03_name | 302     | NULL | 116500 | Using where; Using index |
  13. +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  14. 1 row in set (0.00 sec)
優(yōu)化思路:
 這個測試表中,id是主鍵,葉子節(jié)點上保存了數(shù)據(jù),從索引中就可以去到select的的id的列,不必讀取數(shù)據(jù)行(只有select字段正好就是索引,那么就用到了覆蓋索引),通過覆蓋索引,減少I/O,提高性能。
 優(yōu)化之前的執(zhí)行計劃:

點擊(此處)折疊或打開

  1. mysql> explain select count(*) from test03 where username like '%1%';
  2. +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
  3. | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
  4. +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
  5. | 1  | SIMPLE      | test03 | ALL  | NULL          | NULL | NULL    | NULL | 7164 | Using where |
  6. +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
  優(yōu)化之后的執(zhí)行計劃:

點擊(此處)折疊或打開

  1. mysql> explain select count(*) from test03 a join (select id from test03 where username like '%1%') b on a.id=b.id;
  2. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
  3. | id | select_type | table      | type   | possible_keys | key     | key_len | ref  | rows | Extra       |
  4. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
  5. | 1  | PRIMAR      | <derived2> | ALL    | NULL          | NULL    | NULL    | NULL | 7164 | NULL        |
  6. | 1  | PRIMARY     | a          | eq_ref | PRIMARY       | PRIMARY | 8       | b.id | 1    | Using index |
  7. | 2  | DERIVED     | test03     | ALL    | NULL          | NULL    | NULL    | NULL | 7164 | Using where |
  8. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI