zoukankan      html  css  js  c++  java
  • mysql join优化

    http://www.jb51.net/article/50427.htm

    七.性能优化
    1.显示(explicit) inner join VS 隐式(implicit) inner join

    如:

    复制代码代码如下:

    select * from
    table a inner join table b
    on a.id = b.id;

    VS

    复制代码代码如下:

    select a.*, b.*
    from table a, table b
    where a.id = b.id;

    我在数据库中比较(10w数据)得之,它们用时几乎相同,第一个是显示的inner join,后一个是隐式的inner join。

    2.left join/right join VS inner join

    尽量用inner join.避免 LEFT JOIN 和 NULL.

    在使用left join(或right join)时,应该清楚的知道以下几点:
    (1). on与 where的执行顺序

        ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有列为 NULL 的数据,在匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE 子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤。

    所以我们要注意:在使用Left (right) join的时候,一定要在先给出尽可能多的匹配满足条件,减少Where的执行。如:

    PASS

    复制代码代码如下:

    select * from A
    inner join B on B.name = A.name
    left join C on C.name = B.name
    left join D on D.id = C.id
    where C.status>1 and D.status=1;

    Great

    复制代码代码如下:

    select * from A
    inner join B on B.name = A.name
    left join C on C.name = B.name and C.status>1
    left join D on D.id = C.id and D.status=1

    从上面例子可以看出,尽可能满足ON的条件,而少用Where的条件。从执行性能来看第二个显然更加省时。

    (2).注意ON 子句和 WHERE 子句的不同

    如作者举了一个列子:

    复制代码代码如下:

    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id)
           AND product_details.id=2;
    +----+--------+------+--------+-------+
    | id | amount | id   | weight | exist |
    +----+--------+------+--------+-------+
    |  1 |    100 | NULL |   NULL |  NULL |
    |  2 |    200 |    2 |     22 |     0 |
    |  3 |    300 | NULL |   NULL |  NULL |
    |  4 |    400 | NULL |   NULL |  NULL |
    +----+--------+------+--------+-------+
    4 rows in set (0.00 sec)

    mysql> SELECT * FROM product LEFT JOIN product_details
           ON (product.id = product_details.id)
           WHERE product_details.id=2;
    +----+--------+----+--------+-------+
    | id | amount | id | weight | exist |
    +----+--------+----+--------+-------+
    |  2 |    200 |  2 |     22 |     0 |
    +----+--------+----+--------+-------+
    1 row in set (0.01 sec)

    从上可知,第一条查询使用 ON 条件决定了从 LEFT JOIN的 product_details表中检索符合的所有数据行。第二条查询做了简单的LEFT JOIN,然后使用 WHERE 子句从 LEFT JOIN的数据中过滤掉不符合条件的数据行。

    (3).尽量避免子查询,而用join

    往往性能这玩意儿,更多时候体现在数据量比较大的时候,此时,我们应该避免复杂的子查询。如下:

    PASS

    复制代码代码如下:

    insert into t1(a1) select b1 from t2 where not exists(select 1 from t1 where t1.id = t2.r_id); 

    Great

    复制代码代码如下:

    insert into t1(a1)  
    select b1 from t2  
    left join (select distinct t1.id from t1 ) t1 on t1.id = t2.r_id   
    where t1.id is null;  
  • 相关阅读:
    Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
    L2-011. 玩转二叉树
    zoj 2976 Light Bulbs(暴力枚举)
    ZOJ 2975 Kinds of Fuwas(暴力+排列组合)
    ZOJ-2972-Hurdles of 110m(线性dp)
    ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)
    zoj 2966 Build The Electric System(最小生成树)
    ZOJ 2965 Accurately Say "CocaCola"!(预处理)
    HDU 3452 Bonsai(树形dp)
    zoj 3212 K-Nice(构造)
  • 原文地址:https://www.cnblogs.com/panxuejun/p/5952977.html
Copyright © 2011-2022 走看看