Jump to content

Getting rid of a filesort.


mattal999

Recommended Posts

I'm trying to optimise some queries that seem to be using temporary tables and filesorts, and stumbled across this nasty one. I have the following query:

 

SELECT * FROM `logs` ORDER BY `datetime` DESC LIMIT 0, 50

 

Here's the table structure:

 

CREATE TABLE IF NOT EXISTS `logs` (
  `id` mediumint(12) unsigned NOT NULL AUTO_INCREMENT,
  `userid` mediumint(10) unsigned NOT NULL,
  `objectid` mediumint(10) unsigned NOT NULL,
  `datetime` datetime NOT NULL,
  `action` varchar(100) NOT NULL,
  `reason` mediumtext NOT NULL,
  PRIMARY KEY (`id`),
  KEY `datetime` (`datetime`)
) ENGINE=MyISAM ;

 

Now, by my understanding, that query should use the datetime key. But instead, it decides to do a filesort.

 

id	select_type	table		type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE		logs		ALL	NULL		NULL	NULL	NULL	11	Using filesort

 

What am I doing wrong?

Link to comment
Share on other sites

Your problem is that you should add a where clause, using datetime.

 

http://venublog.com/2007/11/29/mysql-how-to-avoid-filesort/

 

So, the conclusion:

- You can avoid the filesort by making order by column appear in the where clause

- When using join, make sure the left side join table column is used in the ORDER BY clause or change the join type

Link to comment
Share on other sites

Thanks for your reply. That article is interesting, and I've tried it but it doesn't seem to work...

 

Here's the ammended query:

 

SELECT * FROM `logs` WHERE `datetime` > '1970-01-01 00:00:00' ORDER BY `datetime` DESC LIMIT 0, 50

 

And this results in:

 

id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE		logs	ALL	datetime	NULL	NULL	NULL	11	Using where; Using filesort

 

I'll give the join idea a whirl soon, but as far as I can see, that should have worked...

Link to comment
Share on other sites

Well, I need to select everything because I want the 50 most recent rows from that table.

 

EDIT: Funny thing is:

 

SELECT * FROM  `logs` FORCE INDEX ( DATETIME ) ORDER BY  `datetime` DESC LIMIT 0 , 50

 

Outputs:

 

id	select_type	table	type	possible_keys	key		key_len	ref	rows	Extra
1	SIMPLE		logs	index	NULL		datetime	8	NULL	11	 

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.