Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
改进mysql注释查询2层结构_Mysql_Performance - Fatal编程技术网

改进mysql注释查询2层结构

改进mysql注释查询2层结构,mysql,performance,Mysql,Performance,有一个格式为注释的表 | ID |ID_PARENT| ... 如果父项等于0,则它是根注释,子项引用父项 只有两级注释层次结构。所以,家长的意见和所有答案都是第二级的 当前在2个查询中选择: 先按id排序的父项(自动递增) 然后,来自第一个查询的所有id都存储在数组中,子查询为,即 select id, txt, id_parrent from comments where id_parent in (58286, 55857, 54242, 53937, 53770, 52825,

有一个格式为注释的表

|  ID  |ID_PARENT| ...
如果父项等于0,则它是根注释,子项引用父项

只有两级注释层次结构。所以,家长的意见和所有答案都是第二级的

当前在2个查询中选择:

  • 先按id排序的父项(自动递增)

  • 然后,来自第一个查询的所有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,看起来很糟糕


有任何可能的优化吗?

下面的查询应该一次获得所有这些注释,并按id顺序将父项排序在其子项之前

SELECT case when parent_id = 0 
THEN parent_id
ELSE id
END as sort_id, comments.* FROM comments ORDER BY sort_id, parent_id, id ASC
这就是你想做的吗

如果您可以访问保存内容的方式,并且可以在作为顶级注释时将parent_id值更改为NULL,则可以通过执行以下操作加快此查询的速度:

SELECT IFNULL(parent_id, id) as sort_id, comments.*
FROM comments ORDER BY sort_id, parent_id, id ASC
SELECT IFNULL(parent_id, id) as sort_id, comments.*
FROM comments ORDER BY sort_id, parent_id, id ASC