MySQL - What is wrong with this query or my database? Terrible performance.

Posted by Moss on Stack Overflow See other posts from Stack Overflow or by Moss
Published on 2010-05-20T02:14:40Z Indexed on 2010/05/20 2:20 UTC
Read the original article Hit count: 192

Filed under:
|
|
|
|
SELECT * from `employees` a 
LEFT JOIN (SELECT phone1 p1, count(*) c, FROM `employees` GROUP BY phone1) b
ON a.phone1 = b.p1;

I'm not sure if it is this query in particular that has the problem. I have been getting terrible performance in general with this database. The table in question has 120,000 rows. I have tried this particular query remotely and locally with the MyISAM and InnoDB engines, with different types of joins, and with and without an index on phone1. I can get this to complete in about 4 minutes on a 10,000 row table successfully but performance drops exponentially with larger tables. Remotely it will lose connection to the server and locally it brings my system to its knees and seems to go on forever.

This query is only a smaller step I was trying to do when a larger query couldn't complete. Maybe I should explain the whole scenario. I have one big flat ugly table that lists a bunch of people and their contact info and the info of the companies they work for. I'm trying to normalize the database and intelligently determine which phone numbers apply to individual people and which apply to an office location. My reasoning is that if a phone number occurs multiple times and the number of occurrence equals the number of times that the street address it is attached to occurs then it must be an office number. So the first step is to count each phone number grouping by phone number. Normally if you just use COUNT()...GROUP BY it will only list the first record it finds in that group so I figured I have to join the full table to the count table where the phone number matches. This does work but as I said I can't successfully complete it on any table much larger than 10,000 rows. This seems pathetic and this doesn't seem like a crazy query to do. Is there a better way to achieve what I want or do I have to break my large table into 12 pieces or is there something wrong with the table or db?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about joins