Beckham _ mysql index learning and optimization, and Beckham _ mysql Index

Source: Internet
Author: User
Tags split words mysql index

Beckham _ mysql index learning and optimization, and Beckham _ mysql Index
Mysql index and OptimizationBrief:I. What is an index? ii. Index type and syntax. III. Full-text index descriptionI. What is an index?

1. Take the book directory as an example. view the Directory and find the corresponding content. Therefore, the index adds a 'directory' to the data to facilitate quick data discovery.

2. Functions of Indexes:

Benefits: Faster query speed

Disadvantages: a. Reduced the speed of adding, deleting, and modifying

B. Increase the table file size (the index file may even be larger than the data file)

Case: there are 15 columns in a table and 10 columns have indexes. There are rows of data in total. How can we import data quickly?

Answer: 1. delete all the indexes of the empty table. 2. import data. 3. Create an index after the data is imported.

3. index algorithms

There are N random records, no indexes, and an average of N/2 queries. What if an index is used?

3.1. The number of times binary tree indexes correspond to log2N

Example: data 1, 2, 3, 4, 5, 6, and 7, with the center value 4 as the demarcation point

4

2 6

1 3 5 7

How many times does search 3 take?

Because 3 <4, 3 is on the left of the binary tree. Because 3> 2, 2 is on the right of the root node. The result is required twice.

3.2 hash index, theoretically one time

Example: data 1, 2, 3, 4, 5, 6, 7

Hash [1] = 001

Hash [2] = 003

Hash [3] = 005

Hash [4] = 007

Hash [5] = 009

Hash [6] = 011

Hash [7] = 013

How many times does search 3 take?

Hash the file and obtain 005. Exactly once.

Insufficient hash:

A. It is a waste of space because the hash value is not continuous.

B. High hash requirements. Ensure that the hash values of each value are different.

4. indexing principles

A. Excessive Index

B. Index condition columns (the most frequent condition after where is more suitable for indexing)

C. Index hash values. values that are too concentrated cannot be indexed (for example, gender)

5. view the table structure

5.1. The storage engine is myisam.


Frm is the table structure, MYD is the data file, and MYI is the index file.

5.2. The storage engine is innodb


Frm is a table structure, ibd is a data file, and an index file.

 

Ii. Index types and syntax

1. Type

A. Normal index: Just accelerate the query speed

B. unique index: the value on the row cannot be repeated.

C. primary key Index (primary key): it cannot be repeated.

D. Full-text index ):

The relationship between the unique index and the primary key index:

The primary key must be unique, but the unique index is not necessarily the primary key. A table can have only one primary key, but one or more unique indexes.

2. How to view indexes in a table


3. Create an index

3.1 create an index for an existing table

Syntax: alter table name add index/uniqueindex/fulltext index/primary key [index name] (column name) (Note: The index name is optional and is not specified as the same as the column name)

Table Structure:

Create table m (id int, emailvarchar (30), tel char (11), intro text) engine = myisam charset = utf8;


A. Create a common index for the tel Column


(Note: The specified index name is the same as the column name)


B. Add a unique index to the email Column


C. Add full-text indexes to the intro Column

D. Add a primary key index to the id column.

E. Add a composite index of multiple columns


(Note: This general index m applies to the email and tel columns)


Error:

Cause of error: the column where the index is applied is not specified.

3.2 specify the index when creating a new table

Create table m (id int primary keyauto_increment, email varchar (30), tel char (11), intro text, index (tel), uniqueindex (email), fulltext index (intro )) engine = myisam charset = utf8;

 

4. delete an index

4.1 Delete common index/unique index/full-text index

4.2 Delete the primary key index

If the primary key column itself is auto-incrementing, an error will be reported during deletion.


In this case, you should first modify the column auto-increment attribute.

 

Iii. Full-text index

Full-text index is of little significance to Chinese characters by default in mysql.

There are spaces and punctuation marks in English to split words into indexes.

For Chinese, there is no space to separate words. Mysql cannot recognize every Chinese word.

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


The quieter you become, the more you are able to hear!

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.