I have a Modified Preorder Tree Traversal (MPTT) table with 82117 records. This mysql table contains some geographical data (Country, State, Region, City...)
CREATE TABLE IF NOT EXISTS `geotree` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`geo_id` int(11) DEFAULT NULL,
`name` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`lft` int(11) NOT NULL,
`rgt` int(11) NOT NULL,
`parent` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`,`lft`,`rgt`),
KEY `geo_id` (`geo_id`),
KEY `parent` (`parent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=82174 ;
The problem comes when I try to get a path using a standard MPTT query like this:
SELECT p.name
FROM `geotree` AS node, `geotree` AS p
WHERE node.lft BETWEEN p.lft AND p.rgt AND node.rgt='$value'
ORDER BY p.lft
It takes more than 2.5 seconds to complete. What's the problem? I have missed any index? Too much rows for this type of data structure? Or there is any mistake in the query?
NOTE: I've noted that if I remove the "ORDER BY" from the query, the query time decreases to 0.05 seconds.