0

我正在阅读一个很长的文本文件,其中每一行都由一个 ID、groupID 和其他数据组成。每个 ID 可以与许多 groupID 相关联(第 1、2、3 行),并且每个 ID-groupID 组合可以与许多数据相关联(第 2,3 行)。

JWOFJ903JCKDF8O | groupID-22 | some data 
JWOFJ903JCKDF8O | groupID-33 | same ID as above, but different groupID and data
JWOFJ903JCKDF8O | groupID-33 | same ID and groupID as above, but different data 
... 
DF8#CKJ90JJ3WOF | groupID-22 | some data 
...

我正在将此数据移动到数据库中,因此我有一个 ID 表(无 ID 重复)、一个 ID 和 groupID 表(无 ID-groupID 重复)以及一个引用 ID 的数据表-groupID 表。

所以要向数据库中插入1行,我首先检查ID表中不存在这个ID,然后插入它。然后我检查 ID-groupID 表中是否不存在此 ID-groupID 组合,然后将其插入。最后,在这个 ID-groupID id 下插入数据。

does this $id exist in the IDs table
if($id doesn't exist in the IDs table){
  insert a new ID()
  save()
}

does this ID-groupID combo exist in the ID-groupID table
if(doesn't exist){
  create new id-groupid combo
}

does this data exist under the third table in association with this id-groupid combo
if(doesn't exist){
  insert it
}

问题是因为文件非常大(100,000 行),这个过程需要几个小时才能完成。我可以做些什么来优化我的推进查询吗?还是改进数据库的设计?

4

2 回答 2

1

您应该使用PDO。PDO 为您提供了一些性能和安全性改进。此外,PDO 和 MySQLi 模块支持transactions,这很可能是您正在寻找的。

事务,如果你只做 INSERT/UPDATE/SELECTS,将被缓存并一次执行,而不是每次被调用。这非常适合有循环的场景。

例子:

$pdo = new PDO(...);
$pdo->beginTransaction();

foreach($array as $ar){
    $pdo->query('INSERT INTO...');         
}

$pdo->commit();
于 2012-07-09T16:37:05.313 回答
1

看看他的回答

它指向一篇关于 Propel optimizaton for a large insert 的文章。它是法语,但很容易理解。

于 2012-07-09T19:44:06.110 回答