Detailed Analysis of or, in, union and Index Optimization in MySQL

Detailed Analysis of or, in, union and Index Optimization in MySQL

This article originated from the homework assignment of "Learn Indexing Skills in One Minute".

Assume that the order business table structure is:

order(oid, date, uid, status, money, time, …)

in:

  • oid, order ID, primary key
  • date, order date, has a common index, and the management backend often queries by date
  • uid, user ID, has a common index, users query their own orders
  • Status, order status, has a common index, and the management backend often queries according to status
  • money/time, order amount/time, queried field, no index

Assume that an order has three states: 0 has been placed, 1 has been paid, and 2 has been completed.

Business requirement: query unfinished orders, which SQL is faster?

  • select * from order where status!=2
  • select * from order where status=0 or status=1
  • select * from order where status IN (0,1)
  • select * from order where status=0
    union all
    select * from order where status=1

Conclusion: Solution 1 is the slowest, while solutions 2, 3, and 4 can all hit the index.

but...

1: union all can definitely hit the index

select * from order where status=0

union all

select * from order where status=1

illustrate:

Tell MySQL what to do directly, MySQL consumes the least CPU

Programmers don't often write SQL like this (union all)

2: Simple in can hit the index

select * from order where status in (0,1)

illustrate:

Let MySQL think, query optimization consumes more CPU than union all, but it is negligible

Programmers often write SQL (in) like this. In this example, it is recommended to write it like this

Three: For or, the new version of MySQL can hit the index

select * from order where status=0 or status=1

illustrate:

Let MySQL think. Query optimization consumes more CPU than IN. Don't put the burden on MySQL.

It is not recommended that programmers use or frequently, as not all ors will hit the index.

For older versions of MySQL, it is recommended to query and analyze

4. For !=, negative queries will definitely not hit the index

select * from order where status!=2

illustrate:

Full table scan, the least efficient and slowest of all solutions

Negative lookups are prohibited

V. Other options

select * from order where status < 2

In this specific example, it is indeed fast, but:

This example only gives three states. The actual business has more than these three states, and the "value" of the state just satisfies the partial order relationship. What if you want to check other states? SQL should not rely on the value of the enumeration, and the solution is not universal.

This SQL has poor readability, poor understandability, and poor maintainability. It is strongly not recommended.

6. Homework

Can such a query hit the index?

select * from order where uid in (

   select uid from order where status=0

)

select * from order where status in (0, 1) order by date desc

select * from order where status=0 or date <= CURDATE()

Note: This is just an example. Don’t be too picky about the rationality of the SQL corresponding to the business.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to view and optimize MySql indexes
  • How to optimize MySQL index function based on Explain keyword
  • How to use indexes to optimize MySQL ORDER BY statements
  • MySQL functional index optimization solution
  • Solutions to Mysql index performance optimization problems
  • MySQL performance optimization: how to use indexes efficiently and correctly
  • An article to master MySQL index query optimization skills
  • MySQL database optimization: index implementation principle and usage analysis
  • Summary of B-tree index knowledge points in MySQL optimization
  • Analyze the sql statement efficiency optimization issues of Mysql table reading, writing, indexing and other operations
  • A brief discussion on MySQL B-tree index and index optimization summary
  • A brief discussion on MySQL index optimization analysis
  • How to optimize MySQL indexes

<<:  Methods and steps for deploying multiple war packages in Tomcat

>>:  Example code for implementing large screen adaptation on PC using vue+px2rem (rem adaptation)

Recommend

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

JavaScript adds event listeners to event delegation in batches. Detailed process

1. What is event delegation? Event delegation: Ut...

Eight examples of how Vue implements component communication

Table of contents 1. Props parent component ---&g...

Using Vue3 (Part 1) Creating a Vue CLI Project

Table of contents 1. Official Documentation 2. Cr...

How to define input type=file style

Why beautify the file control? Just imagine that a...

Use js in html to get the local system time

Copy code The code is as follows: <div id=&quo...

Summary of react basics

Table of contents Preface start React Lifecycle R...

Detailed explanation of JSON.parse and JSON.stringify usage

Table of contents JSON.parse JSON.parse Syntax re...

Use a table to adjust the format of the form controls to make them look better

Because I want to write a web page myself, I am al...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...

Summary of the use of special operators in MySql

Preface There are 4 types of operators in MySQL, ...

How to quickly build a LAMP environment on CentOS platform

This article uses an example to describe how to q...