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

js version to realize calculator function

This article example shares the specific code of ...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

MySQL SQL statement performance tuning simple example

MySQL SQL statement performance tuning simple exa...

Vue Element front-end application development: Use of API Store View in Vuex

Table of contents Overview 1. Separation of front...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

Methods and steps for deploying go projects based on Docker images

Dependence on knowledge Go cross-compilation basi...

Tutorial on installing and configuring remote login to MySQL under Ubuntu

This article shares the MySQL installation and co...

mysql5.7 remote access settings

Setting up remote access in mysql5.7 is not like ...

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

How to solve the problem of FileZilla_Server:425 Can't open data connection

When installing FileZilla Server on the server, t...