一 索引的创建

 索引减慢了 写的操作,优化了读取的时间

 index:普通索引,加速了查找的时间。

 fulltext:全文索引,可以选用占用空间非常大的文本信息的字段作为索引的字段。使用fulltext时需要借助第三方的软件sphinx专用去那问搜索。

 创建格式,创建表时加上索引:

  create table 表名(字段1 类型 约束键,。。。。。);

  create table 表名(字段1 类型,。。。。,约束键)

mysql> create table t2(id int primary key auto_increment,
-> name char(15) not null unique);
Query OK, 0 rows affected (0.24 sec) mysql> desc t2;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(15) | NO | UNI | NULL | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.01 sec) mysql> create table t3(id int,
-> name char(15),
-> index idx_id(id));
Query OK, 0 rows affected (0.24 sec) mysql> desc t3;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

 常用的索引约束键有:primary key    unique key,

   普通的索引有:index

 创建表后指定字段为索引字段:

  create index 起名 on 表名(字段名);

  alter table 表名 add index 起名(字段名);mysql

mysql> create table t4(id int ,
-> name char(15));
Query OK, 0 rows affected (0.25 sec) mysql> create table t5(id int,
-> name char(15));
Query OK, 0 rows affected (0.33 sec) mysql> desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.03 sec) mysql> create index idx_id on t4(id);
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec) mysql> alter table t5 add index idx_id(id);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t5;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

 删除索引:

  drop index 索引名 on 表名

  删除主键:alter table 表名 drop primary key;

mysql> desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec) mysql> drop index idx_id on t4;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(15) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+

二 测试

 比较符号的测试:<   >   =   !=  >=  <=

mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> select count(*) from s1 where id=370000;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.17 sec) mysql> create index idx_id on s1(id);
Query OK, 0 rows affected (1.17 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> select count(*) from s1 where id=370000;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where id>370000;
+----------+
| count(*) |
+----------+
| 6220 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where id<370000;
+----------+
| count(*) |
+----------+
| 369999 |
+----------+
1 row in set (0.21 sec) mysql> select count(*) from s1 where id!=370000;
+----------+
| count(*) |
+----------+
| 376219 |
+----------+
1 row in set (0.20 sec) mysql> drop index idx_id on s1;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> create index idx_name on s1(name);
Query OK, 0 rows affected (2.19 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> select count(*) from s1 where name='egon';
+----------+
| count(*) |
+----------+
| 376220 |
+----------+
1 row in set (0.25 sec) mysql> select count(*) from s1 where name='xxxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)

注意事项:插入索引过和如果添加记录会改变树形的结构。

  如果一个字段的重复率过高,如果条件成立,反而会拖慢查询的效率。如果条件不成立,查询时间会非常的块

  尽量选择区分度较高的字段作为索引的字段。一般是在十条记录中有一条重复。

  索引的字段不能够参与计算中,因为这样条件就不会明确,所以也要从第一条记录开始计算,这样就增大了查找的范围。这样也会非常耗时间的。

 逻辑符号的测试:  and  or  not

mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> create index idx_id on s1(id);
Query OK, 0 rows affected (1.69 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> select * from s1 where id=370000 and name='egon';
+--------+------+--------+-------------------+
| id | name | gender | email |
+--------+------+--------+-------------------+
| 370000 | egon | male | egon370000@oldboy |
+--------+------+--------+-------------------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon' and id=370000;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon' or id=370000;
+----------+
| count(*) |
+----------+
| 376220 |
+----------+
1 row in set (0.83 sec) mysql> select count(*) from s1 where id=370000 or name='egon';
+----------+
| count(*) |
+----------+
| 376220 |
+----------+
1 row in set (0.79 sec)

 在and的查找中:从左到右首先找到索引区分高的索引字段,如果条件成立,在去按照索引的字段去查找,如果不成立,就不会再去查找。and前面查找的条件如果不成立,那么查找的顺序就会非常的块,如果and前面的索引字段区分度低或者查找的范围大,同样也是耗费时间的,查找效率也会很低的。

 在or的查找中:从左到右一次查找记录,如果前面的条件成立,就不会查找后面的条件,如果前面的条件不成立,才会查找后面的条件。如果查找的索引字段区分度低,还是会拉低查找效率的,如果查找的索引字段区分度高的话,那就要看查找的范围,如果查找的范围过大,效率还是不会提升的,如果明确一个条件,或着范围小,这样就会提升查找的效率。

 范围的比较:in   between  like

mysql> select count(*) from s1 where id in (213,54343,354544);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where id between 100 and 3000;
+----------+
| count(*) |
+----------+
| 2901 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where id between 100 and 370000;
+----------+
| count(*) |
+----------+
| 369901 |
+----------+
1 row in set (0.19 sec) mysql> select count(*) from s1 where id not in (1232,3423,24324);
+----------+
| count(*) |
+----------+
| 376217 |
+----------+
1 row in set (0.20 sec)

 联合索引的测试:and  or

  联合索引就是将需要判断的字段联合起来创建一个索引。联合索引都是后面的字段联合第一个的。

mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> create index idx_xxx on s1(name,gender,id,email);
Query OK, 0 rows affected (3.19 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc s1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| gender | char(6) | YES | | NULL | |
| email | varchar(50) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec) mysql> select count(*) from s1 where name='egon' and gender='male' and id>3000 and email='xxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.49 sec) mysql> select count(*) from s1 where name='egon' and gender='male' and email='xxx' and id>3000;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.49 sec) mysql> select count(*) from s1 where name='egon' and gender='male' and email='xxx' and id=3000;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon' and gender='male' and id=3000;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon' and id=370000 and gender='male';
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec) mysql> select count(*) from s1 where name='egon' and gender='male' and email='xxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.46 sec) mysql> select count(*) from s1 where name='egon' and id=370000 and email='xxx';
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.30 sec)

 注意:如果联合索引的字段条件没有连续出现的话也会拖慢速度的。

  什么叫做最左前缀:联合索引的查找顺序是从左到右依次查找,如果是and,查找出区分度高的索引字段先执行,如果条件成立在执行其他的条件,如果不成立,就不会执行其他的条件;如果是or,从左到右依次执行,如果条件成立就不会在去执行其他的索引条件。

 注意事项:应该将明确的条件字段放在右范围的条件字段的前面。

      排序字段必须是索引字段。

      对于联合索引,只要有第一个联合索引的字段,就会起作用,如果没有,就不会有效果。

 覆盖索引只要有联合索引的第一个字段就可以使用

 合并索引:创建多个单列索引,他们可以合并的使用,也可以单个使用。

详细资料访问:http://www.cnblogs.com/linhaifeng/articles/7274563.html#_label7

mysql之索引查询2的更多相关文章

  1. MySQL多索引查询选择

    MySQL多索引查询选择 MySQL选择索引-引入 我们知道我们一个表里面可以有多个索引的,那么我们查询数据的时候不指定索引,MySQL就会帮我们自动选择.既然是MySQL程序帮我们自动选择的那么会不 ...

  2. mysql之索引查询1

    一 备份数据 备份库: mysqldump:拷贝数据 --database:数据库 基本语法是:mysqldump -h服务器名 -u用户名 -p密码 --database 库名 > 备份路径. ...

  3. MySQL数据库索引:索引介绍和使用原则

    本篇目录: 一.数据页与索引页 二.聚簇索引与非聚簇索引 三.唯一索引 四.索引的创建 五.索引的使用规则 六.数据库索引失效情况 本篇正文: 一.数据页与索引页 数据库的表存储分为数据页存储和索引页 ...

  4. mysql使用索引优化查询效率

    索引的概念 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度.在没 ...

  5. MySQL多表查询之外键、表连接、子查询、索引

    MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...

  6. Mysql 版本号、存储引擎、索引查询

    [1]Mysql 版本号.存储引擎.索引查询 # 查看数据库版本号 SELECT VERSION(); # 查看数据库支持的引擎(默认即Support == DEFAULT行) SHOW ENGINE ...

  7. 第二百八十八节,MySQL数据库-索引、limit分页、执行计划、慢日志查询

    MySQL数据库-索引.limit分页.执行计划.慢日志查询 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获 ...

  8. MySQL索引查询原理

    什么是索引? “索引”是为了能够更快地查询数据.比如一本书的目录,就是这本书的内容的索引,读者可以通过在目录中快速查找自己想要的内容,然后根据页码去找到具体的章节. 数据库也是一样,如果查询语句使用到 ...

  9. mysql in条件查询到底会不会用到索引

    MySQL 的 in 查询在 5.5 以上的版本中存储引擎都是 innodb 的,正常情况下会走索引的!至于 MyISAM 没试过! 如果是 5.5 之前的版本确实不会走索引的,在 5.5 之后的版本 ...

随机推荐

  1. 侯捷STL课程及源码剖析学习3: 深度探索容器list

    一.容器概览 上图为 GI STL 2.9的各种容器.图中以内缩方式来表达基层与衍生层的关系.所谓的衍生,并非继承(inheritance)关系,而是内含(containment)关系.例如 heap ...

  2. Django项目之客户

    关于客户的操作 主页(被继承) {% load static %} <!DOCTYPE html> <html lang="en"> <head> ...

  3. RxJS之Subject主题 ( Angular环境 )

    一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...

  4. 25 【python入门指南】如何编写测试代码

    python如何编写测试代码 python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单. 1,执行单个测试函数 #!/bin/python import unittest clas ...

  5. TZOJ 1221 Tempter of the Bone(回溯+剪枝)

    描述 The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked i ...

  6. C++ 求最长递增子序列(动态规划)

    i 0 1 2 3 4 5 6 7 8 a[i] 1 4 7 2 5 8 3 6 9 lis[i] 1 2 3 2 3 4 3 4 5 时间复杂度为n^2的算法: //求最长递增子序列 //2019/ ...

  7. Linux下的crontab定时执行任务详解

    在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“cr ...

  8. Django 实现登陆验证码

    一 基本使用方法 Python生成随机验证码,需要使用PIL模块 安装: pip3 install pillow 基本使用 1 创建图片 from PIL import Image, ImageDra ...

  9. mysql中如何使用一句话将一个表的数据导入到另一个表中:insert into ...select

    INSERT INTO cdcd2015(`filename`,`xmlfile`,`updatetime`,`isDel`) SELECT `filename`,`xmlfile`,`updatet ...

  10. 关于Vector,map等迭代器问题

    vector.erase(it):后,it自动++,一定要弄清楚,删除成功后it指向删除的下一个地址. 对于map.erase(it),返回值为NULL,而Vector是返回itorator