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

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败