mysql - 如何提高嵌套集模型中数千个插入的速度?

标签 mysql performance bulkinsert

我正在使用 MySQL。我想插入 100.000 个属于不同级别(root_id、lft、rgt、level)的嵌套集模型的位置。如果我不必担心树,我会进行批量插入;但在这种情况下,我必须为此订单上的每个对象执行 3 次查询:

foreach ( $places as $place )
{
    # get parent place from $ancestor_array ( woeid => ids )
    $parent = $ancestors[:parent_woeid]

    update place set lft = lft + 2 where root_id = :root_id and lft >= :obj_lft;
    update place set rgt = rgt + 2 where root_id = :root_id and rgt >= :obj_lft;
    insert into place (..., root_id, lft, rgt, level, ...) values (..., :obj_root_id, :obj_lft, :obj_rgt, :obj_level, ...);

    ...
}

这需要很长时间......所以是时候尝试变得更聪明了。这就是我的想法:

foreach ( $places as $place )
{
    # get parent place from $ancestor_array ( woeid => ids )
    $parent = $ancestors[:parent_woeid]

    $new_admins[] = array('parent_woeid' => :parent_woeid, ...data place....)
    $woeids[] = :parent_woeid;
}

# lock + bulk insert of $new_admins with lft=rgt=level=root_id= null + unlock
    insert into place (...) values (...), (...), (...), ...., (...)

# get ids of those places
select from place where woeid in(:array_woeids)

# lock + bulk update with 3 updates per place + unlock
    update place set lft= lft + 2 where root_id = :root_id and lft >= :obj_lft;
    update place set rgt = rgt + 2 where root_id = :root_id and rgt >= :obj_lft;
    update place set lft=:obj_lft, rgt=:obj_rgt where id=:id

# i have to update the ancestors lft & rgt values on the $ancestors_array, 
# because we have them "cached" in the array in order to avoid doing a
# select per inserted place.

你觉得怎么样?你会怎么做?您是否会将所有插入和更新保存到文件中并使用 LOAD DATA INFILE 语法而不是此选项?

我还缺少其他选项吗?

非常感谢!

最佳答案

仅插入项目并仅在插入所有项目时重新计算所有 lft/rgt 索引不是更高效吗?

但是要做到这一点,您必须知道每个项目的父 ID,以便您有索引的基础。我个人成功地为每个数据项保留了 lft + lrg + ParentId,这使得维护变得更加容易(您可以随时进行简单的重新计算/完整性检查)。

关于mysql - 如何提高嵌套集模型中数千个插入的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352698/

相关文章:

mysql - 将数据从一个表复制到另一个 ERROR 1136

php - 我无法使用 PHP 插入 MySQL(没有显示错误)

mysql - 当我从 >= DATE 切换到 BETWEEN DATE and DATE 时,为什么此查询花费的时间如此之长?

c++ - 代码膨胀何时开始对性能产生显着影响?

linux - 在内核模块中,如何知道给定的 inode 是否属于特定目录?

mysql - 根据复选框过滤 mysql 结果

performance - haskell 从 bytestring 中读取成对的向量非常慢,如何让它更快?

php - 使用 php 随机数生成器在 Mysql 中创建多行

oracle - 使用 PL/SQL 关联数组

java - 使用 hadoop reducer 在将批量写入操作写入 mongodb 时检查重复记录