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

  • 相关阅读:
    EntityFramework Code-First 简易教程(五)-------领域类配置
    EntityFramework Code-First 简易教程(四)-------继承策略
    Springboot整合mybatis:Invalid bound statement (not found)
    centos7服务器安装mysql8
    redis-Jedis
    redis学习-集群模式cluster
    redis学习-发布订阅
    redis学习-持久化机制
    redis学习-简单事务
    redis学习-哨兵模式
  • 原文地址:https://www.cnblogs.com/ly570/p/11311241.html
Copyright © 2011-2022 走看看