【问题标题】:Optimizing propel data insertion for a very long data file为非常长的数据文件优化推进数据插入
【发布时间】:2012-07-09 16:31:33
【问题描述】:

我正在阅读一个很长的文本文件,其中每一行都由一个 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 行),该过程需要数小时才能完成。我可以做些什么来优化我的推进查询吗?还是改进数据库的设计?

【问题讨论】:

    标签: php database orm propel


    【解决方案1】:

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

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

    例子:

    $pdo = new PDO(...);
    $pdo->beginTransaction();
    
    foreach($array as $ar){
        $pdo->query('INSERT INTO...');         
    }
    
    $pdo->commit();
    

    【讨论】:

    • 我曾经使用 PDO,然后几年前我搬到了 propel orm。但是 +1 以获得帮助。
    • 你把它贴在你用过推进器的地方,完全错过了。 :D
    【解决方案2】:

    看看his answer

    它指向一篇关于 Propel 优化以进行大规模插入的文章。它是法语,但很容易理解。

    【讨论】:

    • 感谢您的链接。文章建议使用Propel::disableInstancePooling();,并表示它有助于减少内存消耗。这非常有用,但是您确定这解决了我的问题吗?因为我还没有遇到内存问题(我已将内存限制设置得足够高)。我关心的是如何优化我的查询:我对源文件中的每一行进行 3 次检查和最多 3 次插入。
    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 2023-04-10
    • 2013-05-22
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多