php - SQL 和 PHP 聚合统计信息,使用 COUNT 数百次的性能问题

标签 php mysql sql

我有一个执行 1000 个 SQL 查询的 PHP 页面。它为用户列表提供了已发生事件的统计信息。页面加载时间有点长(调整索引后现在为 6 秒)。我想知道是否有比 1000 个单独查询更好的方法来做到这一点。有没有更快的方法,尤其是随着数据的增长。

这 1000 个 SQL 查询的结果被放入 PHP 数组并最终填充 html 表的单元格,如下所示:

         Installs    Called    Early Install   Event4   Event5    (... 9
George     5           6          3              5        29      different event
Greg       9           7          1              8        23      types, up to
David      4           1          2              4        0       maybe 15
Dan        15          17         4              20       10      eventually)
...        ...         ...        ...            ...      ...
...        ...         ...        ...            ...      ...
Totals     351         312        82             289      1220

(... there are up to ~50 users, maybe 100 total in the next two years)

有些列实际上是在 PHP 中根据 (event4/installs)*100 等数据动态计算的百分比。

表格总是在给定的数据范围内,例如:
选择日期范围:日期 2013 年 1 月 15 日 - 2013 年 3 月 31 日

event 表的字段:id、event_type、user_id、event_date

数据本身存储为由特定日期发生的事件组成的表格。 PHP 页面触发的最频繁的 SQL 语句类型是计数查询,如下所示:

SELECT COUNT(id)
FROM events
WHERE userid = 10
    AND `event_date` BETWEEN '2013-01-01' AND '2013-02-15'
    AND event_type = 'Install';

SELECT COUNT(id)
FROM events
WHERE userid = 10
    AND `event_date` BETWEEN '2013-01-01' AND '2013-02-15'
    AND event_type = 'Called';

SELECT COUNT(id)
FROM events
WHERE userid = 10
    AND `event_date` BETWEEN '2013-01-01' AND '2013-02-15'
    AND event_type = 'Early Install';

/* and so on for each event type and user id */

这些 counts() 填充 html 表格的单元格。它在遍历每个用户(代表 html 输出表中的每一行)的 php 循环中执行这些 counts() 并且在每一行中它遍历每个事件类型(列)并为 COUNT每一个。约 50 个用户,约 10 种事件类型,您在一个页面上获得约 1000 个单独的 SQL 请求。

  1. 是否有一种合理的方法来组合所有这些单独的 SQL COUNT 操作,或者在没有来自 PHP 的所有单独的 COUNT 调用的情况下更快或更正确地完成这一切?也许是一个存储过程……这有意义吗?如果是这样,如何处理(一堆计数查询或游标或什么)?以及如何从存储过程构造/返回计算计数数据行?

我想我想知道,这是“正确的方式”®吗?

我并不是要回答整个问题,一定要回答您可能能够回答的部分,或者您将如何回答。

另外(#2)如何缓存这些东西?通过将所有 COUNT 值带到 PHP,然后将这些值从 PHP 写到一个 mysql 表中来缓存,每个用户和每个日期范围都有一行,或者缓存在某个地方/其他地方?

最佳答案

想到分组。

SELECT userid, event_type, COUNT(id) AS cnt
FROM events
WHERE `event_date` BETWEEN '2013-01-01' AND '2013-02-15'
GROUP BY userid, event_type
ORDER BY userid, event_type

这将返回一个数组,其中每一行大致具有以下结构:

array(
    userid=>10,
    event_type=>'Installs',
    cnt=>5
);

您可以迭代它来构建您的表格。

//iterate over the data first constructing a new array for below
$newData = array();
$headers = array();

foreach($data as $row){
    //save the data in a multi dimensional array under the userid
    if(!isset($newData[$row['userid']])){
        $newData[$row['userid']]=array();
    }
    $newData[$row['userid']][$row['event_type']] = $row['cnt'];
    $headers[$row['event_type']]=1;
}
//get the headers
$headers = array_keys($headers);

//display the data for debugging
echo '<pre>'.print_r($newData,1).'</pre>';

echo "<table colspan=0 cellspacing=0 border=1>\n";
//add "user id" to the headers
array_unshift($headers, "User ID");
//echo the headers
echo "\t<thead>\n\t\t<th>".implode("</th>\n\t\t<th>", $headers)."</th>\n\t</thead>\n";
//remove the user id column from headers
array_shift($headers);

echo "\t<tbody>\n";
//now loop over the new data and display.
foreach($newData as $userID=>$row){
    //start row
    echo "\t\t<tr>\n";
    //user id
    echo "\t\t\t<td>{$userID}</td>\n";
    //loop over the headers. there should be corresponding keys for each header
    foreach($header as $key){
        //get the count if the key exists and '-' if not.
        $cnt = isset($row[$key])?$row[$key]:'-';
        echo "\t\t\t<td>{$cnt}</td>\n";
    }
    echo "\t\t</tr>\n";
}
echo "\t</tbody>\n</table>\n";

关于php - SQL 和 PHP 聚合统计信息,使用 COUNT 数百次的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143874/

相关文章:

sql - 在创建索引后重命名 SQLite 表/列/行

javascript - Websocket - WebSocket 握手期间出错 : Unexpected response code: 404

php - 通过 JSON 将 HTML 从 PHP 发送到 javascript

带有PDF输出的PHP PDF模板库?

php - 在 SQL 数据库中平均用户输入的表单数据

sql - Oracle SQL 查询根据值是否存在返回 1 或 0

通过异步 curl 调用时 Php 脚本停止

php - 在数组中跳过 mysql 数据库中的值

mysql - MySQL 中的索引大小

sql - 即使在where函数中也无法过滤null