MySQL Advanced (VII): index creation SQL statement, verify that the index query performance advantages, and disadvantages of MySQL index and use the principle of

index

learning target

  • Able to write SQL statements to create the index

1. Introduction Index

In MySQL index, also known as "key", which is a special file that holds the position information data table of all records, more popular, the database index is like a book in front of the catalog, you can speed up database query speed.

Scenario:

When a large amount of data in the database, to find data will become very slow, we can improve the efficiency of database queries through the index.

2. The use of the index

View the table have been indexed:

show index from 表名;

Description:

  • Primary key column will automatically create an index

Index creation:

-- 创建索引的语法格式
-- alter table 表名 add index 索引名[可选](列名, ..)
-- 给name字段添加索引
alter table classes add index my_name (name);

Description:

  • Index name does not know, use the default field names

Delete the index:

-- 删除索引的语法格式
-- alter table 表名 drop index 索引名
-- 如果不知道索引名,可以查看创表sql语句
show create table classes;
alter table classes drop index my_name;

3. Case - Verify query performance index

Create a test table testindex:

create table test_index(title varchar(10));

One hundred thousand inserting data into the table:

from pymysql import connect

def main():
    # 创建Connection连接
    conn = connect(host='localhost',port=3306,database='python',user='root',password='mysql',charset='utf8')
    # 获得Cursor对象
    cursor = conn.cursor()
    # 插入10万次数据
    for i in range(100000):
        cursor.execute("insert into test_index values('ha-%d')" % i)
    # 提交数据
    conn.commit()

if __name__ == "__main__":
    main()

Verify Operation Performance Index:

-- 开启运行时间监测:
set profiling=1;
-- 查找第1万条数据ha-99999
select * from test_index where title='ha-99999';
-- 查看执行的时间:
show profiles;
-- 给title字段创建索引:
alter table test_index add index (title);
-- 再次执行查询语句
select * from test_index where title='ha-99999';
-- 再次查看执行的时间
show profiles;

4. The joint index

Called joint index composite index, i.e. an index table covers two or more fields, is generally used when the query with a plurality of fields.

-- 创建teacher表
create table teacher
(
    id int not null primary key auto_increment,
    name varchar(10),
    age int
);

-- 创建联合索引
alter table teacher add index (name,age);

Joint index benefits:

  • Reduce the cost of disk space, because each create an index, in fact, is to create an index file, it will increase the cost of disk space.

5. leftmost principle of joint index

When used in conjunction index, we have to comply with one of the most left-principle that index (name, age) to support name, name and age combined query, the query does not support a separate age, because the joint is not used to create the index.

Example leftmost principles:

-- 下面的查询使用到了联合索引
select * from stu where name='张三' -- 这里使用了联合索引的name部分
select * from stu where name='李四' and age=10 -- 这里完整的使用联合索引,包括 name 和 age 部分 
-- 下面的查询没有使用到联合索引
select * from stu where age=10 -- 因为联合索引里面没有这个组合,只有 name | name age 这两种组合

Description:

  • In the query data when used in conjunction index must ensure that the joint index of the left-most field conditions which appear in the query, or joint index fail

6. The principles and use of the advantages and disadvantages of the index MySQL

  • advantage:
    1. Speed ​​up queries data
  • Disadvantages:
    1. It takes time to create an index and take up disk space, and with the increasing amount of data will increase the time it takes
  • Use principles:
    1. By comparing the advantages and disadvantages, not index the better, but needs its own rational use.
    2. Regularly updated list of them to avoid creating too many indexes on a field often used to query the index should be created,
    3. A small amount of data tables is best not to use the index, because due to less data, all data may query time even shorter than the time it takes to traverse the index, the index may not produce optimal performance.
    4. In a field on the same value more not to index, such as only men and women of two different values ​​on the "gender" student table fields. Instead, in a different field, but more value index.

7. Summary

  • The index is a means to speed up queries database
  • Create an index using: alter table add index table index name [optional] (field, xxx);
  • Remove the index using: alter table table name drop index index name;
Published 729 original articles · won praise 964 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104828861