Mysql – Following thesql query is taking too much time for excecution

mysql-5.6

I have a MySQL table which is described in the following question.
Counting number of ids present in one table which is not presented on another for a particular user
The query which I am using is as follows.

SELECT count(*) coincidences
FROM   table1
WHERE  user_id = 12
AND    NOT EXISTS (SELECT 1
                   FROM   table2
                   WHERE  tableid = table1.id
                   AND    user_id = table1.user_id)

This query is taking too much time for considerably bigger table how can I improve the performance of this particular query.

Best Answer

table2 needs INDEX(tableid, user_id) (in either order).

If this does not solve the problem, please provide SHOW CREATE TABLE for both tables, plus EXPLAIN SELECT ....