I have setup my news script so it will display the latest 5 posts (from the same category as the currently viewed post - excluding the currently viewed post).
My SQL looks like this :
SELECT title, sid, url, category
FROM news
WHERE category = ? AND sid <> ? ORDER BY sid DESC LIMIT 5
Here is the EXPLAIN for the query :
+----+-------------+----------+------+------------------+----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+------------------+----------+---------+-------+------+-------------+
| 1 | SIMPLE | news | ref | PRIMARY,category | category | 98 | const | 154 | Using where |
+----+-------------+----------+------+------------------+----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
What I'm wondering is - is there any way to optimize my query so it doesn't have to scan so many rows to get the 5 results?
EDIT
Results with Category Index :
mysql> EXPLAIN EXTENDED SELECT title, sid, url, category FROM news WHERE category = ? AND sid <> ? ORDER BY sid DESC LIMIT 5\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: news
type: ref
possible_keys: PRIMARY,category
key: category
key_len: 98
ref: const
rows: 156
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
Results without Category Index :
mysql> EXPLAIN EXTENDED SELECT title, sid, url, category FROM news WHERE category = ? AND sid <> ? ORDER BY sid DESC LIMIT 5\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: news
type: index
possible_keys: PRIMARY
key: PRIMARY
key_len: 2
ref: NULL
rows: 5
filtered: 7420.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)