匹配IP sql优化

sean_zhou 2011-06-15 05:08:19
ip维表 table_ip
字段有 ip_from,ip_to,city
条数 大概40多万

源表 table_day_log
字段有id,ip
条数大概200万条。

想得到的结果

select t1.id,t1.ip,t1.city from
table_Day_log t1
left jion table_ip t2 on t1.ip >= t2.ip_from and t1.ip <= t2.ip_to

请教怎么提高查询速度,我需要查询整个表,纠结了好几天没弄好。
...全文
176 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhazhuzhao 2011-06-15
  • 打赏
  • 举报
回复
table_day_log按照IP升序做聚集索引,table_ip按照ip_from 升序,ip_to 升序做聚集索引;查询看看,最好能把执行计划贴上来!
弘恩 2011-06-15
  • 打赏
  • 举报
回复
可以考虑加强制索引!!!
sean_zhou 2011-06-15
  • 打赏
  • 举报
回复
刚刚我们老大说inner join才用到索引,试了一下,可以提高几倍的 效率。不知道你们说left join有没有试过,我试不出来。:(
--小F-- 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sean_zhou 的回复:]
我这样建了索引速度没变啊,老大。
[/Quote]

查看一下执行计划 看看有没有利用到索引?
sean_zhou 2011-06-15
  • 打赏
  • 举报
回复
我这样建了索引速度没变啊,老大。
cd731107 2011-06-15
  • 打赏
  • 举报
回复
在 table_ip 表的 ip_from 和 ip_to 列上建索引.
table_Day_log的 列建索引
查询语句如1楼
-晴天 2011-06-15
  • 打赏
  • 举报
回复
在 table_ip 表的 ip_from 和 ip_to 列上建索引.
select t1.id,t1.ip,t2.city from 
table_Day_log t1
left jion table_ip t2 on t1.ip between t2.ip_from and t2.ip_to

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧