目标IP表是IP段:IP1-IP2 (IP1,ip2建索引),所以转成字符串没法比对。考虑将WEB日志表的IP转成数值并建立索引,但有下列问题: 1、web表十分巨大,会否对插入造成影响 2、用以下sql获取date1-date2之间的在目标IP范围内的记录,能否有效用到索引 select a.ip,b.country from web日志 as a,目标IP as b where a.date>=date1 and a.date<=date2 and a.ip>=b.ip1 and a.ip<=b.ip2
select a.ip,b.country from web日志 as a,目标IP as b where a.date>=date1 and a.date <=date2 and a.ip>=b.ip1 and a.ip <=b.ip2偶看见过关系有这样写的"a.ip>=b.ip1 and a.ip <=b.ip2"
偶没看见过关系有这样写的"a.ip>=b.ip1 and a.ip <=b.ip2"
select a.ip from web日志 as a where a.date>=date1 and a.date <=date2 and a.IP in --按字符串,有用索引 ( select 转成字符串(b.IP) --因为偶不知道你用的是什么DB,就这样写 from 目标IP as b where b.ip>ip1 and b.ip<ip2 --按数字,有用索引 )
按照10分钟执行10w次查询来算,150w也就2小时半够了,缘何最终花了14小时?!
一般来说是从减少全表扫描(就是建索引)和减少记录(就是优化SQL语句)来解决的
你是想要迁就谁呢?谁代价高就听谁的你为什么要转换数据库字段的值而不去转换目标IP表的IP为字符串呢逆向啊
1、web表十分巨大,会否对插入造成影响
2、用以下sql获取date1-date2之间的在目标IP范围内的记录,能否有效用到索引
select a.ip,b.country from web日志 as a,目标IP as b where a.date>=date1 and a.date<=date2 and a.ip>=b.ip1 and a.ip<=b.ip2
from web日志 as a
where a.date>=date1 and a.date <=date2 and a.IP in --按字符串,有用索引
(
select 转成字符串(b.IP) --因为偶不知道你用的是什么DB,就这样写
from 目标IP as b
where b.ip>ip1 and b.ip<ip2 --按数字,有用索引
)
b表的结构是这样地:
IP1:起始IP
IP2:结束IP
country:地区