A couple of thoughts
- For your two queries, look at the
EXPLAIN
plan to see how mysql is limiting the data
You can do this by running
EXPLAIN SELECT ID,INTERESTS FROM users WHERE CITY = (SELECT CITY FROM users WHERE ID=124)
EXPLAIN SELECT b.ID,b.INTERESTS FROM users a,users b WHERE a.CITY = b.CITY AND a.ID=124
I've found this to be a hepful read when it comes to optimizing queries based on their explain plan.
- For good performance, since you are basically joining on the
CITY
column, consider creating an index on the CITY
column in the users
table.
Indexes are a trade-off between space and performance.
You can create one by running
CREATE INDEX idx_city ON users(CITY);
Doing this should make both queries perform well.