Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

This time, I will explain two small knowledge points related to SQL query. Mastering these knowledge points can help you avoid stepping on holes and improve query efficiency.

1. Allow the value of the field to be null, which often leads to disaster

First, prepare some data first, and demonstrate later

create table animal(
id int,
name char(20),
index(id)
)engine=innodb;

index(id) means to create an index for the id field, and both id and name are allowed to be null.

Then insert 4 pieces of data, the id of the last piece of data is.

insert into animal(id, name) values(1, '猫');
insert into animal(id, name) values(2, '狗');
insert into animal(id, name) values(3, '猪');
insert into animal(id, name) values(null, '无名动物');

(Note: the code block can be pulled left and right)

The data in the table at this time is
Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

At this time, we query the table for the animals with id != 1

select * from animal where id != 1;

The results are as follows:

Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)
At this time, we only found two rows of data, which should be three rows in principle, but the row with id = null was not matched. You may have heard that null
is not equal to any other value . It is reasonable to be null != 1 is true, but the reality is cruel, it just won't be matched.

Therefore, never allow the value of the field to be null, otherwise there may be results that do not meet expectations.

Anyway, I have stepped on this pit before, don’t you know if you have stepped on it?

But what if someone sets a null value? If this is the case, for the search for !=, you can add an or id is null clause afterwards (note that it is is null, not = null, because id = null will not match a row with a value of null). which is

select * from animal where id != 1 or id is null;

The results are as follows:
Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

2. Use union instead of or as much as possible

(1). Just now, we created an index for the id field. If we perform equivalent operations, we will generally perform index operations. If you don’t believe me, see:

explain select * from animal where id = 1;

The results are as follows:
Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

As you can see from the execution plan, the equivalent search on id can be indexed (estimated in your expectation), where

type = ref: non-unique index
rows = 1: predictive scan one row

(2) Will the index be used if id is null? The answer is yes, as shown in the figure

explain select * from animal where id is null;

Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)
Among them
type = ref: non-unique index
rows = 1: predict to scan one row

(3) So the question is, if we want to find the animal with id = 1 or id = null, we may use the or statement to connect, that is

select * from animal where id = 1 or id is null;

Will this statement be indexed?

Whether to use the index, you can see the execution plan, as shown in the figure

explain select * from animal where id = 1 or id is null;

Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)
among them:

ref = ALL: means full table scan

rows = 4: predict to scan 4 rows (while our entire table has only 4 rows)

It can be seen from the execution plan that it is very possible to use or without indexing, which will greatly reduce the query rate, so it is generally not recommended to use the or clause to connect conditions.

So how to solve it?

In fact, union can be used to replace or, as follows:

select * from animal where id = 1 union select * from animal where id is null.

Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)
At this time, it will go through the index twice, find all the rows with id = 1 and all id = null, and then use a temporary table to store the final result, and finally scan the temporary table.

3. Summary

1. When defining the table, try not to allow the field value to be null, and you can use default to set the default value.

2. Try to use union instead of or to avoid queries without indexing.

3. Note that the equivalent query with id = null will not match the row whose value is null. Instead, id is null should be used.

You are also welcome to talk about the pits you have stepped on.

You might like
1. Tencent interview: What are the reasons why a SQL statement is executed very slowly? ---
Do not watch Regret Series 2. Why can't you learn recursion? Say goodbye to recursion and talk about some of my experiences.
3. An article to understand how a computer sends data to another computer.
4. How to find the most frequent occurrence from 20/40/80 billion integers with only 2GB of memory Number
5. String matching Boyer-Moore algorithm: How is the search function in the text editor implemented?
Let me go, these two little tricks not only hide my SQL statement, but also improve it by 1000 times (half-minute dry goods series)

Guess you like

Origin blog.51cto.com/15015171/2554965