Database study notes (seven, index features)

To improve the performance of the database, the index is a cheap thing. No need to add memory, no need to change the program, no need to adjust sql, as long as the correct create index is executed, the query speed may be increased by hundreds of times. But there is no free lunch in the world. The increase in query speed comes at the cost of insert, update, and delete speed. These write operations increase a lot of IO. So its value lies in improving the retrieval speed of a massive amount of data.
Common indexes are divided into:

  • Primary key index (primary key)
  • Unique index (unique)
  • Ordinary index (index)
  • Full text index (fulltext)-solve the problem of neutron text index.
    1. Index principle:
    Database study notes (seven, index features)

Index description:

  • Indexes occupy disk space.
  • When adding a record, in addition to adding to the table, but also to maintain the binary tree, the speed has an impact, but not much.
  • When we add an index, we cannot solve all query problems, and we need to index the fields separately.
  • Index is based on space for time.

2. Create index
1. Primary key index
1) When creating the table, specify the primary key directly after the field name

create table user1(id int primary key, name varchar(30));

2) At the end of creating the table, specify a column or columns as the primary key index

create table user2(id int, name varchar(30), primary key(id));
create table user3(id int, name varchar(30)); 
创建表以后再添加主键 
alter table user3 add primary key(id); 

The characteristics of the primary key index:

  • In a table, there is at most one primary key index, of course, you can make it consistent with the primary key
  • High efficiency of primary key index (primary key cannot be repeated)
  • Create the column of the primary key index, its value cannot be null, and cannot be repeated
  • The columns of the primary key index are basically int

2. Unique key index
1) When the table is defined, specify the unique attribute directly after a certain column.

create table user4(id int primary key, name varchar(30) unique);

2) When creating a table, specify a column or a few columns as unique after the table

create table user5(id int primary key, name varchar(30), unique(name));

3)

create table user6(id int primary key, name varchar(30)); 
alter table user6 add unique(name);

Features of unique index:

  • There can be multiple unique indexes in a table
  • High query efficiency
  • If you create a unique index on a column, you must ensure that this column cannot have duplicate data
  • If not null is specified on a unique index, it is equivalent to the primary key index

Three, ordinary index creation
1)

create table user8(id int primary key, 
name varchar(20), email varchar(30), 
index(name) --在表的定义最后,指定某列为索引 
);

2)

create table user9(id int primary key, name varchar(20), email varchar(30)); 
alter table user9 add index(name); --创建完表以后指定某列为普通索引

3)

create table user10(id int primary key, name varchar(20), email varchar(30)); 
-- 创建一个索引名为 idx_name 的索引 
create index idx_name on user10(name);

The characteristics of ordinary index:

  • There can be multiple ordinary indexes in a table, and ordinary indexes are more used in actual development
  • If a column needs to create an index, but the column has duplicate values, then we should use a normal index

4. Full-text index
When searching for article fields or fields with a large amount of text, the full-text index will be used. MySQL provides a full-text indexing mechanism, but there are requirements that the storage engine of the table must be MyISAM, and the default full-text index supports English, not Chinese. For full-text search in Chinese, you can use the Chinese version of Sphinx (coreseek).

CREATE TABLE articles ( 
            id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, 
            title VARCHAR(200), 
            body TEXT, 
            FULLTEXT (title,body) 
)engine=MyISAM;

3. Query index
1) show keys from table name;
2) show index from table name;
3) (information is abbreviated) desc table name;
4. delete index
1) delete primary key index: alter table table name drop primary key;
2 ) Delete other indexes: alter table table name drop index index name; index name is the Key_name field in show keys from table name

mysql> alter table user10 drop index idx_name;

3) drop index index name on table name

mysql> drop index name on user8;

5. The principle of creating an index

  • Fields that are frequently used as query conditions should be indexed
  • Fields with poor uniqueness are not suitable for creating indexes separately, even if they are frequently used as query conditions
  • Fields that are updated very frequently are not suitable for index creation
  • Fields that do not appear in the where clause should not be indexed

Guess you like

Origin blog.51cto.com/14289397/2545138