I am trying to process raw IIS log files and insert them into a MySQL
database. I have no problem accomplishing this, but the php code runs
very slow.
Right now, it is processing 10,000 lines in a log file every ~300
seconds. I am dealing with daily log files with records over 500,000.
It takes hours to process a daily file.
<?php
error_reporting (E_ALL);
$server = "localhost:3306 "; // db host
$user = ""; // db username
$pswd = ""; // db password
$db = ""; // db name
$DB_link = mysql_connect($ server,$user,$p swd) OR DIE (mysql_error()) ;
mysql_select_db ($db, $DB_link) OR DIE (mysql_error()) ;
$start_time = time();
$handle = fopen("/logs/ex050830.log", "r");
$linecnt = 0;
$totalcnt = 0;
while (!feof($handle) ) {
$list = array();
$buffer = fgets($handle, 20000);
if (! preg_match("/^\s*?#/", $buffer) ){
$linecnt++;
$line = split(" ", $buffer);
$stmt = "INSERT INTO logs ( `hit_date` , `hit_time` , `s-sitename` ,
`s-computername` , ".
"`s-ip` , `cs-method` , `cs-uri-stem` , `cs-uri-query` ,
`s-port` , `cs-username` , `c-ip` , ".
"`cs-version` , `User-Agent` , `Cookie` , `Referer` ,
`cs-host` , `sc-status` , `sc-substatus` , ".
"`sc-win32-status` , `sc-bytes` , `cs-bytes` , `time_taken` )
".
" VALUES ( '".$line[0]."', '".$line[1]."', '".$line[2]."',
'".$line[3]."', '".$line[4]."', ".
"'".$line[5]."','".$line[6]."','".$line[7]."','".$line[8]."','".$line[9]."',
".
"'".$line[10]."','".$line[11]."','".$line[12]."','".$line[13]."','".$line[14]."','".$line[15]."',
".
"'".$line[16]."','".$line[17]."','".$line[18]."','".$line[19]."','".$line[20]."','".trim($li ne[21])."'
)";
@mysql_query($s tmt);
if( $linecnt >= 10000 ){
$totalcnt += $linecnt;
echo "[ ".$totalcnt ." ( ". ( time() - $start_time) ." )
]\t";
$linecnt = 0;
}
}
}
fclose($handle) ;
?>
database. I have no problem accomplishing this, but the php code runs
very slow.
Right now, it is processing 10,000 lines in a log file every ~300
seconds. I am dealing with daily log files with records over 500,000.
It takes hours to process a daily file.
<?php
error_reporting (E_ALL);
$server = "localhost:3306 "; // db host
$user = ""; // db username
$pswd = ""; // db password
$db = ""; // db name
$DB_link = mysql_connect($ server,$user,$p swd) OR DIE (mysql_error()) ;
mysql_select_db ($db, $DB_link) OR DIE (mysql_error()) ;
$start_time = time();
$handle = fopen("/logs/ex050830.log", "r");
$linecnt = 0;
$totalcnt = 0;
while (!feof($handle) ) {
$list = array();
$buffer = fgets($handle, 20000);
if (! preg_match("/^\s*?#/", $buffer) ){
$linecnt++;
$line = split(" ", $buffer);
$stmt = "INSERT INTO logs ( `hit_date` , `hit_time` , `s-sitename` ,
`s-computername` , ".
"`s-ip` , `cs-method` , `cs-uri-stem` , `cs-uri-query` ,
`s-port` , `cs-username` , `c-ip` , ".
"`cs-version` , `User-Agent` , `Cookie` , `Referer` ,
`cs-host` , `sc-status` , `sc-substatus` , ".
"`sc-win32-status` , `sc-bytes` , `cs-bytes` , `time_taken` )
".
" VALUES ( '".$line[0]."', '".$line[1]."', '".$line[2]."',
'".$line[3]."', '".$line[4]."', ".
"'".$line[5]."','".$line[6]."','".$line[7]."','".$line[8]."','".$line[9]."',
".
"'".$line[10]."','".$line[11]."','".$line[12]."','".$line[13]."','".$line[14]."','".$line[15]."',
".
"'".$line[16]."','".$line[17]."','".$line[18]."','".$line[19]."','".$line[20]."','".trim($li ne[21])."'
)";
@mysql_query($s tmt);
if( $linecnt >= 10000 ){
$totalcnt += $linecnt;
echo "[ ".$totalcnt ." ( ". ( time() - $start_time) ." )
]\t";
$linecnt = 0;
}
}
}
fclose($handle) ;
?>
Comment