problem description
there is a requirement that php fetch log data from multiple tables at once and return it (using mysql). The table is divided by time and processed by sub-tables, such as log_20180901 and log_20180902. Now you need to query the data and return them according to the time conditions. For example, to query the log data from September 1, 2018 to September 15, 2018, you have tried two methods. The code is as follows:
/**
*@desc:
*@param: $startTime int (unix)
*@pram: $endTime int
*@return: $ret array
*/
function fetchDataByLoop($startTime, $endTime){
$startTime = intval($startTime);
$endTime = intval($endTime);
$ret = array();
while($startTime <= $endTime){
$table = "log_".date("Ymd", $startTime);
$sql = "SELECT * FROM {$table}";
$data = $db->getAll($sql);//
if($data){
$ret[] = $data;
}
$startTime += 86400;
}
return $ret;
}
/**
*@desc: UNION ALLsql
*@param: $startTime int (unix)
*@pram: $endTime int
*@return: $ret array
*/
function fetchDataByUnionAll($startTime, $endTime){
$startTime = intval($startTime);
$endTime = intval($endTime);
$sqlArr = array();
$ret = array();
while($startTime <= $endTime){
$table = "log_".date("Ymd", $startTime);
$sqlArr[] = "SELECT * FROM {$table}";
$startTime += 86400;
}
$sql = implode("UNION ALL", $sqlArr);
$ret = $db->getAll($sql);//
return $ret;
}
I would like to ask, in the case of a large amount of data, which of the above methods will perform faster? Or is there another way to optimize it?