myql深度学习7-索引

1、将一个表的查询的结果插入到另一个表中

insert into table_name1 (column_list1) select (column_list2) from table_name2 where (condition)

2、索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可以提高数据库中特定的数据的查询速度。索引是一个单独的存储在磁盘上的数据库结构,包含的这对数据表的所有记录的引用指针。
缺点:
①创建索引和维护索引要耗费时间,并且随着数据量的增加所耗费的时间也会增加。
②占据磁盘空间。
③对数据表走红的数据进行增加、删除和修改的时候索引要动态维护,降低了数据的维护速度。

3、创建普通索引 使用index或key关键字。其中possible_keys表示可以选用的关键字,key表示的是实际使用的关键字。

create table fruit(
id int(11) auto_increment,
name varchar(50) not null,
price DECIMAL(8,2) not null,
city VARCHAR(100) not null,
primary key(id),
index(city)
)
然后通过explain进行查看 索引的情况,此时的key为city

explain select * from fruit where city = ‘beijing’

若使用下面的查询条件,其key值为空

explain select * from fruit where name = ‘mei’

4、创建唯一索引,唯一索引的和普通索引类似,不同的是索引列的值必须是唯一,但允许有空值。如果是组合索引,则列值得组合必须唯一。 使用unique index关键字,并且为id起了别名UniqIdx。

create table newfruit(
id int not null,
name varchar(50) not null,
price DECIMAL(8,2) not null,
city varchar(100) not null,
UNIQUE INDEX UniqIdx(id)
)

5、创建多列索引,当查询条件至少包含id时才会利用到索引,而查询条件中单独使用name和price的时候不会利用到索引。

create table newfruit2(
id int not null,
name varchar(50) not null,
price DECIMAL(8,2) not null,
city varchar(100) not null,
INDEX Mmindex(id,name,price)
)

6、删除索引

drop index_name on table_name

alter table table_name drop index index_name

你可能感兴趣的:(MySQL)