场景描述:
现在有两个表分别为ip和data表,
ip表:ipId,startIp,endIp 都是整型 数据量为千级,可以按照1000左右来算
data表:dataId,ip,status 都是整型(另外还有其他字段) 数据量为百万级 可以按照2 000 000左右来算
因为业务上的需要,现在需要更新data表的status值为0或1,规则是:如果ip在【startIp,endIp】之间 则更新为1,否则更新为0
我现在只想到了2种方案:
A: 先遍历data表全部设置为status=0,然后根据针对每条ip表记录 更新data表中对应ip段内所有记录的status为1
B:针对data表的所有ip,我们去匹配ip表,只要ip在ip表的任意一个【startIp,endIp】内我就更新status为1 ,否则为0
本人在数据库方面基础薄弱 实在看不出来哪种方法好,希望大家帮忙分析一下
现在有两个表分别为ip和data表,
ip表:ipId,startIp,endIp 都是整型 数据量为千级,可以按照1000左右来算
data表:dataId,ip,status 都是整型(另外还有其他字段) 数据量为百万级 可以按照2 000 000左右来算
因为业务上的需要,现在需要更新data表的status值为0或1,规则是:如果ip在【startIp,endIp】之间 则更新为1,否则更新为0
我现在只想到了2种方案:
A: 先遍历data表全部设置为status=0,然后根据针对每条ip表记录 更新data表中对应ip段内所有记录的status为1
update data set status=0 ;
update data set status=1 where status=0 and (ip bwteen startIp1 and endIp1 );
update data set status=1 where status=0 and (ip bwteen startIp2 and endIp2 );
update data set status=1 where status=0 and (ip bwteen startIp3 and endIp3 );
...
update data set status=1 where status=0 and (ip bwteen startIpN and endIpN );
B:针对data表的所有ip,我们去匹配ip表,只要ip在ip表的任意一个【startIp,endIp】内我就更新status为1 ,否则为0
update data set status=1 where status=0 and ( (ip bwteen startIp1 and endIp1 )
or (ip bwteen startIp2 and endIp2 )
or (ip bwteen startIp3 and endIp3 )
...
or (ip bwteen startIpN and endIpN )
);
update data set status=0 where status=1 and not ( (ip bwteen startIp1 and endIp1 )
or (ip bwteen startIp2 and endIp2 )
or (ip bwteen startIp3 and endIp3 )
...
or (ip bwteen startIpN and endIpN )
);
本人在数据库方面基础薄弱 实在看不出来哪种方法好,希望大家帮忙分析一下