alter table test add index index_name(name); alter table test drop index idx_name;
create index index_name on test(name); drop index idx_stu_name on t1;
mysql> desc t1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(5) | YES | | NULL | | | stu_name | varchar(20) | YES | MUL | NULL | | | age | int(3) | YES | | 28 | | +----------+-------------+------+-----+---------+-------+
mysql> show index from t1\G; *************************** 1. row *************************** Table: t1 Non_unique: 1 Key_name: idx_stu_name Seq_in_index: 1 Column_name: stu_name Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec)
第二种方式的好处就是能够查看到索引的名称,索引的类型为BTREE索引,便于删除索引时指定删除哪一个索引。javascript
mysql> select * from t1; +------+----------+------+ | id | stu_name | age | +------+----------+------+ | 1 | tom | 28 | | 2 | liliy | 28 | | 3 | lucy | 28 | | 4 | lintao | 28 | | 5 | alex | 28 | +------+----------+------+ 5 rows in set (0.00 sec) mysql> show index from t1\G; *************************** 1. row *************************** Table: t1 Non_unique: 1 Key_name: idx_stu_name Seq_in_index: 1 Column_name: stu_name Collation: A Cardinality: 5 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec) # 只有stu_name这一列有索引 mysql> explain select * from t1 where id=4; +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 5 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> explain select * from t1 where stu_name='lintao'; +----+-------------+-------+------+---------------+--------------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+--------------+---------+-------+------+-----------------------+ | 1 | SIMPLE | t1 | ref | idx_stu_name | idx_stu_name | 63 | const | 1 | Using index condition | +----+-------------+-------+------+---------------+--------------+---------+-------+------+-----------------------+ 1 row in set (0.00 sec)
由上述可知因为t1表只有一个stu_name列由索引,用id来查询的属于全表扫描,由于类型为:ALL,而用stu_name查找时则命中了索引,类型为: refjava
CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE `test` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=UTF8; # 增长自增主键 alter table test change id id int(4) primary key not null auto_increment;
create index index_name on test(name); alter table test add index index_name(name);
用了索引,查一堆内容。mysql
在where条件关键字后面的列创建索引才会加快查询速度.sql
select id,name from test where state=1 order by id group by name;
create unique index index_name on test(name);
create index index_name on test(name(8));
alter table test add index union_idx_name(name, age, gender);
PRIMARY KEY (`Host`,`User`) alter table test add sex char(4) not null; create index ind_name_sex on test(name,sex);
create index index_name on test(name(8),sex(2));
select count(distinct user) from mysql.user; select count(distinct user,host) from mysql.user;