[英]How to increase speed of PHP script that using MySQL?
我編寫了一個PHP腳本,使用該庫從excel文件中讀取一些數據。
然后以任意順序將其插入MySQL數據庫中的某些表。
此Excel文件具有200行50列,通常執行時間超過60秒。 因為我的PHP托管不允許我使用set_time_limit(0);
在您看來,如何提高腳本速度?
我的腳本讀取每一行:
function read_row($excelFile, $r){
require_once './inc/excel_reader2.php';
$data = new Spreadsheet_Excel_Reader($excelFile, FALSE);
$col[0] = $data->val($r, 'E');
$col[1] = $data->val($r, 'F');
...
$col[49] = $data->val($r, 'BZ');
return $col;
}
我的將數據插入MySQL的腳本:
$count = $user->rowcount($sheet=0);
if($final>0){
$count = $final;
}
$users = 0;
for($i = 4; $i <= $count; $i++){
$f = $user->val($i,'D');
$c = $user->val($i,'B');
$l = $user->val($i,'C');
$query = "INSERT INTO users(fullname ,ncode, location, createdtime) VALUES('$f','$c','$l', now());";
if(mysql_query($query)){
$users++;
}
$row = read_row($excelFile, $i);
$query = "INSERT INTO `payments` (`id`, `year`, `month`, `user`, `p1`, `p2`,... `p50`) VALUES"
. " (NULL, '$y', '$m', '$c', '$row[0]', '$row[1]', ..., '$row[49]');";
mysql_query($query);
}
這里有幾件事:
使用microtime(true)
對腳本進行基准測試。 了解腳本在何處花費最長的時間對您很有用。
例如
$read_file_time = microtime(true); //true makes function return value in seconds
$data = new Spreadsheet_Excel_Reader($excelFile, FALSE);
$col[0] = $data->val($r, 'E');
$col[1] = $data->val($r, 'F');
...
$col[49] = $data->val($r, 'BZ');
//Output this to screen, or log it, or whatever
$read_file_time = microtime(true) - $read_file_time; //end time - start time
return $col;
對您的數據庫插入執行相同的操作。
接下來,我建議使用批處理插入。
生成一個行數組,然后一次插入所有行,而不是一次使用當前使用的方法。
所以你的數組看起來像
$rows = array(
array('value for col-0', 'value for col-1' ... 'col-49'), //row 1
array('value for col-0', 'value for col-1' ... 'col-49'), //row 2, etc.
);
您的查詢將類似於
INSERT INTO `payments` (`id`, `year`, `month`, `user`, `p1`, `p2`,... `p50`) VALUES
(NULL, '$y', '$m', '$c', '$row[0][0]', '$row[0][1]', ..., '$row[0][49]'),
(NULL, '$y', '$m', '$c', '$row[1][0]', '$row[1][1]', ..., '$row[1][49]'),
...
(NULL, '$y', '$m', '$c', '$row[101][0]', '$row[101][1]', ..., '$row[101][49]')
根據文件的大小,您可能還需要分批執行此操作。
最后,您(如@mellamokb指出的那樣)將在每個循環中打開文件。 相反,您應該嘗試類似的方法:
function read_row($ExcelFile, $r){
/*$col[0] = $ExcelFile->val($r, 'E');
$col[1] = $ExcelFile->val($r, 'F');*/
//Instead of the above, use a loop
$col = array();
$num_cols = $ExcelFile->colcount();
for($i = 0; $i < $num_cols; $i++) {
$col[] = $ExcelFile->val($r, $i);
}
//Or you can use column names if you really want
//Pass the `$col_names` as a function parameter tho
$col_names = array('E', 'F', 'BZ');
$num_cols = count($col_names);
for($i = 0; $i < $num_cols; $i++) {
$col[] = $ExcelFile->val($r, $col_names[$i]);
}
return $col;
}
require_once './inc/excel_reader2.php';
$DataFile = new Spreadsheet_Excel_Reader('path to your file', FALSE);
for($i = 4; $i <= $count; $i++) {
$row = read_row($DataFile, $i);
}
因此,要結合我的上述建議,您可以執行以下操作:
//@param $ExcelFile : excel reader2 file object
//@param $start_row : Row to start reading from, 0 based
//@param $num_rows : Number of rows to read
function read_n_rows($ExcelFile, $start_row, $num_rows) {
$rows = array();
for($i = $start_row; $i < $num_rows; $i++) {
$rows[] = read_row($ExcelFile, $i)
}
return $rows;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.