【问题标题】:How can i reduce my text file size?如何减小文本文件的大小?
【发布时间】:2015-07-10 02:07:05
【问题描述】:

我有一个 php 代码,可以从我的数据库中获取产品 ID(23436 条唯一记录)。

我获取每个产品 ID 并通过比较产品 ID 检查它是否已在 feature_product 表中设置。

如果在 features 表中的该 ID 下未找到任何记录,我将通过将文本文件中的 productID 与 feature_product 表中不存在的 productID 进行比较,再次检查产品缺失功能的 trial.txt 文件.

问题是 trial.txt 文件中有 593262 行,并且在这个文件中匹配 productID 需要很长时间。我的内存用完了。我花了 15 个小时才真正从文件中取出所有数据,而且部分数据也是手动的。有没有办法让它更快或不会耗尽时间和内存?

按照网站上的一些帖子的建议,我尝试增加 php.ini 文件中的最大执行时间。但它不断耗尽内存或最大执行时间。一旦我做对了,我将使用 mysqli,因为不再使用 mysql。我想划分产品 ID,这样我一次只能循环说 5000 个,但我认为这对执行时间没有帮助。

<?php
$conn = mysql_connect("localhost", "dbuser", "pwd");  

 //loop through the 1st line to avoid the headers in csv
 if (!$conn){ 
 die('Could not connect : ' . mysql_error()); 
 echo mysql_error();
 }  
 echo '<p>Connected!';

 mysql_select_db("mydb") or die( "Unable to select database");

//Select all product ids from product table into product array
 $pArray = mysql_query("SELECT `id_product` from `product`",$conn);

 //loop through each product id
 while($row = mysql_fetch_assoc($pArray)) {

 //get product ID to check if it exists in features table
 $productID = $row["id_product"];

//check whether product id exists in feature table where product_id matches both product table and features table
 $fArray = mysql_query("SELECT * from `feature_product` WHERE `id_product`=$productID");

//if product Id does not have entry in feature table than call a function to get check if product id has features in text file
if(mysql_num_rows($fArray) ==0)
 {
 checkFeatures($productID);
 }
 else continue;
}

function checkFeatures($productID){
//trial.txt contains features of the products that are missing in features table but the products are in products table
$fd = fopen('trial.txt', 'r');
$fheader = fgets($fd); 

//creates a new text file to save all features(multiple records per product) separated by ',' for future use
$my_file = 'file.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);

while (($data = fgetcsv($fd,0, "~")) !== FALSE) {
//Since this text file has many products i only get the ones that are missing in the features table by comparing product ID which is the 1st element of data array
     if($data[0]==$productID){
     $d= $data[0].",".$data[1].",".$data[2].$data[3]."\n";
     echo $d."<BR/>";
     fwrite($handle, $d);
     }  
}
fclose($fd);
fclose($handle);

   }
?>  

产品表示例

id_product,shop,manufacutrer,category  
1000010,1,41,1112,1  
1000011,1,7,1721,1  
1000012,1,7,1721,1  

特征表示例

feature_id,id_product,value  
1,1000010,1  
3,1000010,2  
6,1000011,5  
11,1931555,1 

示例trial.txt

IMSKU~AttributeID~Value~Unit~StoredValue~StoredUnit  
1000006~16121~2-25~~~  
1000006~3897~* McAfee Protection Suite~~~  
1000006~3933~* 1yr Subscription~~~  
1000010~1708~Feb 2011~~~  
1000010~1710~Cisco~~0.00~  
1000010~1711~http://www.cisco.com~~~  
1000011~2852~1~~0.00~  
1000011~2855~Light Cyan~~0.00~  
1000012~2840~May 2010~~~  
1000012~2842~HP~~0.00~  

我尝试按照用户的建议将文本文件加载为 sql 中的表

<?php  $con=mysqli_connect("localhost","username","pwd","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed: " . mysqli_connect_error();
}

mysqli_query($con,"CREATE TABLE IF NOT EXISTS `add_features` (`id_product` INT(10) NOT NULL, `id_feature` INT(10) NOT NULL, `value` varchar(255),`unit` varchar(20),`s_value` varchar(20),`s_unit` varchar(20))");

$sql = "LOAD DATA INFILE 'trial.txt'
INTO TABLE `add_features`
FIELDS TERMINATED BY '~'
";
if ($con->query($sql) === TRUE) {
echo "OK!";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
} 
$result = mysqli_query($con,"SELECT * FROM `add_features`");

echo "<table class='add_features'>
<tr class='titles'>
<th>Product_id</th>
<th>feature_id</th>
<th>value</th>
<th>Unit</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id_product'] . "</td>";
echo "<td>" . $row['id_feature'] . "</td>";
echo "<td>" . $row['value'] . "</td>";
echo "<td>" . $row['unit'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>  

但我收到一个错误
错误:将数据输入文件“trial.txt”加载到表中add_features 由“~”终止的字段

【问题讨论】:

  • 将文本文件导入数据库
  • @Dagon 我已经编辑了我的代码以显示将文本文件加载到数据库中。我以前从未这样做过,但我收到了这个错误。
  • 您显示的错误信息可能不完整,还有吗?我已经使用了您的代码,它正确导入并显示了数据。您应该在 LOAD DATA 查询中添加 IGNORE 1 LINES 以跳过带有字段名称的标题行。数据文件在你的mysql数据目录吗?
  • 我确实像你说的那样忽略了第一行。它现在对我有用。
  • 很高兴听到这个消息。记得为您的add_features 表添加索引以便快速访问。

标签: php mysql text-files


【解决方案1】:

如果 trial.txt 文件是静态的,我会将其处理/解析为基于某些逻辑分隔符的单独的较小文件,或者将其导入到新的数据库表中(最好),在那里可以立即进行搜索。这是一次导入,然后就完成了。

如果不是静态的,它多久改变一次?

【讨论】:

  • 文本文件不会经常更改。仅在添加新产品时。这可能每月发生一次或两个月一次。因此,将文本文件作为表格插入数据库中将是一个更好的选择。我需要重做整个代码,因为这肯定会用完时间。
  • 是的,一定要导入数据库。编写代码以导入然后搜索新表将是微不足道的,并且可以为您节省大量的挫败感和数小时的处理时间。
  • 好的,我会试一试,很快就会发布我的结果。
  • 我在尝试将文本文件加载到 sql db 时遇到错误。错误: LOAD DATA INFILE 'trial.txt' INTO TABLE add_features FIELDS TERMINATED BY '~' 警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 ../features.php 第 28 行给出。我不确定,但我需要先创建表还是“加载”为我创建一个?正如我在网上看到的示例,它们没有显示如何创建表。 stackoverflow.com/questions/27975228/…
  • 您需要先创建表以匹配您的文件定义。您使用的是什么数据库(mySQL?)和前端(phpMyAdmin?)?如果您之前没有创建过表格,您将不得不在谷歌上搜索如何使用您拥有的前端创建您想要的表格。您也可以使用 php 创建表,但我建议使用 phpMyAdmin 之类的前端。
猜你喜欢
  • 1970-01-01
  • 2016-10-14
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
相关资源
最近更新 更多