duanguan5922 2013-08-08 00:24
浏览 41
已采纳

高效查询只返回行数

I have five different queries running on my about page showing basic data like the number of news stories we have on the site. I am using queries like this:

$sql4 = "SELECT `ride_id` FROM `tpf_rides` WHERE `type` LIKE '%Roller Coaster%'" ;
$result4 = $pdo->query($sql4);
$coasters = $result4->rowCount();

but wonder if there is a more efficient way. I've tried to minimize the load by only pulling id's but because I only need the count can the load be lightened even more?

Also these queries only really need to run once or twice per day, not every time the page is loaded. Can someone point me in the direction of setting this up? I've never had to do this before. Thanks.

  • 写回答

2条回答 默认 最新

  • dsfe167834 2013-08-08 00:26
    关注

    Yes there is a more efficient way. Let the database do the counting for you:

    SELECT count(*) as cnt
    FROM `tpf_rides`
    WHERE `type` LIKE '%Roller Coaster%';
    

    If all the counts you are looking for are from the tpf_rides table, then you can do them in one query:

    SELECT sum(`type` LIKE '%Roller Coaster%') as RollerCoaster,
           sum(`type` LIKE '%Haunted House%') as HauntedHouse,
           sum(`type` LIKE '%Ferris Wheel%') as FerrisWheel
    FROM `tpf_rides`;
    

    That would be even faster than running three different queries.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算