I wrote this query to get the list of all the ids in user table, but when i run this I get Lost connection to MySQL server during query, as the no of rows in user table is more than 5 million
SELECT id FROM users.user
where user_type_id=(select id from users.user_type where user_type = 'admin')
and email_add not in
(select email_add from users.user
where user_type_id = (select id from users.user_type where user_type = 'normaluser'));
2
Answers
Prefer joins rather than
IN (subquery)
Make sure there’s an index on
user.user_type_id
anduser.email_add
.Changing your
not in
tonot exists
should be much better, assumingemail_add
is indexed:If it’s still super slow, please update your question with the DDL for your
user
table and the execution plan (EXPLAIN SELECT ...
).