0
SELECT `listener` ,
SEC_TO_TIME( SUM( TIME_TO_SEC( `call_time` ) ) ) AS total_time,
COUNT( `listener` ) AS number
FROM calls
WHERE listened_date = '2013-05-09'
AND type in ('column1','column2')
AND id
IN ( SELECT id
FROM calls
GROUP BY CONCAT( name, ' ', when ) )
GROUP BY `listener`

This query is working so slow and making other queries not working in same time. How can i make this lighter?

I think IN make it slower. What is alternative in this case?

7
  • have you indexed "id"? Commented May 9, 2013 at 7:49
  • @ile No. Is IGNORE INDEX (id) enough? Commented May 9, 2013 at 7:53
  • just the opposite - creating an index on a column will increase querying speed greatly, although it will grow the memory size of the db a bit. You can create an index on your table with "CREATE INDEX id_index ON calls (id) USING BTREE;". Note that 1. this should be an addition to optimizing your mysql query (other answers) 2. you should reindex the table after adding new records ("OPTIMIZE TABLE calls"), i.e. it is not a good solution if you have frequent inserts/deletions on the table Commented May 9, 2013 at 8:03
  • @ile Average 9 insert, update, delete query per second working in table. Commented May 9, 2013 at 8:14
  • there is an average of 9 inserts, delete, updates on the table? :| are you sure, seems highly exaggerated?... How many records do you have? Commented May 9, 2013 at 8:18

1 Answer 1

2

Maybe ?:

SELECT c.`listener` ,
       SEC_TO_TIME(SUM(TIME_TO_SEC(c.`call_time`))) AS total_time,
       COUNT(c.`listener`) AS number
FROM calls c
WHERE c.listened_date = '2013-05-09'
  AND c.TYPE IN ('column1',
                 'column2')
  AND EXISTS (SELECT 0
              FROM calls c2
              WHERE c2.id = c.id)
GROUP BY c.`listener`
Sign up to request clarification or add additional context in comments.

3 Comments

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXISTS (SELECT 0 FROM calls c2 WHERE c2.id = c.id) GROUP BY c.listener LIM' at line 1
I don't think you can do more than this... maybe just summing call time can be like this - SEC_TO_TIME(SUM(c.call_time))
About your edit; i was using that for CONCAT; it eliminates records with same listener and same time. This query doesn't do same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.