【问题标题】:Create list of records and insert them later PHP MYSQL创建记录列表并在以后插入它们 PHP MYSQL
【发布时间】:2017-06-02 13:12:21
【问题描述】:

我需要控制谁以及何时进入每个子页面,并将这些信息插入 MySQL。我有 MYSQL 表,它将获取信息(用户 ID、子页面、子页面 ID、日期时间)。

function logstore(){
global $DB, $USER, $CFG;


$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')
=== FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];

$currentUrl = $protocol . '://' . $host . $script;

$newsurl = $CFG->wwwroot.'/news/view.php';
$produrl = $CFG->wwwroot.'/prod/view.php';
$materialurl = $CFG->wwwroot.'/material/index.php';


$datetime = date_create()->format('Y-m-d H:i:s');


$records = new stdClass();
$records->userid = $USER->id;
if($currentUrl == $newsurl){
    $records->activity = 'news';
    $records->activityid = $_GET['id'];
}elseif ($currentUrl == $produrl){
    $records->activity = 'prod';
    $records->activityid = $_GET['id'];
}
elseif ($currentUrl == $materialurl){
    $records->activity = 'material';
    $records->activityid = $_GET['id'];
}

$records->datetime = $datetime;

$DB->insert_record('logstore', $records);}

我在 /news/view.php、/prod/view.php、/material/index.php 之上调用此函数。

它工作正常,但我需要对其进行一些优化。有什么方法可以在数据库空闲时插入这些记录,或者创建列表并每小时插入一次?

//编辑

我尝试了 INSERT DELAYED 但它不适用于我的数据库:(

【问题讨论】:

    标签: php mysql moodle


    【解决方案1】:

    在 Moodle/PHP 中没有标准的方法来延迟查询直到数据库空闲。如您在此处看到的,“插入延迟”在最新的 MySQL 中已弃用:https://dev.mysql.com/doc/refman/5.7/en/insert-delayed.html 我可以建议两个选项:

    1. 使用 http://mysqltuner.pl 检查您的数据库性能 - 也许更多的内存/缓存有助于提高性能。

    2. 为此日志使用另一台服务器 - 这样负载将分布在更多服务器上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      相关资源
      最近更新 更多