使用查询定期填充视图数据

发布于 2024-12-19 13:56:15 字数 109 浏览 0 评论 0原文

我正在编写一个包含每日/每周/每月报告数据的视图。我认为仅定期运行查询来更新数据而不是在有人加载页面时访问数据库是有意义的。这可以完全在 PHP 和 MySQL 中完成吗?有哪些可靠的方法来处理这个问题?

I'm writing a view that will have daily/weekly/monthly report data. I'm thinking it makes sense to only run a query periodically to update the data rather than hit the database whenever someone loads the page. Can this be done completely in PHP and MySQL? What are some robust ways to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

写给空气的情书 2024-12-26 13:56:15

使用像 Smarty 这样支持缓存的模板引擎,您可以为这些页面设置较长的缓存时间。然后,您需要编写 PHP 代码来测试日期约束是否已更改以及数据是否尚未缓存,如果这些条件之一为真,则执行查询。否则,Smarty 将仅加载缓存的页面,而您的代码将不会查询数据库。

$smarty = new Smarty();
if (!$smarty->isCached('yourtemplate.tpl')) {
  // Run your query and populate template variables
}
$smarty->display('yourtemplate.tpl');

有关 Smarty 缓存的更多文档

Using a templating engine like Smarty that supports caching, you can set a long cache time for those pages. You then need to code your PHP to test whether your date constraints have changed and if the data is not already cached, and if either of those conditions is true, perform the query. Otherwise, Smarty will just load the cached page and your code won't query the database.

$smarty = new Smarty();
if (!$smarty->isCached('yourtemplate.tpl')) {
  // Run your query and populate template variables
}
$smarty->display('yourtemplate.tpl');

Further documentation on Smarty caching

转身以后 2024-12-26 13:56:15

是的,但不是很好。您想要查看 Cron 作业,大多数 Web 主机都提供设置 Cron 的服务。它们只是运行脚本、任何脚本、PHP、Javascript、整个页面等的一种方式。

在 google 上搜索 cron 作业,您应该会找到您要查找的内容。

如果您的网络主机不提供 cron 作业,并且您不知道 Unix 命令如何工作,那么有些网站将为您托管 cron 作业。

查看

http://www.cronjobs.org/

Yes but not very well. You want to look into Cron jobs, most Web hosts provide a service to setup Crons. They are simply a way to run a script, any script, PHP, Javascript, a whole page etc.

Search google for cron jobs, you should find what you're looking for.

If your web host doesn't provide cron jobs and you don't know how Unix commands work, then there are sites that will host a cron job for you.

Check out

http://www.cronjobs.org/

鸩远一方 2024-12-26 13:56:15

我认为只定期运行查询来更新数据而不是在有人加载页面时访问数据库是有意义的

就我个人而言,我会同时选择这两种方法。例如

SELECT customer, COUNT(orders.id), SUM(order_lines.value)
FROM orders, order_lines
WHERE orders.id=order_lines.order_id
AND orders.placed>@last_time_data_snapshotted
AND orders.customer=@some_user
GROUP BY customer
UNION
SELECT user, SUM(rollup.orders), SUM(rollup.order_value)
FROM rollup
WHERE rollup.last_order_date<@last_time_data_snapshotted
AND rollup.customer=@some_user
GROUP BY customer

而不是每当有人加载页面时就访问数据库

实际上,根据使用模式,这可能很有意义。但这并不一定排除上述方法 - 只需设置一个阈值,确定何时将聚合数据推送到预合并表中,并测试每个请求的阈值。

I'm thinking it makes sense to only run a query periodically to update the data rather than hit the database whenever someone loads the page

Personally I'd go with both. e.g.

SELECT customer, COUNT(orders.id), SUM(order_lines.value)
FROM orders, order_lines
WHERE orders.id=order_lines.order_id
AND orders.placed>@last_time_data_snapshotted
AND orders.customer=@some_user
GROUP BY customer
UNION
SELECT user, SUM(rollup.orders), SUM(rollup.order_value)
FROM rollup
WHERE rollup.last_order_date<@last_time_data_snapshotted
AND rollup.customer=@some_user
GROUP BY customer

rather than hit the database whenever someone loads the page

Actually, depending on the pattern of usage this may make a lot of sense. But that doesn't necessary preclude the method above - just set a threshold on when you'll push the aggregated data into the pre-consolidated table and test the threshold on each request.

我不是你的备胎 2024-12-26 13:56:15

我个人倾向于将缓存的数据存储在文件中,然后如果该文件在特定时间范围内已更新,则只需读取该文件,如果没有更新,则进行更新(例如从数据库获取信息,写入文件)。

一些示例代码:

$cacheTime = 900; // 15 minutes
$useCache = false;
$cacheFile = './cache/twitter.cachefile';
// check time
if(file_exists($cacheFile)){
    $cacheContents = file_get_contents($cacheFile);
  if((date('U')-filemtime($cacheFile))<$cacheTime || filesize($cacheFile)==0){
    $useCache = true;
  }
}
if(!$useCache){
  // get all your update data setting $cacheContents to the file output. I'd imagine using a buffer here would be a good idea.

  // update cache file contents
  $fh = fopen($cacheFile, 'w+');
  fwrite($fh, $cacheContents);
  fclose($fh);
}

echo $cacheContents;

I'd personally go for the storing the cached data in a file, then just read that file if it has been updated within a certain timeframe, if not then do your update (e.g. getting info from the database, writing to the file).

Some example code:

$cacheTime = 900; // 15 minutes
$useCache = false;
$cacheFile = './cache/twitter.cachefile';
// check time
if(file_exists($cacheFile)){
    $cacheContents = file_get_contents($cacheFile);
  if((date('U')-filemtime($cacheFile))<$cacheTime || filesize($cacheFile)==0){
    $useCache = true;
  }
}
if(!$useCache){
  // get all your update data setting $cacheContents to the file output. I'd imagine using a buffer here would be a good idea.

  // update cache file contents
  $fh = fopen($cacheFile, 'w+');
  fwrite($fh, $cacheContents);
  fclose($fh);
}

echo $cacheContents;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文