I have data that is structured as follows:
Parent | Child
散 | ⺙
⺙ |
⺙ | 乂
散 | 龷
散 | 月
I am using the following query to return all the ancestors up to two levels above a search term (i.e., return parents and grandparents only).
SELECT a.parent AS level3, b.parent AS level2, c.parent AS level1
FROM decomposition_dup AS a
LEFT JOIN decomposition_dup AS b ON a.parent LIKE b.child
LEFT JOIN decomposition_dup AS c ON b.parent LIKE c.child
WHERE a.child LIKE '$searchterm'
The problem is that it is quite a slow query (~5 seconds). My EXPLAIN
shows:
I have already indexed the appropriate columns. Have I made an error? Or is there a better way to structure my query?
EDIT: Here is the table structure:
CREATE TABLE `decomposition_dup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`structure` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`child` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `parent` (`parent`),
KEY `child` (`child`),
KEY `parent_2` (`parent`,`child`)
) ENGINE=InnoDB AUTO_INCREMENT=211929 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci