3
I’m in the process of optimizing my web app queries. But since I don’t have much experience I can’t find a solution to remove the use of FILESORT.
Does anyone have any idea what to do to end this FILESORT? Which indices to create?
SELECT `essences`.*, `brands`.`name` as brand_name, `brands`.`sigla`
FROM (`essences`)
INNER JOIN `brands` ON `brands`.`id` = `essences`.`brand_id`
WHERE essences.published = 1
ORDER BY `essences`.`name` asc
EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE brands ALL PRIMARY - - - 11 Using temporary; Using filesort
1 SIMPLE essences ref name name 4 receitadiy.brands.id 69 Using index condition
Table brands
CREATE TABLE `brands` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`description` TEXT NULL,
`sigla` VARCHAR(50) NULL DEFAULT NULL,
`url` VARCHAR(250) NULL DEFAULT NULL,
`published` TINYINT(4) NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `name` (`name`, `published`)
)
Table essences
CREATE TABLE `essences` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(150) NOT NULL,
`brand_id` INT(10) UNSIGNED NOT NULL,
`tag` CHAR(10) NULL DEFAULT NULL,
`desc` TEXT NULL,
`alert` TINYINT(2) NULL DEFAULT NULL,
`alert_color` VARCHAR(50) NULL DEFAULT NULL,
`alert_notes` TEXT NULL,
`preco_ml` DECIMAL(10,2) NOT NULL,
`published` TINYINT(4) NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `tag` (`tag`),
INDEX `name` (`brand_id`, `name`, `published`)
)
You really need two
ORDER BY
? Try to get these guys out...– gmsantos
I can only take one of the order by... but the order by Essences.name needs to be there. @Jorge B didn’t understand, has an index for Brands.id there.
– JotaKa
Isolate only the name here: INDEX
name
(brand_id
,name
,published
)– gmsantos
I think Mysql should do a filesort because vc indicates it should do an ORDER BY on "Essence" but it has no index for it. Try placing an index over "Essence".
– Peter
@gmsantos if I use only INDEX name (name) the number of Rows in EXPLAIN is equal to the number of registrants in the table 'Essences'. Here is the link to the fiddle
– JotaKa
http://venublog.com/2007/11/29/mysql-how-to-avoid-filesort/
– Jorge B.
@Jorgeb. I tried to change the query with the link suggestions but still couldn’t get rid of filesort.
– JotaKa
I’m trying to figure it out too.
– Jorge B.