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

  • 相关阅读:
    Java 的this和super关键字
    Java关于访问控制权限
    Java 封装 继承 多态
    Java的集成开发工具
    如何查询小程序中的代码量
    小程序中使用echarts及使用的坑
    微信小程序-新的页面授权机制
    前端处理几十万条数据不卡顿(window.requestAnimationFrame)
    Vs code中Eslint 与 Prettier格式化冲突
    从原型与原型链的角度看es6 class
  • 原文地址:https://www.cnblogs.com/ly570/p/11311241.html
Copyright © 2011-2022 走看看