I have a query that gets some data about customers. This customers can have three phone numbers and these can be repeated.
To count the number of times that these phones are repeated a partner have create a subselect:
(select count(*)
from TABLE_A as k
where (k.phone=a.phone or k.phone2=a.phone or k.phone3=a.phone)
and k.id!=a.id) as repetitionsPhone1
This is inside a bigger select like this:
SELECT a.*,c.*,b.*,
(
select count(*)
from TABLE_A as k
where (k.phone=a.phone or k.phone2=a.phone or k.phone3=a.phone)
and k.id!=a.id
) as repetitionsPhone1
FROM a
left join c on a.id=c.id
left join b on a.id=b.id
This query takes for 50 rows about 30 seconds, and it should return about 2000 rows every single day.
To optimize this I use explain
and I see that this subquery was the problem so I searched and I tried this:
SELECT phn,sum(count) as phoneRepetitions
from (
select k.phone1 as phn, count(*) as count
from k
group by k.phone1
UNION
select k.phone2 as phn,count(*) as count
from k
group by k.phone2
UNION
select k.phone3 as phn,count(*) as count
from k
group by k.phone3
) as aux
group by phn
And this returns #1062 MYSQL error: Duplicate entry for key 'distinct key'
First of all I would like to solve this problem. Anyone knows what is happening? This error seems logic in an insert statement, but in select?
And later, this will help to improve the big select that I must optimize? I will have to do this for the three columns.
Thank you.