MySQL Forums
Forum List  »  Optimizer & Parser

How can I get the 'EXISTS' or 'IN' use an index?
Posted by: Johannes Fosseus
Date: July 30, 2009 04:39AM

Hi

I have two tables 'notice' and 'friend' - and one problem. I want to get all notice-info from my friends, the two queries here below does that but seems to be not to optimized. For example the dont use my indexes?

How can I do this better?

How can I get the 'EXISTS' or 'IN' use an index?

SELECT notice.*
FROM notice
WHERE EXISTS(
SELECT friendId
FROM friend
WHERE friend.userId = 76
AND friend.friendId = notice.who )
ORDER BY notice.created
DESC LIMIT 20


SELECT notice.*
FROM notice
WHERE who IN (263,195,137,171,148,159,198,232,237,165,245,228,262,260,261,248)
ORDER BY created
DESC LIMIT 20

Table notice have index on, 'who' and on 'created'
Table friendhave index on, 'userId' and on 'friendId'



Many thanks / johannes



Edited 1 time(s). Last edit at 07/30/2009 04:41AM by Johannes Fosseus.

Options: ReplyQuote


Subject
Views
Written By
Posted
How can I get the 'EXISTS' or 'IN' use an index?
3146
July 30, 2009 04:39AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.