duanduoding2238 2012-07-07 07:12
浏览 89
已采纳

如何在嵌套集模型(MySQL)中插入数据;

In the nested set model we have LEFT and Right columns

the first time when the table is empty, what i need to insert to the RIGHT column, if i don't know how many children will i have

LEFT 1 - forever

RIGHT ? - what value goes here??

how to make it dynamic? not static.

ps: using php

  • 写回答

1条回答 默认 最新

  • douliexu5623 2012-07-07 07:19
    关注

    I'm assuming from your tags and title that you are looking for a solution that works with MySQL.

    Yes, you are right that unless you know the number of elements in advance the value for right needs to be calculated dynamically. There are two approaches you can use:

    • You could start with the least value that works (2 in this case) and increase it later as needed.
    • You could just make a guess like 10000000 and hope that's enough, but you need to be prepared for the possibility that it wasn't enough and may need adjusting again later.

    In both cases you need to implement that the left and right values for multiple rows may need to be adjusted when inserting new rows, but in the second case you only actually need to perform the updates if your guesses were wrong. So the second solution is more complex, but can give better performance.

    Note that of the four common ways to store heirarchical data, the nested sets approach is the hardest to perform inserts and updates. See slide 69 of Bill Karwin's Models for Heirarchical Data.

    nested sets update difficulty

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成