大数据表连接问题

 

一个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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值