mysql优化一:大数据查询添加索引

一、索引是什么?

索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。

更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度

二、索引目的

索引的目的在于提高查询效率

三、索引原理

通过不断的缩小想要获得数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是我们总是通过同一种查找方式来锁定数据。

四、索引的使用

1、查看索引

show index from 表名;

2、创建索引

扫描二维码关注公众号,回复: 4327213 查看本文章
create index 索引名称 on 表名(字段名称(长度))

3、删除索引:

drop index 索引名 on 表名;

五、索引demo:

1、创建测试表test

create table test(
    -> id int not null primary key auto_increment,
    -> name varchar(20)
    -> );

2、插入10万条语句:

    def insert(self):
        for i in range(1000000):
            self.cursor.execute('insert into test values(0,"this is % d")' % i)
        self.conn.commit()

3、查询

3.1 开启运行时间监测:

set profiling=1;

3.2 查找第1万条数据

select * from test where title = 'this is  99999';

3.3 查看执行的时间

show profiles;

3.4 为表title的name字段创建索引

create index test on test(name(20));

3.5 执行查询语句

select * from test where name = 'this is  99999';

3.6 再次查看执行的时间

show profiles;

五、查询方式

1、开启运行时间监测:

set profiling=1;

2、查找第一万条数据ha-99999

select * from test_index where title='ha-99999';

3、查看

猜你喜欢

转载自blog.csdn.net/weixin_42670402/article/details/83419220