dsxsou8465 2013-03-22 04:46
浏览 62
已采纳

从数据库收集统计数据

I have an app that's competition based, and includes (lets say) 10 types of biscuit (cookies) and 6 types of drinks. I'm using MODx Revolution 2.2 as the CMS.

Entrants basically specify which kind of drink they'd like and enter a barcode (relating to the type of biscuit).

I want to present this inside the CMS as a statistic, but I imagine it's quite resource intensive to query the database for each COUNT().

$biscuit1 = $modx->query("SELECT COUNT(*) FROM my_table WHERE barcode = '1234'");
$biscuit1 = $modx->fetch(PDO::FETCH_ASSOC);
$biscuit1 = $biscuit1[0];

So my questions are:

  • Is doing 16 database queries (possibly more) bad practice?
  • If there is a better way of gathering statistics on datasets, what is it?

Note: I had a thought of creating another statistics table, where I run these queries in a cron job on the hour. Would this help?

  • 写回答

1条回答 默认 最新

  • duankun9280 2013-03-22 04:49
    关注

    COUNT(*) is not all that resource intensive. Also it would help if barcode was indexed. Also you can grab the data for all barcodes in one query rather than having to run 16 queries, one for each

    $result = $modx->query("SELECT barcode,COUNT(*) FROM my_table GROUP by barcode");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题