zoukankan      html  css  js  c++  java
  • 使用相关子查询

    truncate t_target;  
    insert into t_target  
    select distinct t1.* from t_source t1 where item_id in   
    (select min(item_id) from t_source t2 where t1.created_time=t2.created_time and t1.item_name=t2.item_name);
            这个语句很长时间都出不来结果,只看一下执行计划吧。

    mysql> explain select distinct t1.* from t_source t1 where item_id in   
        -> (select min(item_id) from t_source t2 where t1.created_time=t2.created_time and t1.item_name=t2.item_name);  
    +----+--------------------+-------+------------+------+---------------+------+---------+------+--------+----------+------------------------------+
    | 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 |
    |  2 | DEPENDENT SUBQUERY | t2    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 997282 |     1.00 | Using where                  |
    +----+--------------------+-------+------------+------+---------------+------+---------+------+--------+----------+------------------------------+
    2 rows in set, 3 warnings (0.00 sec)
            主查询和相关子查询都是全表扫描,一共要扫描100万*100万数据行,难怪出不来结果。
    ---------------------

  • 相关阅读:
    算法的时间复杂度与空间复杂度
    递归八皇后问题(回溯算法)
    vue之插件
    递归之打印、阶乘、九九乘法表、迷宫问题
    选择排序
    归并排序
    sql 字符保留汉字处理
    JAVA filter map groupingBy Collectors
    2021最新版IDEA激活
    WPF 程序退出,进程依然存在
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11311195.html
Copyright © 2011-2022 走看看