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 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路