To learn MySQL (innodb) I’m attempting to write a twitter app with 3 tables: users2, follow2, and tweets2.
Doing an explain as follows shows all the rows in tweets2 being possibly touched. Is this accurate? If you are going to do a WHERE username IN (select) I would assume that given username is indexed MySQL would retrieve the sub-select clause and go directly to the rows in tweets2 which have those usernames.
explain select username, timestamp, tweet
from tweets2
where username in
(select followee from follow2 where follower='user1')
order by timestamp desc;
Heres the SQLAlchemy code creating the tables:
users = Table('users2', metadata,
Column('username', String(16), index=True),
Column('email', String(256)),
)
follow = Table('follow2', metadata,
Column('follower', String(16), index=True),
Column('followee', String(16), index=True),
)
tweets = Table('tweets2', metadata,
Column('id', Integer, primary_key=True),
Column('username', String(16), index=True),
Column('timestamp', BigInteger),
Column('tweet', String(200)),
)
In
MySQL, the select statement inside theINclause will always be driven (though the indexes may still be used on it).Replace it with a
JOIN: