运行并发/并行更新语句(T-SQL)

时间:2015-07-25 13:59:13

标签: sql-server parallel-processing

我有一个基本上是项目记录的表格,每个月的每一天都有列。因此基本上每一行都是ITEM,Day1,Day2,Day3,....我必须运行更新语句,这些语句基本上每天都会遍历每一行,当前日信息需要前一天的一些信息。

基本上,我们需要每日数量。因为订单在框中(固定大小)出来并且计算的数量是碎片,所以系统必须计算下一个最大数量的框。任何"额外数量"被转移到第二天以减少盒子。

例如,对于前面描述的表中的一个记录(框大小为100)

Box Calculation

我当前的代码基本上是记录,计算当天的要求,增加1并重复。我必须为每条记录执行此操作。这是非常低效的,特别是因为它按顺序运行每个记录。

  • 有没有在SQL Server Standard上并行化这个?我正在考虑像缓冲区这样的东西,我将每一行作为工作提交,系统基本上管理资源并运行查询

  • 如果缓冲区的想法不可行,那么无论如何都要有“chunk'这些行并行运行块?

1 个答案:

答案 0 :(得分:0)

Not sure if this helps, but I played around with your data and was able to calculate the figures without row-by-row handling as such. I transposed the figures with unpivot and calculated the values using running total + lag, so this requires SQL Server 2012 or newer:

declare @BOX int = 100

; with C1 as (
  SELECT
    Day, Quantity
  FROM 
     (SELECT * from Table1 where Type = 'Quantity') T1
  UNPIVOT
     (Quantity FOR Day IN  (Day1, Day2, Day3, Day4)) AS up
),

C2 as (
  select Day, Quantity, 
  sum(ceiling(convert(numeric(5,2), Quantity) / @BOX) * @BOX - Quantity)
      over (order by Day asc) % @BOX as Extra
  from C1
),

C3 as (
  select 
    Day, Quantity, 
    Quantity - isnull(Lag(Extra) over (order by Day asc),0) as Required,
    Extra 
  from C2
)

select
  Day, Quantity, Required, 
  ceiling(convert(numeric(5,2), Required) / @BOX) as Boxes, Extra 
from C3

Example in SQL Fiddle