Php 将大量(约180mb)XML数据上传到MySQL的最佳方式是什么?
我的XML格式如下 `Php 将大量(约180mb)XML数据上传到MySQL的最佳方式是什么?,php,mysql,xml,codeigniter,Php,Mysql,Xml,Codeigniter,我的XML格式如下 ` 这是工作,但这是非常缓慢,每次到期,也需要太多的时间。因此,请给我推荐另一种解决方案。像这样插入多行: function _getXML_Hotellist($fname) { $resToInsert1 = $resToInsert2 = array(); $rowsCounter = 0; foreach($xmlData['Hotel'] as $row) { // fill your $result1 and $result2 arr
这是工作,但这是非常缓慢,每次到期,也需要太多的时间。因此,请给我推荐另一种解决方案。像这样插入多行:
function _getXML_Hotellist($fname) {
$resToInsert1 = $resToInsert2 = array();
$rowsCounter = 0;
foreach($xmlData['Hotel'] as $row) {
// fill your $result1 and $result2 arrays
$resToInsert1[] = $result1;
$resToInsert2[] = $result2;
$rowsCounter++;
if ($rowsCounter == 1000) { // for example, you can choose another number
// insert to db like a
// INSERT INTO table (col1, col2)
// VALUES (...),
// VALUES (...),
// VALUES (...);
// all data from $resToInsert1 and $resToInsert2
// and refresh vars
$resToInsert1 = $resToInsert2 = array();
$rowsCounter = 0;
}
}
}
UPD。此外,如果您不担心丢失数据,您可以使用将数据转换为文件并上传到某个位置。然后,将url插入文件,而不是将数据插入数据库
上传大文件不是一个好的做法。它可能会导致MySQL数据库中的流量过大在CLI中执行,而不是通过浏览器执行。您不需要codeigniter,只需要PHP和PDO/mysqli以及XMLSimpleXML(或者在本例中可能需要XMLReader,虽然180MB不是很大,但可能它适合这样的大小)。
function _getXML_Hotellist($fname) {
$filename = $fname.'.xml';
$xmlfile='xml/'.$filename;
$xmlRaw = file_get_contents($xmlfile);
$xmlData = $this->simplexml->xml_parse($xmlRaw);
/* database for XMLResponse */
$result0['ResponseType'] = $xmlData['ResponseType'];
$result0['AffiliateCode'] = $xmlData['RequestInfo']['AffiliateCode'];
$result0['AffRequestId'] = $xmlData['RequestInfo']['AffRequestId'];
$result0['AffRequestTime'] = $xmlData['RequestInfo']['AffRequestTime'];
$result0['TotalNumber'] = $xmlData['TotalNumber'];
/* database for Hotel List */
foreach($xmlData['Hotel'] as $row) {
$result1['HotelCode']=$this->check_empty($row['HotelCode']);
$result1['OldHotelId']= $this->check_empty($row['OldHotelId']);
if(is_array($result1['OldHotelId'])) {
$result1['OldHotelId']= $result1['OldHotelId'][0];;
} else {
$result1['OldHotelId']= $result1['OldHotelId'];
}
$result1['DestinationId']= $this->check_empty($row['DestinationId']);
$result1['Destination']= $this->check_empty($row['Destination']);
$result1['Country']= $this->check_empty($row['Country']);
$result1['HotelName']= $this->check_empty($row['HotelName']);
$result1['StarRating']= $this->check_empty($row['StarRating']);
$result1['HotelAddress']= $this->check_empty($row['HotelAddress']);
$result1['HotelPostalCode']= $this->check_empty($row['HotelPostalCode']);
$result1['HotelPhoneNumber']= $this->check_empty($row['HotelPhoneNumber']);
$result1['HotelArea']=$this->check_empty($row['HotelArea']);
$result1['Chain']=$this->check_empty($row['Chain']);
/* database for Hotels Location */
$result2['Latitude']=$this->check_empty($row['Coordinates']['Latitude']);
$result2['Longitude']=$this->check_empty($row['Coordinates']['Longitude']);
$result2['HotelCode']= $this->check_empty($row['HotelCode']);
/* database for Hotels Images */
$result4['HotelCode']= $this->check_empty($row['HotelCode']);
$result3['ImageURL']=$row['HotelImages']['ImageURL'];
if(empty($result3['ImageURL'])) {
$result4['ImageURL']="";
$this->db->insert('cf_hotel_images',$result4);
} else {
if(is_array($result3['ImageURL'])) {
foreach($result3['ImageURL'] as $row) {
$result4['ImageURL']=$row;
$this->db->insert('cf_hotel_images',$result4);
}
} else {
$result4['ImageURL']= $result3['ImageURL'];
$this->db->insert('cf_hotel_images',$result4);
}
}
/* database Queries */
$this->db->insert('cf_hotel_list',$result1);
$this->db->insert('cf_coordinates',$result2);
}
//$this->db->insert('cf_xml_response',$result0);
}
function _getXML_Hotellist($fname) {
$resToInsert1 = $resToInsert2 = array();
$rowsCounter = 0;
foreach($xmlData['Hotel'] as $row) {
// fill your $result1 and $result2 arrays
$resToInsert1[] = $result1;
$resToInsert2[] = $result2;
$rowsCounter++;
if ($rowsCounter == 1000) { // for example, you can choose another number
// insert to db like a
// INSERT INTO table (col1, col2)
// VALUES (...),
// VALUES (...),
// VALUES (...);
// all data from $resToInsert1 and $resToInsert2
// and refresh vars
$resToInsert1 = $resToInsert2 = array();
$rowsCounter = 0;
}
}
}