MySQL Forums
Forum List  »  Optimizer & Parser

How to avoid filesort
Posted by: Jim Daemon
Date: June 11, 2006 06:50PM

I am trying to optimize a query that for the life of me I cannot figure out why it is doing a filesort and taking a bit of time just to return 1 record.

I would be *extremely* grateful if someone could assist me in resolving this issue.

The structure of the 2 tables involved follows:

CREATE TABLE `reviews` (
`reviews_id` int(11) NOT NULL auto_increment,
`products_id` int(11) NOT NULL default '0',
`customers_id` int(11) default NULL,
`customers_name` varchar(64) NOT NULL default '',
`reviews_rating` int(1) default NULL,
`date_added` datetime default NULL,
`last_modified` datetime default NULL,
`reviews_read` int(5) NOT NULL default '0',
`approved` tinyint(3) unsigned NOT NULL default '0',
PRIMARY KEY (`reviews_id`),
KEY `products_id` (`products_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;


CREATE TABLE `reviews_description` (
`reviews_id` int(11) NOT NULL default '0',
`languages_id` int(11) NOT NULL default '0',
`reviews_text` text NOT NULL,
PRIMARY KEY (`reviews_id`,`languages_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


Both tables contain 910 records as they are a 1 to 1 relation.

I have analyzed and optimized both tables.

The query I am running is:
SELECT r.reviews_id, rd.reviews_text, r.reviews_rating, r.date_added, r.customers_name
FROM reviews r, reviews_description rd
WHERE r.products_id = '1151'
AND r.reviews_id = rd.reviews_id
AND r.approved = '1'
AND rd.languages_id = '1'
ORDER BY r.reviews_id DESC
LIMIT 5


The output from Explain is:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE r ref PRIMARY,products_id products_id 4 const 1 Using where; Using filesort
1 SIMPLE rd eq_ref PRIMARY PRIMARY 8 xr.r.reviews_id,const 1

Sorry about the output display. I could not figure out how to get the forum to display fixed-width (e.g. <pre>) font.

Can anyone tell me what I have wrong and how to avoid the filesort?

Great spirits have always found violent opposition from mediocre minds. The latter cannot understand it when a person does not thoughtlessly submit to hereditary prejudices but honestly and courageously uses their intelligence.

Options: Reply• Quote


Subject
Views
Written By
Posted
How to avoid filesort
13040
June 11, 2006 06:50PM
7142
June 12, 2006 08:40AM
6377
June 12, 2006 09:10AM
6428
June 12, 2006 01:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.