How to use indexes to optimize MySQL ORDER BY statements

How to use indexes to optimize MySQL ORDER BY statements

Create table & create index

create table tbl1 (
id int unique, sname varchar(50),
index tbl1_index_sname(sname desc)
);

Syntax for creating an index on an existing table

create [unique|fulltext|spatial] index index name on table name (field name [length] [asc|desc]);

MySQL can also use indexes to quickly perform sorting and grouping operations in ORDER BY and GROUP BY statements.

Optimize MySQL ORDER BY statements through index optimization:

1. ORDER BY index optimization

If a SQL statement is as follows:

SELECT [column1],[column2],…. FROM [TABLE] ORDER BY [sort];

By creating an index on the [sort] column, you can use the index to optimize order by.

2. Index optimization of WHERE + ORDER BY

Such as:

SELECT [column1],[column2],…. FROM [TABLE] WHERE [columnX] = [value] ORDER BY [sort];

Create a joint index (columnX, sort) to achieve order by optimization.

Note: If columnX corresponds to multiple values, the following statement cannot use the index to optimize the order by:

SELECT [column1],[column2],…. FROM [TABLE] WHERE [columnX] IN ([value1],[value2],…) ORDER BY [sort];

3. WHERE + multiple fields ORDER BY

SELECT * FROM [table] WHERE uid=1 ORDER x,y LIMIT 0,10;

Creating an index (uid, x, y) to optimize order by is much better than creating an index (x, y, uid).

MySQL Order By cannot use indexes to optimize sorting

1. Perform ORDER BY on different index keys: (create indexes for key1 and key2 separately)

SELECT * FROM t1 ORDER BY key1, key2;

2. Perform ORDER BY on non-contiguous index key parts: (create a joint index on key_part1 and key_part2; create an index on key2)

SELECT * FROM t1 WHERE key2=constant ORDER BY key_part2;

3. Using both ASC and DESC: (key_part1, key_part2 create a joint index)

SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

4. The index key used to search records is not the same as the one used for ORDER BY: (key1 and key2 are indexed separately)

SELECT * FROM t1 WHERE key2=constant ORDER BY key1;

5. If you apply expressions (functions) to WHERE and ORDER BY columns, you cannot use indexes to optimize the order by.

SELECT * FROM t1 ORDER BY YEAR(logindate) LIMIT 0,10;

Special Tips:

1>MySQL can only use one index at a time. If you want to use indexes on multiple fields, create a composite index.

2> In an ORDER BY operation, MySQL uses the index only when the sort condition is not a query condition expression.

The above is the details of how MySQL uses indexes to optimize ORDER BY sorting statements. For more information about MySQL optimizing ORDER BY sorting statements, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL Order By Syntax Introduction
  • MySQL briefly understands how "order by" works
  • MySQL Order by statement usage and optimization detailed explanation
  • Analysis of two ideas to improve the query efficiency of Order by statement in MySQL
  • In-depth analysis of the order by and group by sequence issues in MySQL
  • MySQL database index order by sorting detailed explanation

<<:  In-depth explanation of nginx location priority

>>:  Multiple solutions for cross-domain reasons in web development

Recommend

Quickly solve the problem of slow Tomcat startup, super simple

Today I helped a classmate solve a problem - Tomc...

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

Learn one minute a day to use Git server to view debug branches and fix them

Debug branch During the normal development of a p...

CentOS7 uses yum to install mysql 8.0.12

This article shares the detailed steps of install...

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

Implementation steps for installing java environment in docker

This article is based on Linux centos8 to install...

Example code for using text-align and margin: 0 auto to center in CSS

Use text-align, margin: 0 auto to center in CSS W...

Summary of the application of decorative elements in web design

<br />Preface: Before reading this tutorial,...

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps: 1. Check the disk sp...

Five solutions to cross-browser problems (summary)

Brief review: Browser compatibility issues are of...

Detailed explanation of how to configure static IP in Centos8

After installing centos 8, the following error wi...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

Detailed explanation of CSS margin overlap and solution exploration

I recently reviewed some CSS-related knowledge po...