In the multi-table association query statement, placing a huge user ID array in in causes the query time to be too long. How to optimize it?
1. The query statement is as shown below, and the execution time is 147.4ms.
In the multi-table association query statement, placing a huge user ID array in in causes the query time to be too long. How to optimize it?
1. The query statement is as shown below, and the execution time is 147.4ms.
WHERE id IN (1,2,3)
is equivalent to WHERE id = 1 OR id = 2 OR id =3
OR There are so many conditions, even if there is an index on the id, it is normal to be slow.
The root cause of this problem is unreasonable table structure design. Optimizing queries is also a temporary solution rather than a permanent solution
So don’t be too anxious when designing the structure
In addition, use EXPLAIN more often