在数据库中的行之间存储顺序的最佳方法是什么?

时间:2022-11-19 12:32:01

I am cloning and extending the Google Tasks application. I want to be able to store the rows order. I have created a field in the database, named rowNumber. When the user inserts a row in the middle of others, I have to update this field in many other rows to achieve the desired output. There is a better way to do it?

我正在克隆并扩展Google Tasks应用程序。我希望能够存储行顺序。我在数据库中创建了一个名为rowNumber的字段。当用户在其他行的中间插入一行时,我必须在许多其他行中更新此字段以实现所需的输出。还有更好的方法吗?

BTW the application is made for the Google AppEngine (Python).

BTW该应用程序是为Google AppEngine(Python)制作的。

2 个解决方案

#1


Make rowNumber a floating point number. When user inserts between rows x and y, the new row gets rowNumber = (x.rowNumber + y.rowNumer) / 2.

使rowNumber成为浮点数。当用户在行x和y之间插入时,新行获取rowNumber =(x.rowNumber + y.rowNumer)/ 2。

When you want to move a row, just update its rowNumber the same way based on target position.

当您想要移动一行时,只需根据目标位置以相同的方式更新其rowNumber。

New rows get rowNumber eg. MAX + 256.

新行获取rowNumber,例如。 MAX + 256。

Edit Microsoft solved this in SQL Server 2008 using new HierarchyId datatype. You can insert between 2 items almost as many times as you want - until you run out of HierarchyId max size.

编辑Microsoft使用新的HierarchyId数据类型在SQL Server 2008中解决了这个问题。您可以根据需要在两个项目之间插入几次 - 直到用完HierarchyId max size。

#2


One option might be to store rows as a linked list, where each row keeps an additional field for the next and previous row. The up side of this is that it requires touching only two other rows following an insert. the downside is that, (as I understand it) AppEngine doesn't have joins, so querying more than one row at a time will be pretty ugly.

一个选项可能是将行存储为链接列表,其中每行为下一行和上一行保留一个附加字段。这方面的优点是它需要在插入之后仅触摸另外两行。缺点是,(据我所知)AppEngine没有连接,因此一次查询多行将非常难看。

#1


Make rowNumber a floating point number. When user inserts between rows x and y, the new row gets rowNumber = (x.rowNumber + y.rowNumer) / 2.

使rowNumber成为浮点数。当用户在行x和y之间插入时,新行获取rowNumber =(x.rowNumber + y.rowNumer)/ 2。

When you want to move a row, just update its rowNumber the same way based on target position.

当您想要移动一行时,只需根据目标位置以相同的方式更新其rowNumber。

New rows get rowNumber eg. MAX + 256.

新行获取rowNumber,例如。 MAX + 256。

Edit Microsoft solved this in SQL Server 2008 using new HierarchyId datatype. You can insert between 2 items almost as many times as you want - until you run out of HierarchyId max size.

编辑Microsoft使用新的HierarchyId数据类型在SQL Server 2008中解决了这个问题。您可以根据需要在两个项目之间插入几次 - 直到用完HierarchyId max size。

#2


One option might be to store rows as a linked list, where each row keeps an additional field for the next and previous row. The up side of this is that it requires touching only two other rows following an insert. the downside is that, (as I understand it) AppEngine doesn't have joins, so querying more than one row at a time will be pretty ugly.

一个选项可能是将行存储为链接列表,其中每行为下一行和上一行保留一个附加字段。这方面的优点是它需要在插入之后仅触摸另外两行。缺点是,(据我所知)AppEngine没有连接,因此一次查询多行将非常难看。