create table staffs( id int primary key auto_increment, name varchar(24) not null default ‘‘ comment ‘姓名‘, age int not null default 0 comment ‘年龄‘, pos varchar(20) not null default ‘‘ comment ‘职位‘, add_time timestamp not null default current_timestamp comment ‘入职时间‘ )charset utf8 comment ‘员工记录表‘; insert into staffs(name,age,pos,add_time) values (‘z3‘,‘22‘,‘manager‘,now()); insert into staffs(name,age,pos,add_time) values (‘z2‘,‘23‘,‘dev‘,now()); insert into staffs(name,age,pos,add_time) values (‘2000‘,‘21‘,‘dev‘,now()); Query OK, 1 row affected (0.15 sec) alter table staffs add index idx_staffs_nameAgePos(name,age,pos); mysql> select *from staffs; +----+------+-----+---------+---------------------+ | id | name | age | pos | add_time | +----+------+-----+---------+---------------------+ | 1 | z3 | 22 | manager | 2019-08-12 23:06:53 | | 2 | z2 | 23 | dev | 2019-08-12 23:07:35 | | 3 | 2000 | 21 | dev | 2019-08-12 23:07:58 | +----+------+-----+---------+---------------------+
#建立几个复合索引字段,最好就用上几个字段。且按照顺序来用。
#最前缀法则,必须有车头,中间车厢不能断
原文:https://www.cnblogs.com/pdun/p/11343318.html