zoukankan      html  css  js  c++  java
  • 利用窗口函数

    MySQL 8中新增的窗口函数使得原来麻烦的去重操作变得很简单。

    truncate t_target;  
    insert into t_target 
    select item_id, created_time, modified_time, item_name, other
      from (select *, row_number(http://www.amjmh.com/v/) over(partition by created_time,item_name) as rn
              from t_source) t where rn=1;
            这个语句执行只需要12秒,而且写法清晰易懂,其查询计划如下:

    mysql> explain select item_id, created_time, modified_time, item_name, other
        ->   from (select *, row_number() over(partition by created_time,item_name) as rn
        ->           from t_source) t where rn=1;
    +----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
    | id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref   | rows   | filtered | Extra          |
    +----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
    |  1 | PRIMARY     | <derived2> | NULL       | ref  | <auto_key0>   | <auto_key0> | 8       | const |     10 |   100.00 | NULL           |
    |  2 | DERIVED     | t_source   | NULL       | ALL  | NULL          | NULL        | NULL    | NULL  | 997281 |   100.00 | Using filesort |
    +----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
    2 rows in set, 2 warnings (0.00 sec)
            该查询对t_source表进行了一次全表扫描,同时用filesort对表按分区字段created_time、item_name进行了排序。外层查询从每个分区中保留一条数据。因为重复created_time和item_name的多条数据中可以保留任意一条,所以oevr中不需要使用order by子句。

            从执行计划看,窗口函数去重语句似乎没有消除嵌套查询的变量去重好,但此方法实际执行是最快的。

  • 相关阅读:
    log4net
    winform datagridview 刷新数据不需要重新绑定
    Git 简明教程(一)
    Task详解【转载】
    虚拟机Linux系统手动指定IP
    DataGrip连接sqlserver,提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
    c#中Equals方法和GetHashCode
    获取程序集的类型信息
    第一个windows桌面应用程序
    在运行时让PropertyGrid动态显示我们想要的对象属性
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11311223.html
Copyright © 2011-2022 走看看