MySQL 5.5 模式匹配LIKE
mysql> select * from t_test;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | Research | Beijing |
| 20 | Maintenance | Huludao |
| 30 | Market | Tianjin |
| 40 | Leader | Qingdao |
+--------+-------------+---------+
4 rows in set (0.00 sec)
mysql> create index idx_test_dname on t_test(dname);
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from t_test\G
*************************** 1. row ***************************
Table: t_test
Non_unique: 1
Key_name: idx_test_dname
Seq_in_index: 1
Column_name: dname
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
%Lea%沒有使用索引掃描
mysql> explain select * from t_test where dname like '%Lea%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | t_test | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
Lea%使用索引掃描
mysql> explain select * from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
| 1 | SIMPLE | t_test | range | idx_test_dname | idx_test_dname | 47 | NULL | 1 | Using where |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
1 row in set (0.12 sec)
mysql> explain select dname from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
| 1 | SIMPLE | t_test | index | idx_test_dname | idx_test_dname | 47 | NULL | 4 | Using where; Using index |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | Research | Beijing |
| 20 | Maintenance | Huludao |
| 30 | Market | Tianjin |
| 40 | Leader | Qingdao |
+--------+-------------+---------+
4 rows in set (0.00 sec)
mysql> create index idx_test_dname on t_test(dname);
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show keys from t_test\G
*************************** 1. row ***************************
Table: t_test
Non_unique: 1
Key_name: idx_test_dname
Seq_in_index: 1
Column_name: dname
Collation: A
Cardinality: 4
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
%Lea%沒有使用索引掃描
mysql> explain select * from t_test where dname like '%Lea%';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | t_test | ALL | NULL | NULL | NULL | NULL | 4 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
Lea%使用索引掃描
mysql> explain select * from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
| 1 | SIMPLE | t_test | range | idx_test_dname | idx_test_dname | 47 | NULL | 1 | Using where |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+-------------+
1 row in set (0.12 sec)
mysql> explain select dname from t_test where dname like 'Lea%';
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
| 1 | SIMPLE | t_test | index | idx_test_dname | idx_test_dname | 47 | NULL | 4 | Using where; Using index |
+----+-------------+--------+-------+----------------+----------------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2112954/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mysql利用Like支援根據匹配度進行查詢MySql
- mysql like查詢 - 根據多個條件的模糊匹配查詢MySql
- 《MySQL 入門教程》第 09 篇 字串模式匹配MySql字串模式
- Centos5.5中安裝Mysql5.5過程分享CentOSMySql
- Elasticsearch實現Mysql的Like效果ElasticsearchMySql
- MySQL用LIKE特殊字元搜尋MySql字元
- MySQL 中文 like 問題解決MySql
- centos7 RPM MySQL5.5CentOSMySql
- Rust -- 模式與匹配Rust模式
- C# 模式匹配C#模式
- MySQL的create table as 與 like區別MySql
- Oracle 解決like中無法匹配下劃線的問題Oracle
- 聊聊 scala 的模式匹配模式
- Swift中的模式匹配Swift模式
- 字串匹配模式問題字串匹配模式
- Scala模式匹配詳解模式
- Glob Patterns匹配模式使用模式
- MySQL 裡的 find_in_set () 和 in () 和 likeMySql
- MySQL 針對 like 條件的優化MySql優化
- 《MySQL必知必會》萬用字元 ( like , % , _ ,)MySql字元
- scala模式匹配詳細解析模式
- KMP字串模式匹配詳解KMP字串模式
- PostgreSQL:模式匹配操作符SQL模式
- MySQL5.5升級到MySQL5.7踩坑日記MySql
- MySQL like查詢字元轉義遇到的坑MySql字元
- 企業環境下MySQL5.5調優MySql
- KMP模式匹配演算法KMP模式演算法
- 正規表示式支配匹配模式模式
- 一起學Scala 模式匹配模式
- [JS高程] 字串模式匹配方法JS字串模式
- MySQL 使用 like "%x",索引一定會失效嗎?MySql索引
- ubuntu 16.04+nginx+mysql+php7.1+laravel5.5環境UbuntuNginxMySqlPHPLaravel
- MySQL 5.5使用者遷移到5.7使用者MySql
- CentOS安裝MySQL5.5的完整步驟DSITCentOSMySql
- MySql5.5忘記root密碼怎麼辦MySql密碼
- 5.5
- [譯] Swift 中強大的模式匹配Swift模式
- AC 自動機——多模式串匹配模式
- 模式匹配kmp演算法(c++)模式KMP演算法C++