MySql优化子查询

时间:2022-02-25 15:10:21
  • 用子查询语句来影响子查询中产生结果rows的数量和顺序. For example:

  • SELECT * FROM t1 WHERE t1.column1 IN
    (SELECT column1 FROM t2 ORDER BY column1);
    SELECT * FROM t1 WHERE t1.column1 IN
    (SELECT DISTINCT column1 FROM t2);
    SELECT * FROM t1 WHERE EXISTS
    (SELECT * FROM t2 LIMIT 1);//limit关键字不在含有in关键字的子查询中(用exists代替)
  • 代替和子查询做join操作. For example:

    SELECT DISTINCT column1 FROM t1 WHERE t1.column1 IN (
    SELECT column1 FROM t2);

    代替:

    SELECT DISTINCT t1.column1 FROM t1, t2
    WHERE t1.column1 = t2.column1;
  • 一些子查询会被改写成join连接为了兼容不支持子查询的老版本.然而,在一些情况下改写子查询为join操作会提高性能 ;

  • 去掉在子查询中出现的外部语句. For example:

    SELECT * FROM t1
    WHERE s1 IN (SELECT s1 FROM t1 UNION ALL SELECT s1 FROM t2);

    代替:

    SELECT * FROM t1
    WHERE s1 IN (SELECT s1 FROM t1) OR s1 IN (SELECT s1 FROM t2);

    For another example:

    SELECT (SELECT column1 + 5 FROM t1) FROM t2;

    代替:

    SELECT (SELECT column1 FROM t1) + 5 FROM t2;
  • 用行子查询代替一个相关子查询. For example:

    SELECT * FROM t1
    WHERE (column1,column2) IN (SELECT column1,column2 FROM t2);

    代替:

    SELECT * FROM t1
    WHERE EXISTS (SELECT * FROM t2 WHERE t2.column1=t1.column1
    AND t2.column2=t1.column2);
  • 用 NOT (a = ANY (...)) 代替 a <> ALL (...).

  • 用 x = ANY (table containing (1,2))代替 x=1 OR x=2.

  • 用 = ANY 代替 EXISTS.

  • 因为不相关的子查询通常返回一行结果, IN 通常慢于 =. For example:

    SELECT * FROM t1
    WHERE t1.col_name = (SELECT a FROM t2 WHERE b = some_const);

    代替:

    SELECT * FROM t1
    WHERE t1.col_name IN (SELECT a FROM t2 WHERE b = some_const);
  • MySQL 执行不相关的子查询一次. 用 explain确保一个子查询是真正的不相关的.

  • MySQL改写 INALLANY, and SOME 子查询尝试提高select的列在子查询中加索引的可能性 .

  • MySQL 代替用带Index查找功能,explain语句描述为一种特别的join(unqie subquery 或者index subquery)子查询(如下面形式):

    ... IN (SELECT indexed_column FROM single_table ...)
    
  • MySQL 增前了表达式(以下形式调用(min() or max()), 除非null值或者空集合:

    value {ALL|ANY|SOME} {> | < | >= | <=} (uncorrelated subquery)
    

    For example,:

    WHERE 5 > ALL (SELECT x FROM t)
  • 可能被优化成:WHERE 5 > (SELECT MAX(x) FROM t)