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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.