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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!