Jump to content

[SOLVED] Index Not Getting Used


neoform

Recommended Posts

I'm making a messaging system, but have run into a problem with one of my queries not using the proper index. I keep getting a "ALL" for the query. If I don't have the right index, what should it be?

 

SELECT
bodies.posted_on,	
bodies.user_id,		
recipients.thread_id id,
SUM(IF(bodies.id > recipients.viewed_body_id, 1, 0)) new_messages
FROM			
system_messages_recipients recipients
INNER JOIN
system_messages_bodies bodies
ON
recipients.thread_id = bodies.thread_id
WHERE							
recipients.recipient_user_id = '1'
AND
bodies.id > recipients.delete_body_id

 

+----+-------------+------------+--------+-------------------+---------+---------+-----------------------------------+--------+-------------+
| id | select_type | table      | type   | possible_keys     | key     | key_len | ref                               | rows   | Extra       |
+----+-------------+------------+--------+-------------------+---------+---------+-----------------------------------+--------+-------------+
|  1 | SIMPLE      | bodies     | ALL    | PRIMARY,posted_on | NULL    | NULL    | NULL                              | 400232 |             |
|  1 | SIMPLE      | recipients | eq_ref | PRIMARY           | PRIMARY | 7       | phpneoform.bodies.thread_id,const |      1 | Using where |
+----+-------------+------------+--------+-------------------+---------+---------+-----------------------------------+--------+-------------+

 

-- Table "system_messages_bodies" DDL

CREATE TABLE `system_messages_bodies` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `thread_id` int(10) unsigned NOT NULL,
  `posted_on` datetime NOT NULL,
  `user_id` mediumint( unsigned NOT NULL,
  `body` varchar(60000) NOT NULL,
  `body_hash` binary(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `hash` (`body_hash`),
  KEY `posted_on` (`id`,`thread_id`,`posted_on`)
) ENGINE=MyISAM AUTO_INCREMENT=400233 DEFAULT CHARSET=latin1;

 

-- Table "system_messages_recipients" DDL

CREATE TABLE `system_messages_recipients` (
  `thread_id` int(10) unsigned NOT NULL,
  `recipient_user_id` mediumint( unsigned NOT NULL,
  `viewed_body_id` int(10) unsigned NOT NULL,
  `delete_body_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`thread_id`,`recipient_user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Link to comment
https://forums.phpfreaks.com/topic/121266-solved-index-not-getting-used/
Share on other sites

MySql is notorious (possibly too strong a word) for not always using the index that you would want, if it thinks there is a more optimal one to use. If you know the name of the index you wish to use on a specific query, you can always use FORCE INDEX to specify the one you want.

 

http://dev.mysql.com/doc/refman/5.1/en/index-hints.html

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.