开发者

Nested Set indices & performance

开发者 https://www.devze.com 2023-01-30 12:36 出处:网络
I\'m having some troubles understanding what indices to use on a Nested-Set model. The query is: SELECT `node`.`id`,(COUNT(parent.id) - 1) AS `depth`,`name` FROM `categories` AS `parent`

I'm having some troubles understanding what indices to use on a Nested-Set model. The query is:

SELECT `node`.`id`,(COUNT(parent.id) - 1) AS `depth`,`name` FROM `categories` AS `parent` 
INNER JOIN `categories` AS `node` ON (`node`.`lft` BETWEEN parent.lft AND parent.rgt)
INNER JOIN `filebank_categories` ON (`node`.`id` = `filebank_categories`.`category_id` AND `filebank_categories`.`filebank_id` = 136)
INNER JOIN `categories_names` ON (`categories_names`.`category_id` = `node`.`id` AND `categories_names`.`language_id` = 1) 
WHERE `node`.`system_id` = parent.system_id 
GROUP BY node.id 
ORDER BY `node`.`lft` ASC

This query takes ~350ms with ~5000 rows in categories. EXPLAIN gives this:

1   SIMPLE  filebank_categories     ref     fk_filebank_categories_categories1,filebank_id          filebank_id 5   const                               474 Using where; Using temporary; Using filesort
1   SIMPLE  node                    eq_ref  PRIMARY,lft,category,cat,lft,rgt,system,id,lft,system   PRIMARY     4   filebank_categories.category_id     1    
1   SIMPLE  parent                  ref     lft,category,system                                     system      5   node.system_id                      50  Using where
1   SIMPLE  categories_names        eq_ref  PRIMARY,fk_categories_names_categories1                 PRIMARY     8   node.id,const                       1   Using where

Table structure:

CREATE TAB开发者_JS百科LE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `system_id` int(11) DEFAULT NULL,
  `lft` int(11) DEFAULT NULL,
  `rgt` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `lft,category` (`lft`,`id`),
  KEY `cat,lft,rgt` (`id`,`lft`,`rgt`),
  KEY `system` (`system_id`),
  CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`system_id`) REFERENCES `systems` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11519 DEFAULT CHARSET=utf8;

Any ideas how to improve this? Is it even possible? I'm not very experienced in database-optimization so I can't really figure out what indices to use here (and why).

Thanks.


You can try to move the WHERE clause to join, like

SELECT ...
INNER JOIN `categories` AS `node` ON 
(
  node.system_id=parent.system_id AND 
  node.lft BETWEEN parent.lft AND parent.rgt
)

And refine the index as :

CREATE TABLE `categories` (
  ...
  KEY `system_id,lft,rgt` (`system_id`,`lft`,`rgt`),
  ...
);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号