大数据表连接问题

    技术2022-05-19  18

     

    一个5000W数据表去连接一个3W的数据表,怎么来提高查询性能?下面提出了一种解决方案。

     

    大致思路是:1 将表进行压缩,去掉此次查询不需要的数据。

                2 将数据放入临时表,然后再给查询条件字段加索引。(这是关键点)

                3 将临时表进行连接查询。

     

    实例:page_visit_log 5000W数据, china_ip_detail 3W 数据。

      

     

        --1、从页面访问日志中查找大于最大编号并且访问时间小于当天的记录

        select  code, site_hash,page_hash,page_url,visit_guid,ip,visit_time

        into #T1

        from page_visit_log

        where   (code >@max_code and visit_time < @current_date)

             or (code <= @max_code and convert(varchar(10),visit_time,121) = @last_process_time )

        -- #T1.ip加索引

        create   clustered   index   T1_ip   on   #T1(ip)

       

        -- 取无重复省份IP信息

        select start_ip,end_ip,province into #ip_table

        from china_ip_detail c

        where city is not null or c.province = '北京市' or c.province = '天津市' or c.province = '上海市'or c.province = '重庆市'

       

         -- IP临时表加索引

        create   clustered   index   tab_start_ip   on   #ip_table(start_ip,end_ip)

       

        --2、将ip中对应的省份对应

        select  t1.*,ip_tab.province  province_name

        into #T2

        from #T1 t1

               left join  #ip_table ip_tab

               on t1.ip between ip_tab.start_ip and ip_tab.end_ip

     

    简单测试表明:select  top 10000 由原来的查半天不出来,变成24秒。

              


    最新回复(0)