国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > 文庫 > 正文

模式配置like %XXX% 優化

2024-09-07 22:12:54
字體:
來源:轉載
供稿:網友
  在MySQL里,like'XXX%可以用到索引,但like '%XXX%'卻不行,比如,以下這個案例:
   查看測試表行數:
  點擊(此處)折疊或打開
 
  mysql> select count(*) from test03;
  +----------+
  | count(*) |
  +----------+
  | 117584   |
  +----------+
    兩次like匹配對比:
  點擊(此處)折疊或打開
 
  mysql> explain select count(*) from test03 where username like '1%';
  +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  | id | select_type | table  | type  | possible_keys   | key             | key_len | ref  | rows  | Extra                    |
  +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  | 1  | SIMPLE      | test03 | range | idx_test03_name | idx_test03_name | 302     | NULL | 58250 | Using where; Using index |
  +----+-------------+--------+-------+-----------------+-----------------+---------+------+-------+--------------------------+
  1 row in set (0.03 sec)
 
  mysql> explain select count(*) from test03 where username like '%1%';
  +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  | id | select_type | table | type   | possible_keys | key             | key_len | ref  | rows   | Extra                    |
  +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  | 1  | SIMPLE      | test03| index  | NULL          | idx_test03_name | 302     | NULL | 116500 | Using where; Using index |
  +----+-------------+--------+-------+---------------+-----------------+---------+------+--------+--------------------------+
  1 row in set (0.00 sec)
  優化思路:
   這個測試表中,id是主鍵,葉子節點上保存了數據,從索引中就可以去到select的的id的列,不必讀取數據行(只有select字段正好就是索引,那么就用到了覆蓋索引),通過覆蓋索引,減少I/O,提高性能。
   優化之前的執行計劃:
  點擊(此處)折疊或打開
 
  mysql> explain select count(*) from test03 where username like '%1%';
  +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
  | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
  +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
  | 1  | SIMPLE      | test03 | ALL  | NULL          | NULL | NULL    | NULL | 7164 | Using where |
  +----+-------------+--------+------+---------------+------+---------+------+------+-------------+
    優化之后的執行計劃:
  點擊(此處)折疊或打開
 
  mysql> explain select count(*) from test03 a join (select id from test03 where username like '%1%') b on a.id=b.id;
  +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
  | id | select_type | table      | type   | possible_keys | key     | key_len | ref  | rows | Extra       |
  +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+
  | 1  | PRIMAR      | <derived2> | ALL    | NULL          | NULL    | NULL    | NULL | 7164 | NULL        |
  | 1  | PRIMARY     | a          | eq_ref | PRIMARY       | PRIMARY | 8       | b.id | 1    | Using index |
  | 2  | DERIVED     | test03     | ALL    | NULL          | NULL    | NULL    | NULL | 7164 | Using where |
  +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵山县| 武邑县| 沧源| 海原县| 甘孜县| 太白县| 丹江口市| 越西县| 宝兴县| 淮滨县| 无为县| 天气| 波密县| 永兴县| 石柱| 黄冈市| 星座| 鞍山市| 油尖旺区| 迁安市| 凌海市| 林西县| 高安市| 平泉县| 高邑县| 丹寨县| 青岛市| 黔西| 延津县| 高唐县| 伊宁县| 江达县| 山东省| 萝北县| 新丰县| 小金县| 鸡泽县| 剑川县| 上饶县| 兰坪| 响水县|