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)优化查询。
    ---------------------

  • 相关阅读:
    vue项目,百度地图api高亮选取区域,高亮某个地区,行政区域等
    vue 项目, 通知子组件更新,父组件中每次点击按钮重新加载子组件,(重新生成dom 元素)
    洛谷 P1003 铺地毯
    Codeforces Round #582 (Div. 3)
    安科 OJ 1190 连接电脑 (并查集)
    2018年牛客多校寒假 第四场 F (call to your teacher) (图的连通性)
    牛客小白月赛16 A 小石的签到题 ( 博弈)
    牛客小白月赛16 E 小雨的矩阵 ( 暴搜)
    安科 OJ 1054 排队买票 (递归,排列组合)
    牛客小白月赛15 C 表单 ( map 使用)
  • 原文地址:https://www.cnblogs.com/ly570/p/11311241.html
Copyright © 2011-2022 走看看