【发布时间】:2015-02-08 01:27:31
【问题描述】:
如果针对大量资源运行 getChildIds 可能会非常慢,因此我正在尝试编写查询和一些代码来更快地获取所有子 ID。
但是,我从 getChildIds 和我的脚本中得到了不同的结果。
谁能明白为什么这些会产生不同的结果?
使用 getChildIDs 的方法:
$parentDepth = isset($scriptProperties['parentDepth']) ? $scriptProperties['parentDepth'] : 5;
$parents = explode(',', $parents);
$children = array();
foreach ($parents as $parent){
$ids = $modx->getChildIds($parent,10,array('context' => 'web'));
foreach($ids as $id){
$children[] = $id;
}
}
echo ' number of children = ' . count($children);
使用查询和循环的方法:
$comma_separated = implode(",", $parents);
$sql = "SELECT `id` from modx_site_content where `parent` IN (".$comma_separated.") and published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";
$results = $modx->query($sql);
$mychildren = array();
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$mychildren[] = $row['id'];
}
for($i=0; $i <= 10; $i++){
$comma_separated = implode(",", $mychildren);
$sql = "SELECT `id` from modx_site_content where `parent` IN (".$comma_separated.") and published = 1 and isfolder = 0 and deleted = 0 and hidemenu = 0;";
$results = $modx->query($sql);
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$mychildren[] = $row['id'];
}
}
echo ' number of children = ' . count($mychildren);
getChildIDs 方法需要将近 1.5 秒的时间来运行并提供大约 1100 个结果
SQL/脚本方法在 0.1 秒内运行,并给出 1700 个结果。
要么我没有将子 ID 正确地附加到数组中 ~ 或者 ~ 也许 getChildIDs 正在过滤掉一些其他结果?
有人知道这里会发生什么吗?
【问题讨论】:
标签: mysql modx-revolution