欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

3399 mysql_MySQL索引

发布时间:2025/3/19 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 3399 mysql_MySQL索引 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

索引:类似于书的目录,饭店的菜单,字典的目录

功能:加快数据检索速度,提高效率

缺点:

1)创建和维护索引都需要消耗时间,消耗时间的长短取决于表中数据量的多少

2)会占用磁盘空间

3)更新数据库中的数据时,索引也会更新

什么时候都可以创建索引吗?

不是。对于数据频繁更新的表不适合创建索引。(更新包括insert、update、delete)

索引的分类

单列索引、多列索引、唯一性索引

一、创建索引

1、在创建表时直接创建索引

create table 表名 (字段名 字段类型,...,index [索引名](索引字段列表));

mysql> create table ind1 (id int,name char(10),index (id));

mysql> show create table ind1\G

*************************** 1. row***************************

Table: ind1

Create Table: CREATE TABLE `ind1` (

`id` int(11) DEFAULT NULL,

`name` char(10) DEFAULT NULL,

KEY `id` (`id`)//索引相关的行   KEY 索引名 (索引字段)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

创建索引时不指定名字,那么默认会创建与字段名同名的索引。

2、使用create index命令创建索引

语法:create index 索引名(必有) on 表名(字段名);

mysql> create index ind1_name on ind1(name);

mysql> show create table ind1\G

*************************** 1. row ***************************

Table: ind1

Create Table: CREATE TABLE `ind1` (

`id` int(11) DEFAULT NULL,

`name` char(10) DEFAULT NULL,

KEY `id` (`id`),

KEY `ind1_name` (`name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.00 sec)

3、对于已经存在的表,添加索引

语法:alter table 表名 add index [索引名](字段名);

mysql> alter table score add index s_sno(sno);

唯一性索引: unique index  [索引名](索引字段)

mysql> alter table score add unique index s_sname(sname);

二、查看索引

mysql> show create table score\G

或者

mysql> show index from score\G

三、删除索引

1、drop index 索引名 on 表名;

mysql> drop index s_sno on score;

2、alter table 表名 drop index 索引名;

mysql> alter table score drop index score_sno;

测试:

1、创建表

mysql> create table shop (id int,name varchar(20),price float(10,2),street varchar(20),city varchar(20));

2、写脚本生成插入语句

[~]# vim insert.sh

#!/bin/bash

i=1

while [ $i -le 1000000 ]

do

echo "insert into shop values ($i,‘name$i‘,$i.00,‘street$i‘,‘city$i‘);" >> /tmp/a.sql

echo $i

let i++

done

3、执行脚本生成sql文件

[~]# sh insert.sh   //执行完成再做第4步

4、向数据库中插入数据

mysql> use up1;

Database changed

mysql> source /tmp/a.sql   //比较慢

mysql> select count(*) from up1.shop;  //查看已经向表中插入了多少数据

+----------+

| count(*) |

+----------+

|     3280 |

+----------+

1 row in set (0.45 sec)

5、开启可以查看每个select语句执行时间的功能

mysql> show variables like ‘%profi%‘;

+------------------------+-------+

| Variable_name          | Value |

+------------------------+-------+

| have_profiling         | YES   |

| profiling              | OFF   |

| profiling_history_size | 15    |

+------------------------+-------+

3 rows in set (0.16 sec)

mysql> set profiling=1;    //打开性能分析功能

6、创建一张带索引的表

mysql> create table shop1 (id int,name varchar(20),price float(10,2),street varchar(20),city varchar(20),index(id));

7、复制表shop到shop1

mysql> insert into shop1 select * from shop;

8、对两个表分别执行sql语句

先查看shop表:

mysql> select * from shop;

select id from shop;

select * from shop where id=1;

select id,name from shop where id=1;

select * from shop where id=650000;

select id,name from shop where id=650000;

select id from shop where price=10000.00;

show profiles;

总结

以上是生活随笔为你收集整理的3399 mysql_MySQL索引的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。