[英]mysql add duplicated field id to a current field
i have table like : 我有这样的表:
id IP Subnet Duplicates Valid
1 foo 16 1
2 bar 24 1
3 foo 28 1
4 foo 32 1
i want update description with id of duplicated row . 我想要更新描述与重复行的ID。 something like:
就像是:
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
Here is my query : 这是我的查询:
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
my code works but its very slow ( about 53 second for 10000 record ) 我的代码有效,但速度很慢(10000条记录约为53秒)
How can i improve speed ? 我怎样才能提高速度? is there any way that i can decrease comparison operation.
有什么方法可以减少比较操作。
Here is a solution without self join in your sub query, maybe will not improve performance greatly, but try it, and also, try to explain it and yours. 这是一个在子查询中没有自联接的解决方案,可能不会大大提高性能,但尝试它,并尝试解释它和你的。
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, ',')
, '')
;
you can also use this query for test: 您也可以使用此查询进行测试:
UPDATE dupli d
SET description = (
SELECT CONCAT('duplicate in ',GROUP_CONCAT(`id` ORDER BY id))
FROM (SELECT * FROM dupli) AS d1
WHERE `name` = d.`name` AND id <> d.id ) ;
sample 样品
MariaDB [yourSchema]> UPDATE dupli d
-> SET description = (
-> SELECT CONCAT('duplicate in ',GROUP_CONCAT(`id` ORDER BY id))
-> FROM (SELECT * FROM dupli) AS d1
-> WHERE `name` = d.`name` AND id <> d.id ) ;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 1
MariaDB [yourSchema]> select * from dupli;
+----+------+------------------+
| id | name | description |
+----+------+------------------+
| 1 | foo | duplicate in 3,4 |
| 2 | bar | NULL |
| 3 | foo | duplicate in 1,4 |
| 4 | foo | duplicate in 1,3 |
+----+------+------------------+
4 rows in set (0.00 sec)
MariaDB [yourSchema]>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.