Psytherium
07-26-2007, 12:21 AM
I'm returning 20 cities that have the most reviews. There are about 41900 rows in the reviews table, and about 140000 total in the apartments table. I've pasted the select query that I use and the explain for it. It takes 1.19 seconds to perform this query. I would love for this to run sub 100/200ms, but is that an unreasonable goal? I have a quad core 3.00ghz xeon, 2GB ram, and a 10k SAS drive. Any help/criticism would be GREATLY appreciated.
Thanks,
Jon
mysql> SELECT COUNT(r.rid) as 'reviewCount', a.city, a.state
->FROM apartments a
->JOIN reviews r ON r.aid = a.aid
->WHERE a.state = 'TX'
->GROUP BY a.city, a.state
->ORDER BY reviewCount DESC
->LIMIT 20;
20 rows in set (1.19 sec)
mysql> EXPLAIN SELECT COUNT(r.rid) as 'reviewCount', a.city, a.state
-> FROM apartments a
-> JOIN reviews r ON r.aid = a.aid
-> WHERE a.state = 'TX'
-> GROUP BY a.city, a.state
-> ORDER BY reviewCount DESC
-> LIMIT 20;
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
| id | select_type | table | type | possible_keys | key |
key_len | ref | rows | Extra
|
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
| 1 | SIMPLE | a | ref | PRIMARY,state | state |
53 | const | 13209 | Using where; Using temporary; U
sing filesort |
| 1 | SIMPLE | r | ref | aid | aid |
9 | ardb.a.aid | 3 | Using where
|
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
2 rows in set (0.00 sec)
Thanks,
Jon
mysql> SELECT COUNT(r.rid) as 'reviewCount', a.city, a.state
->FROM apartments a
->JOIN reviews r ON r.aid = a.aid
->WHERE a.state = 'TX'
->GROUP BY a.city, a.state
->ORDER BY reviewCount DESC
->LIMIT 20;
20 rows in set (1.19 sec)
mysql> EXPLAIN SELECT COUNT(r.rid) as 'reviewCount', a.city, a.state
-> FROM apartments a
-> JOIN reviews r ON r.aid = a.aid
-> WHERE a.state = 'TX'
-> GROUP BY a.city, a.state
-> ORDER BY reviewCount DESC
-> LIMIT 20;
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
| id | select_type | table | type | possible_keys | key |
key_len | ref | rows | Extra
|
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
| 1 | SIMPLE | a | ref | PRIMARY,state | state |
53 | const | 13209 | Using where; Using temporary; U
sing filesort |
| 1 | SIMPLE | r | ref | aid | aid |
9 | ardb.a.aid | 3 | Using where
|
+----+-------------+-------+------+------------------------+-------------------+
---------+-----------------------------+-------+--------------------------------
--------------+
2 rows in set (0.00 sec)