Updated on 2025-03-13 GMT+08:00

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 cannot 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.