Database Tuning Tutorials (v) The role of indexes and the types of indexes

Source: Internet
Author: User

Third, index


In the previous chapter we learned to use explain to generate a query execution plan (QEP) to find the problem with the statement. Of the results returned by explain, three are index-related (possible key, key, extra), and the visible index has a prominent position in improving query efficiency.

In this chapter, we will explain in all directions how to use the index to optimize our database.

1. Role of the Index

The principle of indexing is to use special search algorithms (such as binary tree algorithm) to limit the number of rows accessed and improve access efficiency.

In addition, indexes can efficiently establish connection operations between tables, efficiently sort the results, and so on.

2. Basic index type and creation

Indexes can be divided into primary key indexes, normal indexes, unique indexes, and full-text indexes.

1) Primary key index

The column is the primary key index when a table is set to a column as the main key

CREATE TABLE AAA (ID int unsigned primary key auto_increment, name varchar (+) NOT null defaul ');

This is the ID column, which is the primary key index.

If you do not specify a primary key index when you create the table, you can also add the table after you create it

ALTER TABLE name add primary key (column name);

2) General Index

In general, the creation of a normal index is to create a table first, and then create a normal index

Create index index name on table (column name 1, column name 2);

When there are more than one column in parentheses, we call it a multi-column index, which in some cases can be translated into an overwrite index (described later).

Here we can see the power of the index by creating an ordinary index in the EMP table.

Before the index is created

We execute the following statement

Select COUNT (1) from the EMP where ename like ' yu% ';


Then we create the index

CREATE INDEX Index_ename on EMP (ename);


Execute again

Select COUNT (1) from the EMP where ename like ' yu% ';


Incredible!

The power of the index is visible!

Of course, indexes are expensive.

This is the size of the data/bigtable/emp.ibd file in the MySQL installation directory when I did not create the index, and for the InnoDB engine table, the IBD file holds the table's data and indexes.


After I've created the index


Obviously, the index occupies the host's space, which is also known as the cost of the index.

3) Unique Index

When a column of a table is specified as a unique constraint, this column is a unique index
CREATE TABLE DDD (ID int primary key auto_increment, name varchar (+) unique);

At this point, the Name column is a unique index

After creating the table, go to create a unique index

Create unique index index name on  table name (list:);
The unique field can be null and can have multiple null, that is, null! =null. However, if the content is specific, you cannot repeat

4) Full-text indexing

Full-text indexing, which is useful only for the MyISAM engine. Mainly for the document, text retrieval, such as articles or paragraphs,.

It generates a list of all the words that appear in one of the data columns of a data table

Creation of full-text indexes

Create a table when you create it

CREATE TABLE articles (       ID INT UNSIGNED auto_increment not NULL PRIMARY KEY,       title VARCHAR (+),       body Text,
   fulltext (title,body)     ) Engine=myisam charset UTF8;

Or, after you create the table, add

ALTER TABLE Articles ADD fulltext (title,body);

The correct usage for full-text indexing is:

SELECT * from articles where match (title,body) against (' words to search ');

In addition, MySQL's own fulltext for English, want to search Chinese need to use the Sphinx full-Text search engine or use the enhanced version of the fuzzy query.

Attention:

Full-text indexes are not exactly equivalent to fuzzy queries

For example, the title field has this data ' ABCD20088CCAA ', using the fuzzy query select * FROM Articleswhere the title like '%2008% ' can be found, while using full-text Search select * FROM Articleswhere Match (title) against (' 2008 '); is not retrieved because 2008 is not a word!


At the end of this lecture, the next lecture will explain the related operations of the index.




Database Tuning Tutorials (v) The role of indexes and the types of indexes

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.