Subquery
- [Rule] Do not use repeated subquery statements in an SQL statement.
- [Recommendation] Do not use scalar subqueries.
A scalar subquery is a subquery whose result is one value and whose condition expression uses an equal operator. Example:
The following statement does not meet specifications:
SELECT * FROM t1 WHERE id = (SELECT id FROM t2 LIMIT 1);
You are advised to split the preceding statement into two SQL statements and execute the subquery first.
- [Recommendation] Do not use subqueries in the SELECT target columns. Otherwise, the plan may fail to be pushed down, affecting the execution performance.
- [Recommendation] The nesting depth of a subquery cannot exceed 2.
Subqueries cause temporary table overhead. Therefore, complex queries must be optimized based on service logic.