【问题标题】:How to improve speed of thousand of inserts in a Nested Set model?如何提高嵌套集模型中千次插入的速度?
【发布时间】:2011-03-18 13:33:15
【问题描述】:

我正在使用 MySQL。我想插入 100.000 个属于具有不同级别(root_id、lft、rgt、级别)的嵌套集模型的位置。如果我不必担心树,我会做一个批量插入;但在这种情况下,我必须按此顺序对每个对象进行 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 语法而不是此选项?

我还有其他选择吗?

非常感谢!

【问题讨论】:

  • 我现在会放弃嵌套集合模型并选择邻接列表 - 生命太短了。

标签: mysql performance bulkinsert


【解决方案1】:

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

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

【讨论】:

  • 这就是我所希望的答案!事实上,我已经有了 parent_id,并且已经有了重建树的任务。非常感谢!
猜你喜欢
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多