Example analysis of the impact of MySQL index on sorting

Example analysis of the impact of MySQL index on sorting

This article uses examples to illustrate the impact of MySQL indexes on sorting. Share with you for your reference, the details are as follows:

Indexes can not only improve query speed, but also increase sorting speed. If the statement after order by uses the index, the sorting speed will be improved.

test

1. Create a test table: t15 table

CREATE TABLE `t15` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `cat_id` int(10) unsigned NOT NULL DEFAULT '0',
 `price` decimal(10,2) NOT NULL DEFAULT '0.00',
 `name` char(5) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2. Insert 10,000 rows of data

<?php
$db = 'test';
$conn = mysql_connect('localhost','root','1234');
mysql_query('use ' . $db , $conn);
mysql_query('set names utf8' , $conn);
for($i=1;$i<=10000;$i++) {
  $cat_id = rand(1,10);
  $price = rand(1,50000);
  $name = substr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,5);
  $sql = sprintf("insert into t15 values ​​(%d,%d,%f,'%s')",$i,$cat_id,$price,$name);
  mysql_query($sql, $conn);
}

3. Shopping mall websites usually sort prices according to a certain category. Let’s simulate it and execute the same SQL statement.

select name,cat_id,price from t15 where cat_id=1 order by price;

(1) Do not add an index first

From the query analysis, we can see that Using filesort is used in Extra, indicating that file sorting is required.

這里寫圖片描述

(2) Add an index to the (cat_id, price) column

alter table t15 add index cp(cat_id,price);

In the query analysis here, Extra does not use Using filesort, which means that the SQL that adds the index and then performs the query does not use external sorting, but uses the index sorting. Because the index itself is sorted, there is no need for an additional order by.

這里寫圖片描述

4. Observation query time

這里寫圖片描述

The statement with Query_ID 1 is executed without adding an index, and the statement with Query_ID 3 is executed after adding an index. The execution time of one is 0.013 seconds, and the execution time of the other is 0.005 seconds. It is definitely faster with the index. Let's see where it is faster.

Detailed time-consuming graph of the SQL statement with Query_ID 1:

這里寫圖片描述

Detailed time consumption diagram for the SQL statement with Query_ID 3:

這里寫圖片描述

Obviously, Sorting result in Query_ID1 takes 0.012 seconds, while Sorting result in Query_ID2 only takes 0.000004 seconds, and this Sorting result is the sorting time.

Conclusion: The index has a certain impact on the speed of sorting. Therefore, in actual development, it is necessary to formulate the index based on the actual situation and add the sorting field to the index as much as possible.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL index usage monitoring skills (worth collecting!)
  • Detailed explanation of MySQL row locks when encountering composite primary keys and multi-column indexes
  • Summary of situations where MySQL indexes will not be used
  • Will the index be used in the MySQL query condition?
  • How to modify the IP restriction conditions of MySQL account
  • Detailed explanation of the limitations and restrictions of MySQL partitioned tables
  • MySQL query statement uses limit to limit the number of rows queried
  • Solution to the problem of MySQL connection number exceeding the limit
  • Analysis of the Principle of MySQL Index Length Limit

<<:  Detailed explanation of the solution to the nginx panic problem

>>:  Small program to implement a simple calculator

Recommend

Summary of 10 common HBase operation and maintenance tools

Abstract: HBase comes with many operation and mai...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...

How to install Windows Server 2008 R2 on Dell R720 server

Note: All pictures in this article are collected ...

Where is the project location deployed by IntelliJ IDEA using Tomcat?

After IntelliJ IDEA deploys a Javaweb project usi...

Example of using CSS3 to customize the style of input multiple-select box

Principle: First hide the input element, then use...

Specific operations of MYSQL scheduled clearing of backup data

1|0 Background Due to project requirements, each ...

Installation and daemon configuration of Redis on Windows and Linux

# Installation daemon configuration for Redis on ...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Solution to the error when calling yum in docker container

When executing yum in dockerfile or in the contai...

How to execute PHP scheduled tasks in CentOS7

Preface This article mainly introduces the releva...

Sample code for achieving small triangle border effect with pure CSS3+DIV

The specific code is as follows: The html code is...

How to add and delete unique indexes for fields in MySQL

1. Add PRIMARY KEY (primary key index) mysql>A...

How to use docker compose to build fastDFS file server

The previous article introduced a detailed exampl...

Master-slave synchronous replication configuration of MySQL database under Linux

The advantage of the master-slave synchronization...