Application of mysql joint index

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to enter a log table with the following structure: CREATETABLE 'weblog '('id' int (10) unsignedNOTNULLAUTO_INCREMENT, 'IP' varchar (45) NOTNULL, 'time' timestampNOTNULLDEFAULTCURRENT _ TIMESTAMPONUPDA

Welcome to the Linux community forum and interact with 2 million technical staff> enter a log TABLE with the following structure: create table 'weblog '('id' int (10) unsigned not null AUTO_INCREMENT, 'IP' varchar (45) not null, 'time' timestamp not null default CURRENT_TIMESTAMP ON UPDA

Welcome to the Linux community forum and interact with 2 million technicians>

There is a log table with the following structure:

Create table 'weblog '(

'Id' int (10) unsigned not null AUTO_INCREMENT,

'IP' varchar (45) not null,

'Time' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

'Kind' varchar (255) not null,

Primary key ('id ')

) ENGINE = MyISAM AUTO_INCREMENT = 48024 default charset = utf8;

0.11 million records

Select count (times1) as times from (SELECT count (id) as times1 FROM weblog group by ip, kind) as

It takes 1.9 seconds to execute this query, and some of them cannot stand it. Later I found that it would take seconds to add a composite index.

Alter table weblog add INDEX 'sindex '('IP', 'kind ')

Then, the query will be reduced to 0.182 seconds.

The optimization is very obvious.

However, if

Alter table weblog add INDEX 'sindex '('IP', 'kind', 'id ')

The query slows down.

Select count (times1) as times from (SELECT count (id) as times1 FROM weblog group by kind) as

It takes 1.8025 seconds for me to optimize the joint query.

Alter table weblog add INDEX 'sindex '('kind', 'id') after Union INDEX

This time changes to 0.143 seconds.

The optimization is also very obvious.

If a group by clause is similar to a group by clause, it can improve the efficiency.

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.