I have a mysql query that gets a list of private messages where a user is either the sender, or receiver.
SELECT
users_user1.user_name AS pm_username_1,
users_user1.user_avatar AS pm_username_1_avatar,
users_user2.user_name AS pm_username_2,
users_user2.user_avatar AS pm_username_2_avatar,
pms.*
FROM pm pms
LEFT JOIN users users_user1
ON users_user1.user_id = pms.pm_sender
LEFT JOIN users users_user2
ON users_user2.user_id = pms.pm_receiver
WHERE pm_thread = pm_id
AND (pm_receiver = '1' OR pm_sender = '1')
AND pm_delete != '1'
ORDER by pm_thread_last DESC LIMIT 0, 15
The problem is.... as far as I can tell... it cannot use any index.
Any way I can get around that?
EDIT
+----+-------------+-------------+--------+---------------+---------+---------+------------------------+-------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+--------+---------------+---------+---------+------------------------+-------+-----------------------------+
| 1 | SIMPLE | pms | ALL | pm_receiver | NULL | NULL | NULL | 25354 | Using where; Using filesort |
| 1 | SIMPLE | users_user1 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_sender | 1 | |
| 1 | SIMPLE | users_user2 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_receiver | 1 | |
+----+-------------+-------------+--------+---------------+---------+---------+------------------------+-------+-----------------------------+
Altered the schema to this:
(SELECT
users_user1.user_name AS pm_username_1,
users_user1.user_avatar AS pm_username_1_avatar,
users_user2.user_name AS pm_username_2,
users_user2.user_avatar AS pm_username_2_avatar,
pms.*
FROM pm pms
LEFT JOIN users users_user1
ON users_user1.user_id = pms.pm_sender
LEFT JOIN users users_user2
ON users_user2.user_id = pms.pm_receiver
WHERE pm_thread = pm_id
AND (pm_receiver = '1')
AND pm_delete != '1')
UNION
(SELECT
users_user1.user_name AS pm_username_1,
users_user1.user_avatar AS pm_username_1_avatar,
users_user2.user_name AS pm_username_2,
users_user2.user_avatar AS pm_username_2_avatar,
pms.*
FROM pm pms
LEFT JOIN users users_user1
ON users_user1.user_id = pms.pm_sender
LEFT JOIN users users_user2
ON users_user2.user_id = pms.pm_receiver
WHERE pm_thread = pm_id
AND (pm_sender = '1')
AND pm_delete != '1')
ORDER by pm_thread_last DESC LIMIT 0, 15
EXPLAIN
+----+--------------+-------------+--------+---------------+-------------+---------+------------------------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+-------------+--------+---------------+-------------+---------+------------------------+------+----------------+
| 1 | PRIMARY | pms | ref | pm_receiver | pm_receiver | 4 | const | 336 | Using where |
| 1 | PRIMARY | users_user1 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_sender | 1 | |
| 1 | PRIMARY | users_user2 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_receiver | 1 | |
| 2 | UNION | pms | ref | pm_sender | pm_sender | 4 | const | 283 | Using where |
| 2 | UNION | users_user1 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_sender | 1 | |
| 2 | UNION | users_user2 | eq_ref | PRIMARY | PRIMARY | 4 | movies.pms.pm_receiver | 1 | |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using filesort |
+----+--------------+-------------+--------+---------------+-------------+---------+------------------------+------+----------------+