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;