【发布时间】:2017-05-07 02:10:23
【问题描述】:
有一个格式为 cmets 的表格
| ID |ID_PARENT| ...
如果 parent 等于 0 那么它是根注释,children 有对 parent 的引用。
只有 2 级评论层次结构。因此,父 cmets 及其所有答案都是第二级。
目前在 2 个查询中被选中:
-
父母(自动递增)优先
select id, txt from comments where id_parent = 0 order by id desc limit 500 -
那么来自第一个查询的所有 id 都存储在数组中,对孩子的查询是,即
select id, txt, id_parrent from comments where id_parent in (58286, 55857, 54242, 53937, 53770, 52825, 51765, 51204, 50996, 50810, 44735, 43680, 43576, 42336, 41440, 41157, 39715, 38973, 38614, 36560, 36331, 36099, 35819, 35280, 33950, 33607, 33503, 32802, 30689, 27807, 27712, 26821, 25895, 23927, 23485, 23433, 22709, 22706, 22252, 21203, 20293, 20041, 19824, 19619, 19560, 19233, 17209, 17129, 16879, 16822, 16602, 14060, 13992, 13986, 13137, 13074, 12294, 10729, 10698, 10690, 10689, 10687, 10679, 10677) order by id_parent desc, id asc
然后从第二个查询的结果中选择子代对父代进行迭代。 有时有 500 个 id,看起来很糟糕......
有没有可能的优化?
【问题讨论】:
标签: mysql performance