zoukankan      html  css  js  c++  java
  • 使用表连接

    truncate t_target;  
    insert into t_target  (http://www.my516.com)
    select distinct t1.* from t_source t1,  
    (select min(item_id) item_id,created_time,item_name from t_source group by created_time,item_name) t2  
    where t1.item_id = t2.item_id;
            这种方法用时14秒,查询计划如下:

    mysql> explain select distinct t1.* from t_source t1,   (select min(item_id) item_id,created_time,item_name from t_source group by created_time,item_name) t2   where t1.item_id = t2.item_id;
    +----+-------------+------------+------------+------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
    | id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref             | rows   | filtered | Extra                        |
    +----+-------------+------------+------------+------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
    |  1 | PRIMARY     | t1         | NULL       | ALL  | NULL          | NULL        | NULL    | NULL            | 997282 |   100.00 | Using where; Using temporary |
    |  1 | PRIMARY     | <derived2> | NULL       | ref  | <auto_key0>   | <auto_key0> | 5       | test.t1.item_id |     10 |   100.00 | Distinct                     |
    |  2 | DERIVED     | t_source   | NULL       | ALL  | NULL          | NULL        | NULL    | NULL            | 997282 |   100.00 | Using temporary              |
    +----+-------------+------------+------------+------+---------------+-------------+---------+-----------------+--------+----------+------------------------------+
    3 rows in set, 1 warning (0.00 sec)
    内层查询扫描t_source表的100万行,建立临时表,找出去重后的最小item_id,生成导出表derived2,此导出表有50万行。
    MySQL会在导出表derived2上自动创建一个item_id字段的索引auto_key0。
    外层查询也要扫描t_source表的100万行数据,在与导出表做链接时,对t_source表每行的item_id,使用auto_key0索引查找导出表中匹配的行,并在此时优化distinct操作,在找到第一个匹配的行后即停止查找同样值的动作。
    ---------------------

  • 相关阅读:
    2021“MINIEYE杯”中国大学生算法设计超级联赛(1)1005. Minimum spanning tree(min25筛)
    2021“MINIEYE杯”中国大学生算法设计超级联赛(1)1008. Maximal submatrix(DP/单调栈)
    2021牛客暑期多校训练营2 K. Stack(拓扑排序)
    2021牛客暑期多校训练营2 F. Girlfriend(阿波罗尼斯圆/计算几何)
    牛客小白月赛36 C. 杨辉三角(组合数/推柿子)
    牛客小白月赛36 I. 四面楚歌(DFS)
    链路聚合
    python 重复尝试【retry】编写
    groovy 错误retry函数
    Jenkins【共享库功能】开发通用流水线模板
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11311194.html
Copyright © 2011-2022 走看看