1

我有一个正在尝试修改的旧存储过程。

这是正在修改的表中的数据的转储。

每次 PayrollRateID(第 2 列,此处显示的示例值 25、27、28 等)递增时,“Incrementer”列需要递增。

ID 列是使用 Row_Number() 生成的

编写存储过程的人正在使用一个列向下移动表中的每一行,并在每次 PayrollRateID 值发生变化时设置增量列的值。不幸的是,这需要几分钟才能完成 20,000 奇数行。这只是几天的数据,如果我以一个月的价值运行,那么你可以想象我遇到的性能问题。

sproc 的逻辑是从增量值 0 开始,并使用第一个 PayrollRateID(在示例中 - 25),给所有这些 0,当 PayrollRateID 更改时,然后增加增量列中的计数,并给出所有下一个值(在示例中 - 27's)为 1,当它更改为下一个值(在我的示例中为 28)然后给增量列 2 等。

Date
                        PayrollRateID
                            DayNum  
                                Variance
                                        VarianceID
                                            Incrementer
                                                ID
2011-07-25 00:00:00.000 25  1   1.00    0   0   1
2011-07-26 00:00:00.000 25  2   1.00    0   0   2
2011-07-27 00:00:00.000 25  3   1.00    0   0   3
2011-07-28 00:00:00.000 25  4   1.00    0   0   4
2011-07-29 00:00:00.000 25  5   1.00    0   0   5
2011-07-30 00:00:00.000 25  6   1.00    0   0   6
2011-07-31 00:00:00.000 25  7   1.00    0   0   7
2011-08-01 00:00:00.000 25  1   1.00    0   0   8
2011-08-02 00:00:00.000 25  2   1.00    0   0   9
2011-08-03 00:00:00.000 25  3   1.00    0   0   10
2011-08-04 00:00:00.000 25  4   1.00    0   0   11
2011-08-05 00:00:00.000 25  5   1.00    0   0   12
2011-08-06 00:00:00.000 25  6   1.00    0   0   13
2011-08-07 00:00:00.000 25  7   1.00    0   0   14
2011-08-08 00:00:00.000 25  1   1.00    0   0   15
2011-08-09 00:00:00.000 25  2   1.00    0   0   16
2011-08-10 00:00:00.000 25  3   1.00    0   0   17
2011-08-11 00:00:00.000 25  4   1.00    0   0   18
2011-08-12 00:00:00.000 25  5   1.00    0   0   19
2011-08-13 00:00:00.000 25  6   1.00    0   0   20
2011-08-14 00:00:00.000 25  7   1.00    0   0   21
2011-07-25 00:00:00.000 27  1   1.00    0   1   22
2011-07-26 00:00:00.000 27  2   1.00    0   1   23
2011-07-27 00:00:00.000 27  3   1.00    0   1   24
2011-07-28 00:00:00.000 27  4   1.00    0   1   25
2011-07-29 00:00:00.000 27  5   1.00    0   1   26
2011-07-30 00:00:00.000 27  6   1.00    0   1   27
2011-07-31 00:00:00.000 27  7   1.00    0   1   28
2011-08-01 00:00:00.000 27  1   1.00    0   1   29
2011-08-02 00:00:00.000 27  2   1.00    0   1   30
2011-08-03 00:00:00.000 27  3   1.00    0   1   31
2011-08-04 00:00:00.000 27  4   1.00    0   1   32
2011-08-05 00:00:00.000 27  5   1.00    0   1   33
2011-08-06 00:00:00.000 27  6   1.00    0   1   34
2011-08-07 00:00:00.000 27  7   1.00    0   1   35
2011-08-08 00:00:00.000 27  1   1.00    0   1   36
2011-08-09 00:00:00.000 27  2   1.00    0   1   37
2011-08-10 00:00:00.000 27  3   1.00    0   1   38
2011-08-11 00:00:00.000 27  4   1.00    0   1   39
2011-08-12 00:00:00.000 27  5   1.00    0   1   40
2011-08-13 00:00:00.000 27  6   1.00    0   1   41
2011-08-14 00:00:00.000 27  7   1.00    0   1   42
2011-07-25 00:00:00.000 28  1   1.00    0   2   43
2011-07-26 00:00:00.000 28  2   1.00    0   2   44
2011-07-27 00:00:00.000 28  3   1.00    0   2   45
2011-07-28 00:00:00.000 28  4   1.00    0   2   46
2011-07-29 00:00:00.000 28  5   1.00    0   2   47
2011-07-30 00:00:00.000 28  6   1.00    0   2   48
2011-07-31 00:00:00.000 28  7   1.00    0   2   49
2011-08-01 00:00:00.000 28  1   1.00    0   2   50
2011-08-02 00:00:00.000 28  2   1.00    0   2   51
2011-08-03 00:00:00.000 28  3   1.00    0   2   52
2011-08-04 00:00:00.000 28  4   1.00    0   2   53
2011-08-05 00:00:00.000 28  5   1.00    0   2   54
2011-08-06 00:00:00.000 28  6   1.00    0   2   55
2011-08-07 00:00:00.000 28  7   1.00    0   2   56
2011-08-08 00:00:00.000 28  1   1.00    0   2   57
2011-08-09 00:00:00.000 28  2   1.00    0   2   58
2011-08-10 00:00:00.000 28  3   1.00    0   2   59
2011-08-11 00:00:00.000 28  4   1.00    0   2   60
2011-08-12 00:00:00.000 28  5   1.00    0   2   61
2011-08-13 00:00:00.000 28  6   1.00    0   2   62
2011-08-14 00:00:00.000 28  7   1.00    0   2   63
2011-07-25 00:00:00.000 34  1   1.00    0   3   64
2011-07-26 00:00:00.000 34  2   1.00    0   3   65
2011-07-27 00:00:00.000 34  3   1.00    0   3   66
2011-07-28 00:00:00.000 34  4   1.00    0   3   67
2011-07-29 00:00:00.000 34  5   1.00    0   3   68
2011-07-30 00:00:00.000 34  6   1.00    0   3   69
2011-07-31 00:00:00.000 34  7   1.00    0   3   70
2011-08-01 00:00:00.000 34  1   1.00    0   3   71
2011-08-02 00:00:00.000 34  2   1.00    0   3   72
2011-08-03 00:00:00.000 34  3   1.00    0   3   73
2011-08-04 00:00:00.000 34  4   1.00    0   3   74
2011-08-05 00:00:00.000 34  5   1.00    0   3   75
2011-08-06 00:00:00.000 34  6   1.00    0   3   76
2011-08-07 00:00:00.000 34  7   1.00    0   3   77
2011-08-08 00:00:00.000 34  1   1.00    0   3   78
2011-08-09 00:00:00.000 34  2   1.00    0   3   79
2011-08-10 00:00:00.000 34  3   1.00    0   3   80
2011-08-11 00:00:00.000 34  4   1.00    0   3   81
2011-08-12 00:00:00.000 34  5   1.00    0   3   82
2011-08-13 00:00:00.000 34  6   1.00    0   3   83
2011-08-14 00:00:00.000 34  7   1.00    0   3   84
2011-07-25 00:00:00.000 38  1   1.00    0   4   85
2011-07-26 00:00:00.000 38  2   1.00    0   4   86
2011-07-27 00:00:00.000 38  3   1.00    0   4   87
2011-07-28 00:00:00.000 38  4   1.00    0   4   88
2011-07-29 00:00:00.000 38  5   1.00    0   4   89
2011-07-30 00:00:00.000 38  6   1.00    0   4   90
2011-07-31 00:00:00.000 38  7   1.00    0   4   91
2011-08-01 00:00:00.000 38  1   1.00    0   4   92
2011-08-02 00:00:00.000 38  2   1.00    0   4   93
2011-08-03 00:00:00.000 38  3   1.00    0   4   94
2011-08-04 00:00:00.000 38  4   1.00    0   4   95
2011-08-05 00:00:00.000 38  5   1.00    0   4   96
2011-08-06 00:00:00.000 38  6   1.00    0   4   97
2011-08-07 00:00:00.000 38  7   1.00    0   4   98
2011-08-08 00:00:00.000 38  1   1.00    0   4   99
2011-08-09 00:00:00.000 38  2   1.00    0   4   100
2011-08-10 00:00:00.000 38  3   1.00    0   4   101
2011-08-11 00:00:00.000 38  4   1.00    0   4   102
2011-08-12 00:00:00.000 38  5   1.00    0   4   103
2011-08-13 00:00:00.000 38  6   1.00    0   4   104
2011-08-14 00:00:00.000 38  7   1.00    0   4   105
2011-07-25 00:00:00.000 40  1   1.00    0   5   106
2011-07-26 00:00:00.000 40  2   1.00    0   5   107
2011-07-27 00:00:00.000 40  3   1.00    0   5   108
2011-07-28 00:00:00.000 40  4   1.00    0   5   109
2011-07-29 00:00:00.000 40  5   1.00    0   5   110
2011-07-30 00:00:00.000 40  6   1.00    0   5   111
2011-07-31 00:00:00.000 40  7   1.00    0   5   112
2011-08-01 00:00:00.000 40  1   1.00    0   5   113
2011-08-02 00:00:00.000 40  2   1.00    0   5   114
2011-08-03 00:00:00.000 40  3   1.00    0   5   115
2011-08-04 00:00:00.000 40  4   1.00    0   5   116
2011-08-05 00:00:00.000 40  5   1.00    0   5   117
2011-08-06 00:00:00.000 40  6   1.00    0   5   118
2011-08-07 00:00:00.000 40  7   1.00    0   5   119
2011-08-08 00:00:00.000 40  1   1.00    0   5   120
2011-08-09 00:00:00.000 40  2   1.00    0   5   121
2011-08-10 00:00:00.000 40  3   1.00    0   5   122
2011-08-11 00:00:00.000 40  4   1.00    0   5   123
2011-08-12 00:00:00.000 40  5   1.00    0   5   124
2011-08-13 00:00:00.000 40  6   1.00    0   5   125
2011-08-14 00:00:00.000 40  7   1.00    0   5   126
2011-07-25 00:00:00.000 41  1   1.00    0   6   127
2011-07-26 00:00:00.000 41  2   1.00    0   6   128
2011-07-27 00:00:00.000 41  3   1.00    0   6   129
2011-07-28 00:00:00.000 41  4   1.00    0   6   130
2011-07-29 00:00:00.000 41  5   1.00    0   6   131
2011-07-30 00:00:00.000 41  6   1.00    0   6   132
2011-07-31 00:00:00.000 41  7   1.00    0   6   133
2011-08-01 00:00:00.000 41  1   1.00    0   6   134
2011-08-02 00:00:00.000 41  2   1.00    0   6   135
2011-08-03 00:00:00.000 41  3   1.00    0   6   136
2011-08-04 00:00:00.000 41  4   1.00    0   6   137
2011-08-05 00:00:00.000 41  5   1.00    0   6   138
2011-08-06 00:00:00.000 41  6   1.00    0   6   139
2011-08-07 00:00:00.000 41  7   1.00    0   6   140
2011-08-08 00:00:00.000 41  1   1.00    0   6   141
2011-08-09 00:00:00.000 41  2   1.00    0   6   142
2011-08-10 00:00:00.000 41  3   1.00    0   6   143
2011-08-11 00:00:00.000 41  4   1.00    0   6   144
2011-08-12 00:00:00.000 41  5   1.00    0   6   145
2011-08-13 00:00:00.000 41  6   1.00    0   6   146
2011-08-14 00:00:00.000 41  7   1.00    0   6   147
2011-07-25 00:00:00.000 42  1   1.00    0   7   148
2011-07-26 00:00:00.000 42  2   1.00    0   7   149
2011-07-27 00:00:00.000 42  3   1.00    0   7   150
2011-07-28 00:00:00.000 42  4   1.00    0   7   151
2011-07-29 00:00:00.000 42  5   1.00    0   7   152
2011-07-30 00:00:00.000 42  6   1.00    0   7   153
2011-07-31 00:00:00.000 42  7   1.00    0   7   154
2011-08-01 00:00:00.000 42  1   1.00    0   7   155
2011-08-02 00:00:00.000 42  2   1.00    0   7   156
2011-08-03 00:00:00.000 42  3   1.00    0   7   157
2011-08-04 00:00:00.000 42  4   1.00    0   7   158
2011-08-05 00:00:00.000 42  5   1.00    0   7   159
2011-08-06 00:00:00.000 42  6   1.00    0   7   160
2011-08-07 00:00:00.000 42  7   1.00    0   7   161
2011-08-08 00:00:00.000 42  1   1.00    0   7   162
2011-08-09 00:00:00.000 42  2   1.00    0   7   163
2011-08-10 00:00:00.000 42  3   1.00    0   7   164
2011-08-11 00:00:00.000 42  4   1.00    0   7   165
2011-08-12 00:00:00.000 42  5   1.00    0   7   166
2011-08-13 00:00:00.000 42  6   1.00    0   7   167
2011-08-14 00:00:00.000 42  7   1.00    0   7   168
2011-07-25 00:00:00.000 44  1   1.00    0   8   169
2011-07-26 00:00:00.000 44  2   1.00    0   8   170
2011-07-27 00:00:00.000 44  3   1.00    0   8   171
2011-07-28 00:00:00.000 44  4   1.00    0   8   172
2011-07-29 00:00:00.000 44  5   1.00    0   8   173

我将尝试对 PayrollRateID 进行分组,并使用 Row_Number() 生成 Incrementer 列的值。

任何更好的建议将不胜感激。

SQL 服务器 2008。

4

3 回答 3

2

那这个呢:

Update a
Set Incrementer = b.Incrementer
From dbo.YourTable a
Join
(
Select PayRollId,
       Row_Number() Over(Order By PayRollId) as [Incrementer]
From dbo.YourTable 
Group By PayRollId
)b on a.PayRollId = b.PayRollId
于 2012-06-19T10:46:54.263 回答
1

不确定我是否理解存储过程中的逻辑,但是,您能否创建另一个表,例如 [PayrollIncrementer],它具有 ID 标识种子值,并在您需要增加 ID 时插入表中。

CREATE TABLE [dbo].[PayrollIncrementer]
(
    [Id] INT NOT NULL IDENTITY(1),
    [DateAdded] DATETIME NOT NULL
)

您的存储过程将使用此表,如下所示

BEGIN
    -- PROC BEGIN

    DECLARE @id INT
    INSERT INTO [dbo].[PayrollIncrementer] ([DateAdded]) VALUES (GETDATE())
    SET @id = SCOPE_IDENTITY()

    -- remainder of proc & use the identity

    -- PROC END
END

抱歉,如果您已经想到了这一点,但我不太了解预期的结果。

于 2012-06-19T10:39:42.303 回答
0

只使用DENSE_RANK而不是怎么样ROW_NUMBER?这将立即完成所有工作。

于 2012-06-19T19:30:04.440 回答