更新数千行中的几个字段

时间:2018-09-04 14:40:20

标签: sql-server tsql sql-server-2008-r2 sql-update

我有数据库MS SQL Server 2008 R2,每秒可加载2000个事务。表格是:

'lion_Tasks'(uid_obj, order_new, __usn_field_order_new, and more 40 fields) 

'lion_Tasks_Changes_Parts'(uid_task_cp, uid_user_cp, __usn_entity_cp and more 20 fields )

Web服务器获得10.000个对象,其中只有一个字段已更改“ order_new”。数据库中仅需要更新3个字段。我将这些表参数传递给存储过程:

我尝试多次重写此存储过程,而没有任何性能更改,但它仍会跳出 10-20秒,当前版本为:

CREATE PROCEDURE dbo.lion_UpdateTasksNewOrder
( @Table TasksNewOrderTableType READONLY) 
WITH RECOMPILE AS
BEGIN

DECLARE @ErrorCode     int
SET @ErrorCode = -1

UPDATE TasksTable
SET     
  order_new = t.ORDER_NEW,   
  __usn_field_order_new = t.USN_ORDER_NEW
FROM dbo.lion_Tasks TasksTable
INNER JOIN @Table t
ON t.UUID_TASK = TasksTable.uid_obj

  IF( @@ERROR != 0 )
  BEGIN
      SET @ErrorCode = -1
      GOTO Cleanup
  END                       

UPDATE TasksTableCP
SET 
  __usn_entity_cp = tcp.USN_ENTITY
FROM dbo.lion_Tasks_Changes_Parts TasksTableCP
INNER JOIN @Table tcp
ON tcp.UUID_TASK = TasksTableCP.uid_task_cp AND tcp.UUID_USER = TasksTableCP.uid_user_cp

  IF( @@ERROR != 0 )
  BEGIN
      SET @ErrorCode = -1
      GOTO Cleanup
  END

  RETURN 0
  Cleanup:
  RETURN @ErrorCode    
END

临时数据库位于SSD磁盘上。也许有人告诉我该怎么做才能加速?

估计的执行计划是: https://www.brentozar.com/pastetheplan/?id=Byq2JzTwm

表脚本:

USE [lion_data]
GO

/****** Object:  Table [dbo].[lion_Tasks_Changes_Parts]    Script Date: 09/05/2018 11:31:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[lion_Tasks_Changes_Parts](
    [uid_obj_cp] [uniqueidentifier] NOT NULL,
    [uid_task_cp] [uniqueidentifier] NOT NULL,
    [uid_user_cp] [uniqueidentifier] NOT NULL,
    [_order_cp] [int] NOT NULL,
    [uid_marker_cp] [uniqueidentifier] NOT NULL,
    [date_begin_cp] [datetime] NULL,
    [date_end_cp] [datetime] NULL,
    [readed_cp] [int] NOT NULL,
    [collapsed_cp] [int] NOT NULL,
    [__usn_entity_cp] [int] NOT NULL,
    [__usn_field_order_cp] [int] NOT NULL,
    [__usn_field_uid_marker_cp] [int] NOT NULL,
    [__usn_field_date_begin_cp] [int] NOT NULL,
    [__usn_field_date_end_cp] [int] NOT NULL,
    [__usn_field_readed_cp] [int] NOT NULL,
    [__usn_field_collapsed_cp] [int] NOT NULL,
    [__usn_field_list_tags_cp] [int] NULL,
    [Contacts] [nvarchar](max) NULL,
    [__usn_field_contacts_cp] [int] NULL,
    [uid_user_marker] [uniqueidentifier] NULL,
    [__usn_field_uid_user_marker] [int] NULL,
    [focus_cp] [int] NULL,
    [__usn_field_focus_cp] [int] NULL,
 CONSTRAINT [PK_lion_Tasks_Changes_Parts] PRIMARY KEY CLUSTERED 
(
    [uid_obj_cp] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

索引脚本:

USE [lion_data]
GO

/****** Object:  Index [PK_lion_Tasks]    Script Date: 09/05/2018 11:28:44 ******/
ALTER TABLE [dbo].[lion_Tasks] ADD  CONSTRAINT [PK_lion_Tasks] PRIMARY KEY CLUSTERED 
(
    [uid_obj] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [lion_data]
GO

/****** Object:  Index [IX_lion_Tasks_CP_Uid_Task]    Script Date: 09/05/2018 11:29:17 ******/
CREATE NONCLUSTERED INDEX [IX_lion_Tasks_CP_Uid_Task] ON [dbo].[lion_Tasks_Changes_Parts] 
(
    [uid_task_cp] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


USE [lion_data]
GO

/****** Object:  Index [IX_lion_Tasks_CP_Uid_User]    Script Date: 09/05/2018 11:29:30 ******/
CREATE NONCLUSTERED INDEX [IX_lion_Tasks_CP_Uid_User] ON [dbo].[lion_Tasks_Changes_Parts] 
(
    [uid_user_cp] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

0 个答案:

没有答案