/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50022
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50022
File Encoding : 65001
Date: 2020-03-13 16:57:10
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `employees`
-- ----------------------------
DROP TABLE IF EXISTS `employees`;
CREATE TABLE `employees` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(24) NOT NULL,
`age` int(11) unsigned zerofill NOT NULL default '00000000000',
`position` varchar(20) NOT NULL,
`hire_time` timestamp NOT NULL default '0000-00-00 00:00:00' on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_name_age_position` (`name`,`age`,`position`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of employees
-- ----------------------------
INSERT INTO `employees` VALUES ('1', 'LiLei', '00000000022', 'mana ger', '0000-00-00 00:00:00');
INSERT INTO `employees` VALUES ('2', 'HanMeimei', '00000000023', 'dev', '2020-03-13 15:33:11');
INSERT INTO `employees` VALUES ('3', 'Lucy', '00000000023', 'dev', '0000-00-00 00:00:00');
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------+------+-------------+
| 1 | SIMPLE | employees | ref | idx_name_age_position | idx_name_age_position | 74 | const | 1 | Using where |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------+------+-------------+
1 row in set
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
| 1 | SIMPLE | employees | ref | idx_name_age_position | idx_name_age_position | 78 | const,const | 1 | Using where |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------+------+-------------+
1 row in set
mysql> EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 22 AND position ='manager';
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
| 1 | SIMPLE | employees | ref | idx_name_age_position | idx_name_age_position | 140 | const,const,const | 1 | Using where |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
1 row in set
mysql> EXPLAIN SELECT name,age FROM employees WHERE name= 'LiLei' AND age = 23 AND position ='manager';
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+--------------------------+
| 1 | SIMPLE | employees | ref | idx_name_age_position | idx_name_age_position | 140 | const,const,const | 1 | Using where; Using index |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+--------------------------+
1 row in set
mysql> EXPLAIN SELECT * FROM employees WHERE name= 'LiLei' AND age = 23 AND position ='manager';
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
| 1 | SIMPLE | employees | ref | idx_name_age_position | idx_name_age_position | 140 | const,const,const | 1 | Using where |
+----+-------------+-----------+------+-----------------------+-----------------------+---------+-------------------+------+-------------+
1 row in set
mysql> EXPLAIN SELECT * FROM employees WHERE name is null;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
1 row in set
mysql> EXPLAIN SELECT * FROM employees WHERE name is not
null;
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
| 1 | SIMPLE | employees | ALL | idx_name_age_position | NULL | NULL | NULL | 3 | Using where |
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
1 row in set
mysql>
8..like以通配符开头('$abc...')mysql索引失效会变成全表扫描操作
b)如果不能使用覆盖索引则可能需要借助搜索引擎
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
| 1 | SIMPLE | employees | ALL | idx_name_age_position | NULL | NULL | NULL | 3 | Using where |
+----+-------------+-----------+------+-----------------------+------+---------+------+------+-------------+
1 row in set
mysql> ALTER TABLE `employees` ADD INDEX `idx_age` (`age`) ;
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0
mysql> explain select * from employees where age >=1 and age <=2000;
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | employees | range | idx_age | idx_age | 4 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+---------+---------+------+------+-------------+
1 row in set
mysql> ALTER TABLE `employees`
DROP INDEX `idx_age`;
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0