The basic concept of MySQL index

1. What is an index?

For example, if we want to find a word in a dictionary, how can we find it quickly? That is through the directory of dictionaries.

For the database, the role of the index is to add a directory to the 'data'.  

 

Second, the index algorithm

There are N random records, no index is used, and the average search is N/2 times, then what about after the index is used?

tree (binary tree) index log2N

hash index 1

 

3. Advantages and disadvantages

Advantages: Speeds up queries (select)

Disadvantage: Reduced the speed of additions, deletions, and changes (update/delete/insert)

        Increased table file size (index files may even be larger than data files)

 

Fourth, the principle of using the index

But you have to use an index;

Index condition column (the most frequent condition after where is more suitable for indexing)

Index the hash value, do not index the value that is too concentrated

For example: indexing the gender columns 'male', 'female' is of little significance

 

5. Index Type

1. Ordinary index: just speed up the query

2. Unique Index: Values ​​on a row cannot be repeated

3. Primary key index: cannot be repeated

The difference between primary key index and unique index: the primary key must be unique, but the unique index is not necessarily the primary key;

There can only be one primary key on a table, but one or more unique indexes

4. Full-text index: fulltext index

 

6. View all indexes on a table

show index from tableName [\G, if it is in the cmd window, you can wrap it];

 

7. Create an index

alter table table name add index/unique/fulltext [index name] (column name) ; ---The index name can be omitted, and the column name is used by default if not written

alter table table name add primary key (column name) -- do not add the index name, because there is only one primary key

 

Eight, delete the index

delete non-primary key index

alter table table name drop index index name;

Delete the primary key index:

alter table 表名 drop primary key;

 

9. Cases

There is a news table with 15 columns, 10 columns have indexes, a total of 500w rows of data, how to import quickly?

1. Delete all the indexes of the empty table

2. Import data

3. After the data is imported, centrally build the index

 

10. Full-text indexing and stop words

Full-text index usage:

match(full-text index name) against('keyword');

 

About stop words for full-text indexing:

Full-text indexing does not index very frequent words

Such as: this, is, you, my, etc.

 

The full-text index is of little significance for Chinese by default in mysql.

Because English has spaces, punctuation marks are split into words, and then words are indexed;

And for Chinese, there is no space to separate words, mysql can't recognize every Chinese word.

You can use the sphinx plugin to perform Chinese indexing of full-text indexing.

Reprinted from: https://blog.csdn.net/qq_15766181/article/details/47382415

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325294479&siteId=291194637