How to choose between in and exists, not in and not exists in mysql

16467935:

In the MySQL database, the performance comparison of IN, EXISTS, NOT IN and NOT EXISTS is also affected by many factors, including query structure, data volume, index and database engine, etc.

General guidance for some common situations:

IN vs. EXISTS:

Using EXISTS may be more efficient when there is a smaller main query and a larger subquery, because MySQL can stop executing the subquery as soon as a match is found.
IN may work well in some cases, especially when the set of values ​​in the main query is small and indexed. However, if the collection of values ​​is large, IN can become inefficient because it needs to compare each value with each element in the collection.

NOT IN vs. NOT EXISTS:

In the case of NOT EXISTS , the database can stop execution immediately when it finds a match, so it may be more efficient for some cases.
NOT IN may perform well in situations where the main query value set is small and indexed. However, NOT IN can cause performance problems if the set of values ​​is large.

scenes to be used

Using both IN and EXISTS depends on the requirements and data structure of the query. Here are some common scenarios where specific operators are recommended:

Scenarios for using IN:

Small collections of values: IN may be more efficient when you need to find matches in a relatively small collection of values.

Single column comparison: If you only need to compare on a single column, and the column is indexed, it may be more appropriate to use IN.

List queries: IN may be more intuitive to use when you have a predefined list of values ​​and want to find records matching those values ​​in the database.

Scenarios using EXISTS:

Existence of subquery results: When you need to check whether there are records that meet certain conditions, especially in relational tables or subqueries, using EXISTS is more advantageous.

Related records check: If you need to check whether certain records exist related records (for example, whether an order has an associated line item), using EXISTS is more suitable.

Large collections of values: When the main query has a large collection of values, or the subquery has a large result set, EXISTS can be more efficient than IN because it can stop the query as soon as a match is found.

Performance optimization: In complex queries, the database optimizer may handle EXISTS better because it can stop the query as soon as it finds a match, reducing unnecessary work.

In summary, IN is suitable for simple value set comparisons, while EXISTS is more suitable for situations where you need to check the existence of subquery result sets. The best choice should be based on your query requirements, database structure, and performance considerations. The best approach is to test and evaluate on real data to find the best operator for your particular situation.

Guess you like

Origin blog.csdn.net/yuanchengfu0910/article/details/132320490