zoukankan      html  css  js  c++  java
  • 在两个库中查找出差异明细

    这个方案是之前一个前同事问我时想出来的,但当时没进行实践,只是觉得可行。

    今天终于也遇到这个场景,所以就进行测试。

    两个库进行测试,还有其它的方案,比如spark,python,落地文件再对比差异等,各有优劣,比如spark需要集群的支持,并且速度也不是很快。ptyhon对大数据量的支持不是很好,落地文件需要占用存储空间。

    这个方案可能简单进行修改为脚本版,然后通过脚本进行计算,即可一次计算3到4位,使迭代次数大大减少,加快执行速度。

    另改进的地方是过滤时可使用in,将多个有差异的SQL一次性查找出来。

    -- 此例为简明,以每次递进1位进行分析
    -- 第一次计算1位
    select
         substr(md5_value,1,1) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,0) = ''
    group by substr(md5_value,1,1)
    order by flag
    ;
    
    -- 第一次计算2位
    select
         substr(md5_value,1,2) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,1) = '3'
    group by substr(md5_value,1,2)
    order by flag
    ;
    
    -- 第一次计算3位
    select
         substr(md5_value,1,3) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,2) = '33'
    group by substr(md5_value,1,3)
    order by flag
    ;
    
    -- 第一次计算4位
    select
         substr(md5_value,1,4) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,3) = '333'
    group by substr(md5_value,1,4)
    order by flag
    ;
    
    -- 第一次计算5位
    select
         substr(md5_value,1,5) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,4) = '3338'
    group by substr(md5_value,1,5)
    order by flag
    ;
    
    -- 第一次计算6位
    select
         substr(md5_value,1,6) as flag
        ,count(1) as cnt
    from (
        select 
            md5(ifnull(id,'123')) as md5_value
        from table_name
    ) t1
    where substr(md5_value,1,5) = '33384'
    group by substr(md5_value,1,6)
    order by flag
    ;
    +--------+------+
    | flag   | cnt  |
    +--------+------+
    | 333840 |    1 |
    | 333841 |    1 |
    | 333845 |    1 |
    | 333846 |    1 |
    | 333847 |    1 |
    | 333849 |    1 |
    | 33384a |    1 |
    | 33384b |    2 |
    +--------+------+
    
    +--------+-----+
    | flag   | cnt |
    +--------+-----+
    | 333840 |   1 |
    | 333841 |   1 |
    | 333845 |   1 |
    | 333846 |   1 |
    | 333847 |   1 |
    | 333849 |   1 |
    | 33384a |   1 |
    | 33384b |   1 |
    +--------+-----+
    
    
    -- 最后可得到明细记录
    select 
         *
    from table_name
    where substr(md5(ifnull(id,'123')),1,6) = '33384b'
    G
  • 相关阅读:
    C#遍历DataSet中数据的几种方法总结
    ajax跨域访问控制
    几种数据库的大数据批量插入(SqlServer、Oracle、SQLite和MySql)
    转:InnoDB多版本(MVCC)实现简要分析
    转:InnoDB Page Structure(InnoDB页面结构详解)
    PostgreSQL 数据库角色
    PostgreSQL 9.5 客户端认证
    PostgreSQL服务器参数配置
    转:InnoDB Log Block Structure(InnoDB日志Block结构详解)
    转:InnoDB Crash Recovery 流程源码实现分析
  • 原文地址:https://www.cnblogs.com/chenzechao/p/13345707.html
Copyright © 2011-2022 走看看