-1

我有两个表序列号,Org

create table org(
  org_id  INT NOT NULL IDENTITY(1,1)PRIMARY KEY CLUSTERED,
  orgName VARCHAR(10)
  )
create table serialNumber(id  INT NOT NULL IDENTITY(1,1)PRIMARY KEY CLUSTERED,
                          org_id INT FOREIGN KEY REFERENCES org,
                          serialNumber BIGINT,
                          transferedStatus INT DEFAULT(0),
                          parentID INT NULL FOREIGN KEY REFERENCES serialNumber)

insert into org values('ORG A')
insert into org values('ORG B')
insert into org values('ORG C')
insert into org values('ORG D')
insert into org values('ORG E')

insert into serialNumber values(1,123456100,0,NULL)
insert into serialNumber values(1,123456101,0,NULL)
insert into serialNumber values(1,123456102,0,NULL)
insert into serialNumber values(1,123456103,0,NULL)
insert into serialNumber values(1,123456104,0,NULL)
insert into serialNumber values(1,123456105,0,NULL)

我想将序列号(123456100)从 ORG A 转移到 ORG B 并将其标记为已转移转移状态 = 0(有库存)转移状态 = 1(无库存)

这是我的查询,它可以工作,但是当我要传输大量序列号(10K)时它太慢了……我知道还有其他方法可以做到,但是怎么做?!

declare @parentID int
declare @From_org_id  int 
declare @To_org_id  int 
declare @serialNumber BIGINT

set  @From_org_id  = 1
set  @To_org_id  = 2
set @serialNumber = 123456101

/* mark the serialNumber as transfered */

UPDATE serialNumber                             
set
transferedStatus = 1 
Where   
org_id              = @From_org_id
AND serialNumber    = @serialNumber 

/* find ID of the source of Serial Number */
select TOP 1 @parentID = id
FROM serialNumber
Where org_id            = @From_org_id 
  AND transferedStatus  = 1
    AND serialNumber    = @serialNumber 
ORDER BY id DESC


insert into serialNumber values(@To_org_id,@serialNumber,0,@parentID)

sqlfiddle 链接

4

1 回答 1

1

您是逐行进行的,因此难怪 10k 行会很慢。尝试修改您的过程,使其适用于表值参数而不是 int。tbv 使用示例:http: //blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/ 基于集合的操作肯定会更快。

于 2013-03-25T10:03:48.013 回答