创建记录列表并稍后插入它们PHP MYSQL

时间:2017-06-02 13:12:21

标签: php mysql moodle

我需要控制输入每个子页面的人和时间,并将此信息插入MySQL。我有MYSQL表,它将获取信息(userid,subpage,subpageid,datetime)。

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上调用此函数。

它工作正常,但我需要优化它一点。有没有什么办法可以在DB空闲时插入这些记录,或创建列表并每小时插入一次?

//修改

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

1 个答案:

答案 0 :(得分:0)

在Moodle / PHP中没有标准方法来延迟查询,直到数据库空闲为止。您可以在最新的MySQL中删除“INSERT DELAY”,如您所见:https://dev.mysql.com/doc/refman/5.7/en/insert-delayed.html 我可以提出两个选择:

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

  2. 为此日志使用其他服务器 - 因此负载将分布在更多服务器上。