使用索引实现多表连接

此次我处理的数据量大概在10w左右,一开始直接使用连接查询,效率极低。【甚至是查询40多分钟没能出结果这种情况】

后来在两个表中对我需要连接的列分别建立了非聚集索引。在这之后再进行连接查询时,效率简直不能再高了!!!【就1min之内结束了】

连接查询语句如下:

select * into orderInBeijing
from realytrain, mobike_beijing
where geohashed_loc = geohashed_start_loc or geohashed_loc = geohashed_end_loc 

【其中realytrain表中有250w+条数据】

 

建立索引:


-- 创建唯一非聚集索引
create unique nonclustered        --表示创建唯一非聚集索引
index UQ_Clu_geoLoc        --索引名称
on mobike_beijing(geohashed_loc)        --数据表名称(建立索引的列名)
with 
(
    pad_index=on,    --表示使用填充
    fillfactor=50,    --表示填充因子为50%
    ignore_dup_key=on,    --表示向唯一索引插入重复值会忽略重复值
    statistics_norecompute=off    --表示启用统计信息自动更新功能
)

 

--创建非聚集索引
create nonclustered index NonClu_startLoc
on realytrain(geohashed_start_loc)
with (drop_existing=on);
 
create nonclustered index NonClu_endLoc
on realytrain(geohashed_end_loc)
with (drop_existing=on);

 

索引是个好东西呀!

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值