简体   繁体   English

存储在SQL Server中维护排序索引的计算列的最佳方法是什么?

[英]What's the best way to store a computed column that maintains a sorting index in SQL Server?

So let's say I've got a table called [transactions] with an identity column as the primary key, a date field, and some other columns. 所以,假设我有一个名为[transactions]的表,其中包含一个标识列作为主键,一个日期字段和一些其他列。 I want to include a persistent column that is computed or somehow calculated which will effectively store the "sort order." 我想要包含一个计算或以某种方式计算的持久列,它将有效地存储“排序顺序”。 This cannot simply be the ID column, however, because I'm sorting by date, and sometimes you might add a value retroactively. 但是,这不能仅仅是ID列,因为我按日期排序,有时您可能会追溯性地添加值。

So for example, if the table started out looking like this: 例如,如果表开始时看起来像这样:

+-----+------------+---------+--------+--------+
| id  |    date    | account | amount | (sort) |
+-----+------------+---------+--------+--------+
|   1 | 2014-05-22 |      7  | 100.00 |    1   |
|   2 | 2014-05-29 |      7  |  45.25 |    2   |
|   3 | 2014-06-03 |      8  |  99.00 |    3   |
+-----+------------+---------+--------+--------+

Then, if I ran this statement: 然后,如果我运行此声明:

INSERT INTO [transactions] ([date], [account], [amount]) 
VALUES ('2014-05-27', 8, 88.50);

I would want the sort column to be smart enough so that the table would then look like this: 我希望sort列足够智能,以便表格看起来像这样:

+-----+------------+---------+--------+--------+
| id  |    date    | account | amount | (sort) |
+-----+------------+---------+--------+--------+
|   1 | 2014-05-22 |      7  | 100.00 |    1   |
|   2 | 2014-05-29 |      7  |  45.25 |    3   |
|   3 | 2014-06-03 |      8  |  99.00 |    4   |
|   4 | 2014-05-27 |      8  |  88.50 |    2   |
+-----+------------+---------+--------+--------+

Ideally, I'd like this column to persist as an actual column. 理想情况下,我希望此列保持为实际列。 What's the best way to accomplish this? 实现这一目标的最佳方法是什么?

You can just calculate the value when you need it: 您可以在需要时计算该值:

select t.*, row_number() over (order by [date]) as [sort]
from transactions t;

Alternatively, just use [date] for order by . 或者,只需使用[date]作为order by I don't see why another column is necessary. 我不明白为什么需要另一个专栏。

EDIT: 编辑:

If you want to keep the column sort in order, then you are going to need a trigger. 如果sort顺序保持列sort ,那么您将需要一个触发器。 And, the trigger will not be cheap. 并且,触发器不会便宜。 The logic in the trigger would be essentially: 触发器中的逻辑基本上是:

update transactions
    set sort = sort + 1
    where [date] > NEWDATE;

insert into transactions( . . . , [sort])
    select . . . , coalesce(max([sort])) + 1
    from transactions
    where [date] < NEWDATE;

(I'm leaving out all the stuff involved with defining a trigger.) (我省略了定义触发器所涉及的所有内容。)

Although the insert can be made quite efficient with an index, this does affect the performance of the update , which has to affect every row with a larger date. 尽管可以使用索引使insert变得非常高效,但这确实会影响update的性能,这必须影响具有更大日期的每一行。 If you are almost always inserting rows with a newer date, this might be a reasonable trade-off. 如果您几乎总是插入具有较新日期的行,这可能是一个合理的权衡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM