我有两个表serialNumber,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)
我想从ORG A转移序列号(123456100)到ORG B并标记 它被转移了 transferedStatus = 0(有货) transferedStatus = 1(缺货)
这是我的查询它的工作,但是当我想要传输大量的serialNumber(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)
答案 0 :(得分:1)
你是逐行进行的,所以难怪它对于10k行来说很慢。尝试修改您的过程,使其适用于表值参数而不是int。 tbv用法示例: http://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/ 基于集合的操作肯定会更快。