我的表格如下:
id IP Subnet Duplicates Valid
1 foo 16 1
2 bar 24 1
3 foo 28 1
4 foo 32 1
我希望更新描述的ID为重复行。类似的东西:
id IP Subnet Duplicates Valid
1 foo 16 3,4 0
2 bar 24 1
3 foo 28 1,4 0
4 foo 32 1,3 0
这是我的问题:
update tblSample inner join (
select
t1.Id,
group_concat(t2.Id) dups
from
tblSample t1 inner join tblSample t2
on t1.Id<>t2.Id ) AND
((t1.IP >> (32-LEAST(t1.Subnet,t2.Subnet))
<< (32-LEAST(t1.Subnet,t2.Subnet))
=
((t2.IP >> (32-LEAST(t1.Subnet,t2.Subnet))
<< 32-LEAST(t1.Subnet,t2.Subnet)))
group by
t1.Id
) s on tblSample.Id = s.Id
set
Valid=0 ,Duplicates=dups
我的代码有效,但速度非常慢(10000条记录约为53秒)
我如何提高速度? 有什么方法可以减少比较操作。
答案 0 :(得分:1)
这是一个在子查询中没有自联接的解决方案,可能不会大大提高性能,但尝试一下,并尝试解释它和你的。
update tblSample t1
join (
select name, group_concat(id order by id) as description
from tblSample
group by name
) t2
on t1.name = t2.name and cast(t1.id as char) <> t2.description
set t1.description = replace(
replace(
replace(
t2.description,
concat(',', t1.id, ','),
',')
, concat(',', t1.id)
, '')
, concat(t1.id, ',')
, '')
;
答案 1 :(得分:0)
您也可以将此查询用于测试:
petalCount
<强>样品强>
this