SQL Server 2008:帮助使用游标优化查询

发布于 2024-11-01 14:28:22 字数 57 浏览 1 评论 0原文

我需要重写它,这样它就不会导致我的数据库溢出。我需要重写它,使其不使用游标。

谢谢。

I need to rewrite this so it does not cause an overrun on my db. I need to rewrite it so it does not use a cursor.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦纸 2024-11-08 14:28:22

更新这是一些测试数据:

CREATE TABLE #TableB(SKU INT, BeginYear INT, EndYear INT, OptionA VARCHAR(10), OptionB VARCHAR(10))

INSERT INTO #TableB(SKU, BeginYear, EndYear, OptionA, OptionB)
VALUES  (1, 1920, 1950, 'option1', 'option1'),
        (1, 1980, 2001, 'option1', 'option2'),
        (1, 1940, 1952, 'option1', 'option1'), --overlapping years
        (2, 2001, 2005, 'option1', 'option1')

CREATE TABLE #TableA(SKU INT, OptionA VARCHAR(10), OptionB VARCHAR(10), Years INT)

然后您可以尝试递归 CTE:

;WITH CTE AS
(
    SELECT DISTINCT SKU, BeginYear, EndYear, OptionA, OptionB
    FROM #TableB
    UNION ALL
    SELECT SKU, BeginYear+1, EndYear, OptionA, OptionB
    FROM CTE 
    WHERE BeginYear < EndYear
)  
INSERT INTO #TableA(SKU, OptionA, OptionB, Years)
SELECT DISTINCT SKU, OptionA, OptionB, BeginYear
FROM CTE 
OPTION(MAXRECURSION 0)

SELECT *
FROM #TableA
ORDER BY SKU, OptionA, OptionB, Years

这里没有笛卡尔积。

UPDATED Here's some test data:

CREATE TABLE #TableB(SKU INT, BeginYear INT, EndYear INT, OptionA VARCHAR(10), OptionB VARCHAR(10))

INSERT INTO #TableB(SKU, BeginYear, EndYear, OptionA, OptionB)
VALUES  (1, 1920, 1950, 'option1', 'option1'),
        (1, 1980, 2001, 'option1', 'option2'),
        (1, 1940, 1952, 'option1', 'option1'), --overlapping years
        (2, 2001, 2005, 'option1', 'option1')

CREATE TABLE #TableA(SKU INT, OptionA VARCHAR(10), OptionB VARCHAR(10), Years INT)

Then you can try a recursive CTE:

;WITH CTE AS
(
    SELECT DISTINCT SKU, BeginYear, EndYear, OptionA, OptionB
    FROM #TableB
    UNION ALL
    SELECT SKU, BeginYear+1, EndYear, OptionA, OptionB
    FROM CTE 
    WHERE BeginYear < EndYear
)  
INSERT INTO #TableA(SKU, OptionA, OptionB, Years)
SELECT DISTINCT SKU, OptionA, OptionB, BeginYear
FROM CTE 
OPTION(MAXRECURSION 0)

SELECT *
FROM #TableA
ORDER BY SKU, OptionA, OptionB, Years

Here there's no cartesian product.

木有鱼丸 2024-11-08 14:28:22

您可以轻松地用 while 循环替换游标,如果您要耗尽 tempdb,则执行一些中间事务


declare @counter int

declare @temptable as tatble (RecID int Identity(1,1),
       SKU Varchar(25),
       OptionA Varchar(50),
       OptionB Varchar(50),
       CurrentYear Int,
       EndYear Int)

insert into @temptable
   Select SKU,OptionA, OptionB, BeginYear,EndYear
   From TableB

set @counter = (select max(recid) from @temptable)

begin transaction

while  @counter <> 0
Begin

    (***whatever sql logic***)

    if (@counter % 5000 = 0) and @@error = 0
        commit Transaction

    begin Transaction

    set @counter = @counter -1
End

commit transaction

You can easily substitute a while loop for a cursor and if you are blowing out your tempdb then do some intermediate transactions


declare @counter int

declare @temptable as tatble (RecID int Identity(1,1),
       SKU Varchar(25),
       OptionA Varchar(50),
       OptionB Varchar(50),
       CurrentYear Int,
       EndYear Int)

insert into @temptable
   Select SKU,OptionA, OptionB, BeginYear,EndYear
   From TableB

set @counter = (select max(recid) from @temptable)

begin transaction

while  @counter <> 0
Begin

    (***whatever sql logic***)

    if (@counter % 5000 = 0) and @@error = 0
        commit Transaction

    begin Transaction

    set @counter = @counter -1
End

commit transaction
倾城月光淡如水﹏ 2024-11-08 14:28:22

插入发生的次数可能较少

Declare @temp table (
    SKU Varchar(25),
    OptionA Varchar(50),
    OptionB Varchar(50),
    [Year] Int
)

While @@FETCH_STATUS = 0
Begin
    While @CurrentYear <= @EndYear
    Begin
        Insert Into @temp(SKU, OptionA, OptionB, Years)
            Values(@SKU, @OptionA, @OptionB, @CurrentYear)                          

        Set @CurrentYear = @CurrentYear + 1
    End

    Insert Into TableA
        Select * From @temp

    Delete From @temp

    Fetch Next From @Row Into @SKU, @CurrentYear, @EndYear, @OptionA, @OptionB
End

May be use less number of times the insert happen

Declare @temp table (
    SKU Varchar(25),
    OptionA Varchar(50),
    OptionB Varchar(50),
    [Year] Int
)

While @@FETCH_STATUS = 0
Begin
    While @CurrentYear <= @EndYear
    Begin
        Insert Into @temp(SKU, OptionA, OptionB, Years)
            Values(@SKU, @OptionA, @OptionB, @CurrentYear)                          

        Set @CurrentYear = @CurrentYear + 1
    End

    Insert Into TableA
        Select * From @temp

    Delete From @temp

    Fetch Next From @Row Into @SKU, @CurrentYear, @EndYear, @OptionA, @OptionB
End
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文