zoukankan      html  css  js  c++  java
  • 建立查重的存储过程

    有了以上信息我们就可以写出4条语句处理全部数据。为了调用接口尽量简单,建立下面的存储过程。

    delimiter //
    create procedure sp_unique(i smallint)    
    begin     
        set @a:='1000-01-01 00:00:00';  
        set @b:=' ';  
        if (i<4) then
            insert into t_target  
            select * from t_source force index (idx_sort)  
             where created_time >= date_add('2017-01-01',interval (i-1)*125000 second) 
               and created_time < date_add('2017-01-01',interval i*125000 second) 
               and (@a!=created_time or @b!=item_name) 
               and (@a:=created_time) is not null 
               and (@b:=item_name) is not null  
             order by created_time,item_name;  
        else 
        insert into t_target  
            select * from t_source force index (idx_sort)  
             where created_time >= date_add('2017-01-01',interval (i-1)*125000 second) 
               and created_time <= date_add('2017-01-01',interval i*125000 second) 
               and (@a!=created_time or @b!=item_name) 
               and (@a:=created_time) is not null 
               and (@b:=item_name) is not null  
             order by created_time,item_name;  
        end if;    
    end     
    //
            查询语句的执行计划如下:

    mysql> explain select * from t_source force index (idx_sort)  
        ->          where created_time >= date_add('2017-01-01',interval (http://www.amjmh.com)*125000 second) 
        ->            and created_time < date_add('2017-01-01',interval 1*125000 second) 
        ->            and (@a!=created_time or @b!=item_name) 
        ->            and (@a:=created_time) is not null 
        ->            and (@b:=item_name) is not null  
        ->          order by created_time,item_name; 
    +----+-------------+----------+------------+-------+---------------+----------+---------+------+--------+----------+-----------------------+
    | id | select_type | table    | partitions | type  | possible_keys | key      | key_len | ref  | rows   | filtered | Extra                 |
    +----+-------------+----------+------------+-------+---------------+----------+---------+------+--------+----------+-----------------------+
    |  1 | SIMPLE      | t_source | NULL       | range | idx_sort      | idx_sort | 6       | NULL | 498640 |   100.00 | Using index condition |
    +----+-------------+----------+------------+-------+---------------+----------+---------+------+--------+----------+-----------------------+
    1 row in set, 3 warnings (0.00 sec)
            MySQL优化器进行索引范围扫描,并且使用索引条件下推(ICP)优化查询。
    ---------------------

  • 相关阅读:
    .NET Core: 在.NET Core中进行单元测试
    .NET: 使用.NET Core CLI开发应用程序
    .NET: 谈谈C#中的扩展方法
    WPF: WPF 中的 Triggers 和 VisualStateManager
    WPF: 只读依赖属性的介绍与实践
    XAML: 自定义控件中事件处理的最佳实践
    .NET: 谈谈共享项目 (Shared Project) 的使用
    UWP: 实现 UWP 应用自启动
    UWP: 通过命令行启动 UWP 应用
    在 .NET中,一种更方便操作配置项的方法
  • 原文地址:https://www.cnblogs.com/ly570/p/11311241.html
Copyright © 2011-2022 走看看