0

我有一个像下面这样的查询

SELECT
                t1.categoryId as level1Id, t2.categoryId as level2Id, t3.categoryId as level3Id, t4.categoryId as level4Id, t5.categoryId as level5Id, t6.categoryId as level6Id, t7.categoryId as level7Id
                FROM `productCategories` as t1
                LEFT JOIN productCategories as t2 ON t1.categoryId = t2.parentcategoryId
                LEFT JOIN productCategories as t3 ON t2.categoryId = t3.parentcategoryId
                LEFT JOIN productCategories as t4 ON t3.categoryId = t4.parentcategoryId
                LEFT JOIN productCategories as t5 ON t4.categoryId = t5.parentcategoryId
                LEFT JOIN productCategories as t6 ON t5.categoryId = t6.parentcategoryId
                LEFT JOIN productCategories as t7 ON t6.categoryId = t7.parentcategoryId
                WHERE t1.categoryId IN ('" . $categories . "')

productCategories 有大约 15000 行和 2020 个唯一父 ID。一旦我在一个数组中获得了所有唯一的类别 ID,我就会开始一个新的查询来搜索具有匹配类别 ID 的产品。所以在那一点上事情变得有点慢。关于如何改进此查询或创建索引的任何建议?

4

0 回答 0