How can I optimize this mysql query? I'm using the IN()
operator, but I believe it's not the way to do it.
select *
from users
where usid NOT in
(
select usid
from images
where status=0
)
and us_status=0
How can I optimize this mysql query? I'm using the IN()
operator, but I believe it's not the way to do it.
select *
from users
where usid NOT in
(
select usid
from images
where status=0
)
and us_status=0
Using a LEFT OUTER JOIN
SELECT users.*
FROM users
LEFT OUTER JOIN images ON user.usid = images.usid AND images.status = 0
WHERE images.usid IS NULL
AND us_status = 0
This avoids using IN which can perform poorly.
SELECT users.*
FROM users
LEFT JOIN images ON users.usid = images.usid AND images.status=1 AND images.usid IS NOT NULL
WHERE users.us_status = 0
You can use the following query :
SELECT us.*
FROM users as us
INNER JOIN images as img ON us.usid = img.usid AND img.status=1
WHERE us.us_status = 0
Must read the article : http://www.w3schools.com/sql/sql_join.asp