国产探花免费观看_亚洲丰满少妇自慰呻吟_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 |
  +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五指山市| 哈尔滨市| 富源县| 海盐县| 无锡市| 玉屏| 尖扎县| 海安县| 田林县| 策勒县| 左云县| 金堂县| 昂仁县| 霍城县| 阳泉市| 伊宁市| 嘉黎县| 铁岭县| 灌南县| 南皮县| 尉氏县| 龙州县| 金华市| 岚皋县| 海阳市| 甘洛县| 历史| 双鸭山市| 鱼台县| 霍山县| 周口市| 合肥市| 汤阴县| 恩施市| 永善县| 汉源县| 宁化县| 二手房| 金门县| 宕昌县| 竹溪县|