dtddjq9637 2014-11-24 22:30
浏览 40
已采纳

如何在单个查询中获取Silverstripe中的200多个页面?

One of our Silverstripe sites is on shared hosting and having major performance issues. The issues seem to be caused by the shared SQL server throttling the number of queries that can be made.

The pages that are running the slowest get 200+ hundred pages to place on a Google Map:

$DirectoryItems = DirectoryItem::get()->where("\"Latitude\" IS NOT NULL AND \"Longitude\" IS NOT NULL ")->sort('Title ASC');
$MapItems = new ArrayList();
foreach ($DirectoryItems as $DirectoryItem) {
  $MapItems->push(new ArrayData(array(
      "Latitude" => $DirectoryItem->Latitude,
      "Longitude" => $DirectoryItem->Longitude,
      "MapMarkerURL" => $DirectoryItem->MapMarkerURL,
      "Title" => addslashes($DirectoryItem->Title),
      "Link" => $DirectoryItem->Link()
  )));
}

Each of the 200+ MapItems generate it's own SQL Query which is overloading the shared SQL server.

I started off trying to get the same information with a single query:

$DirectoryItems = DB::query('SELECT `DirectoryItem`.`Latitude`, `DirectoryItem`.`Longitude`, `DirectoryItem`.`MapMarkerURL`, `SiteTree_Live`.`Title`
FROM `DirectoryItem`, `SiteTree_Live`
WHERE `DirectoryItem`.`ID` = `SiteTree_Live`.`ID` 
AND `DirectoryItem`.`Latitude` IS NOT NULL AND `DirectoryItem`.`Longitude` IS NOT NULL
ORDER BY `SiteTree_Live`.`Title`');

$MapItems = new ArrayList();
foreach ($DirectoryItems as $DirectoryItem) {
  $MapItems->push(new ArrayData(array(
      "Latitude" => $DirectoryItem['Latitude'],
      "Longitude" => $DirectoryItem['Longitude'],
      "MapMarkerURL" => $DirectoryItem['MapMarkerURL'],
      "Title" => addslashes($DirectoryItem['Title']),
      "Link" => ??????
  )));
}

But this falls over when it comes to getting the link to the DirectoryItem.

I thought about adding the Link as a DB field in DirectoryItem but that's beginning to feel needless complicated for what should be a straightforward operation.

What is the best way of getting the information for 200+ DirectoryItems in a single query?

  • 写回答

2条回答 默认 最新

  • dqd3690 2014-11-25 08:36
    关注

    Did you have a look into caching? If you show the same items on the map all the time you don't need to hit the db on every request.

    See

    It takes a huge load off your server if you cache properly.

    If you still have problems when caching you should think about a better server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历