I have database MS SQL Server 2008 R2 with loading 2000 transactions per second. Tables are:
'lion_Tasks'(uid_obj, order_new, __usn_field_order_new, and more 40 fields)
and
'lion_Tasks_Changes_Parts'(uid_task_cp, uid_user_cp, __usn_entity_cp and more 20 fields )
Web-server get 10.000 objects, in which only one field has changed 'order_new'. Only 3 fields need to be updated in the database. These objects I pass in a table parameter to the stored procedure:
I tried to rewrite this stored procedure several times without any performance changes, it still trips out 10-20 seconds, current version is:
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
The temp-database is located on the SSD disk. Maybe someone tell me what to do for acceleration?
The estimated execution plan is:
https://www.brentozar.com/pastetheplan/?id=Byq2JzTwm
Table script:
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
Scripts for indexes:
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
Related
I've been tasked with creating a generic Trigger(one that will work on all tabled in my database) that will will fire on Insert, Delete, and Update to capture changes done on a table.
I have 2 new tables a Master Table...
CREATE TABLE [dbo].[DATA_HIL_Master](
[MasterId] [int] IDENTITY(1,1) NOT NULL,
[ReferenceTable] [nvarchar](100) NULL,
[ReferenceId] [int] NULL,
[OperationType] [smallint] NULL,
[Last_UserId_Log] [int] NULL,
[Last_WorkstationId_Log] [int] NULL,
[Last_DateTime_Log] [datetime] NULL, PRIMARY KEY CLUSTERED (
[MasterId] 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
And then a Detail table...
CREATE TABLE [dbo].[DATA_HIL_Detail](
[DetailId] [int] IDENTITY(1,1) NOT NULL,
[MasterId] [int] NOT NULL,
[ColumnName] [nvarchar](100) NULL,
[OriginalValue] [nvarchar](max) NULL,
[ModifiedValue] [nvarchar](max) NULL,
PRIMARY KEY CLUSTERED (
[DetailId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
My trigger needs to be able to update these tables with the correct information for that update and than table. For Instance I have another table..
CREATE TABLE [dbo].[CNF_Tax2Package](
[Tax2PackageId] [int] IDENTITY(1,1) NOT NULL,
[TaxPackageId] [int] NOT NULL,
[TaxId] [int] NOT NULL,
[Last_UserId_Log] [int] NULL,
[Last_WorkstationId_Log] [int] NULL,
[Last_DateTime_Log] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[Tax2PackageId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[Tax2PackageId] 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
EveryTime a record is change in this CNF_Tax2Package table I need to up the History tables with information
I do the following UPdate statement
Update CNF_Tax2Package
set TaxPackageid = 1,
Last_UserID_Log = 1098,
Last_WorkstationID_Log = 77,
Last_DateTime_Log = Getdate()
where Tax2PackageID = 2]
I would insert into the DATA_HIL_Master a row with the following information
[MasterId] = (NEWMasterID) [ReferenceTable] = 'CNF_Tax2Package' [ReferenceId] = 2 ---This is the primary Key of the
---table updated. [OperationType] = 'Update' [Last_UserId_Log] = 1098 [Last_WorkstationId_Log] = 77 [Last_DateTime_Log] getdate()
I would then insert into the DATA_HIL_Detail the following rows.
[DetailId] = (NEWID) [MasterId] = (NEWMasterID) (From above) [ColumnName] = 'TaxPackageid' [OriginalValue] = '19' [ModifiedValue] = '1'
[DetailId] = (NEWID) [MasterId] = (NEWMasterID) (From above) [ColumnName] = 'Last_UserID_Log' [OriginalValue] = '1954' [ModifiedValue] = '1098'
[DetailId] = (NEWID) [MasterId] = (NEWMasterID) (From above) [ColumnName] = 'Last_WorkstationId_Log' [OriginalValue] = '55' [ModifiedValue] = '77'
[DetailId] = (NEWID) [MasterId] = (NEWMasterID) (From above) [ColumnName] = 'Last_DateTime_Log' [OriginalValue] = '2018-08-18 [ModifiedValue] = getdate()
Understanding that the trigger must be generic enough to handle all tables in my database with different columns, different primary Keys
I have two tables, Bins and Locations, the first with 3 million records, the second with 30 million. I am trying to match Bin for Location with the following code
Create Table #tbl_locations (LocationID int not null, Lat float not null, Lon float not null)
Create Table #tbl_bins (BinID int not null, MinLat float not null, MaxLat float not null, MinLon float not null, MaxLon float not null)
CREATE NONCLUSTERED INDEX [IX_tbl_locations] ON [#tbl_locations] ([Lat] ASC, [Lon] ASC)
ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_tbl_bins1] ON [#tbl_bins] ([MinLat] ASC, [MaxLat] ASC)
ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_tbl_bins2_ABC] ON [#tbl_bins] ([MinLon] ASC, [MaxLon] ASC)
ON [PRIMARY]
Select L.LocationID, C.BinID
From #tbl_bins C
Inner Join #tbl_locations L
ON (L.Lat Between C.MinLat And C.MaxLat)
AND (L.Lon Between C.MinLon And C.MaxLon)
Unfortunately the performance is extremely bad, I have already tried to index the different fields but that did not help enough. Everything still takes more than 10 minutes to run.
Any idea of how can I make this perform better? Maybe a better matching algorithm? SQL Server 2012 SP3.
BinID and LocationID already have a PRIMARY KEY CLUSTERED created on them.
When I check the Execution Plan I see that the JOIN is performed by a NESTED LOOP.
The output of STATISTICS IO is
Table '#tbl_locations_________________________________________________________________________________________________________000000000283'.
Scan count 2631070, logical reads 25575057, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table '#tbl_bins________________________________________________________________________________________________________000000000284'.
Scan count 17, logical reads 14741, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Thanks a lot!
Edit - if instead of Temp Tables I use real ones, this would be the schema exported
/****** Object: Table [dbo].[tbl_Bins] Script Date: 5/30/2017 3:49:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Bins](
[BinID] [int] NOT NULL,
[MinLat] [float] NOT NULL,
[MaxLat] [float] NOT NULL,
[MinLon] [float] NOT NULL,
[MaxLon] [float] NOT NULL,
CONSTRAINT [PK_tbl_Bins] PRIMARY KEY CLUSTERED
(
[BinID] 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
/****** Object: Table [dbo].[tbl_Locations] Script Date: 5/30/2017 3:49:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Locations](
[ExposureID] [int] NOT NULL,
[Accgrpid] [int] NOT NULL,
[LocID] [int] NOT NULL,
[Lat] [float] NOT NULL,
[Lon] [float] NOT NULL,
CONSTRAINT [PK_tbl_locations] PRIMARY KEY CLUSTERED
(
[ExposureID] ASC,
[Accgrpid] ASC,
[LocID] 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
/****** Object: Index [IX_tbl_bins1] Script Date: 5/30/2017 3:49:05 PM ******/
CREATE NONCLUSTERED INDEX [IX_tbl_bins1] ON [dbo].[tbl_Bins]
(
[MinLat] ASC,
[MaxLat] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IX_tbl_bins2_ABC] Script Date: 5/30/2017 3:49:05 PM ******/
CREATE NONCLUSTERED INDEX [IX_tbl_bins2_ABC] ON [dbo].[tbl_Bins]
(
[MinLon] ASC,
[MaxLon] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object: Index [IX_tbl_locations] Script Date: 5/30/2017 3:49:05 PM ******/
CREATE NONCLUSTERED INDEX [IX_tbl_locations] ON [dbo].[tbl_Locations]
(
[Lat] ASC,
[Lon] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
I have found a way to improve this.
SELECT L.LocationID, C.BinID
FROM #tbl_bins C
INNER JOIN #tbl_locations L
ON (L.Lat Between C.MinLat And C.MaxLat)
AND (L.Lon Between C.MinLon And C.MaxLon)
WHERE FLOOR(C.MinLat * 1000 ) <= FLOOR(L.Lat*1000)
AND FLOOR(C.MinLon * 1000 ) <= FLOOR(L.Lon*1000)
AND CEILING(C.MaxLat * 1000 ) >= CEILING(L.Lat*1000)
AND CEILING(C.MaxLon * 1000 ) >= CEILING(L.Lon*1000)
This helps the query by reducing the amounts of Bins to join to 1.
Create a covering index:
CREATE INDEX IX_tbl_lat_long_locations ON [#tbl_locations] (Lat, Lon, LocationID)
Having such an index means the table doesn't need to be accessed in order to retrieve the LocationID.
Make it a CLUSTERED index if you can.
I am having a table with around 60000 rows. I am using MS SQL Server Express 2012.
When updating, it takes long time to finish.
My table design is as below.
CREATE TABLE [dbo].[Entry_Jangad_Lot](
[Srno] [bigint] IDENTITY(1,1) NOT NULL,
[Kapan] [nvarchar](50) NOT NULL,
[Lotno] [int] NOT NULL,
[Quality] [nvarchar](50) NOT NULL,
[Pcs] [int] NOT NULL,
[Caret] [decimal](18, 2) NOT NULL,
[JangadNo] [int] NOT NULL,
[IssuedTo] [nvarchar](50) NULL,
[IssuedDate] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[ReceivedPcs] [int] NULL,
[ReceivedCaret] [decimal](18, 2) NULL,
[ReceivedDate] [nvarchar](50) NULL,
[ReceivedType] [nvarchar](50) NULL,
[Position] [nvarchar](50) NULL,
[Process] [nvarchar](50) NULL,
[MergedStatus] [nvarchar](50) NULL,
[MergedToLotno] [int] NULL,
CONSTRAINT [PK_Entry_Jangad_Lot_1] PRIMARY KEY CLUSTERED
(
[Srno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I am using following Procedure to update a table record.
CREATE procedure [dbo].[sp_add_update_lot_receive]
(
#mode nvarchar(50),
#Kapan nvarchar(50),
#Lotno int,
#ReceivedPcs int,
#ReceivedCaret decimal(18,2),
#ReceivedDate nvarchar(50),
#ReceivedType nvarchar(50)
)
as
if #mode='ADD'
begin
declare #srno int
select #srno=srno from Entry_Jangad_Lot where Kapan=#Kapan
and Lotno=#Lotno
and Position='CURRENT'
update Entry_Jangad_Lot set
ReceivedPcs=#ReceivedPcs,
ReceivedCaret=#ReceivedCaret,
ReceivedDate=#ReceivedDate,
ReceivedType=#ReceivedType,
Status='RECEIVED'
where srno=#srno
--where Kapan=#Kapan
--and Lotno=#Lotno
--and Position='CURRENT'
if #ReceivedType='POLISH'
begin
update Entry_Jangad_Lot set
Process='POLISH'
where srno=#srno
--where Kapan=#Kapan
--and Lotno=#Lotno
--and Position='CURRENT'
end
else
begin
if #ReceivedType='TABLE'
begin
update Entry_Jangad_Lot set
Process='TABLE'
where srno=#srno
--where Kapan=#Kapan
--and Lotno=#Lotno
--and Position='CURRENT'
end
else
begin
update Entry_Jangad_Lot set
Process='NOTPOLISH'
where srno=#srno
--where Kapan=#Kapan
--and Lotno=#Lotno
--and Position='CURRENT'
end
end
end
GO
Following indexes are also available.
ALTER TABLE [dbo].[Entry_Jangad_Lot] ADD CONSTRAINT [PK_Entry_Jangad_Lot_1] PRIMARY KEY CLUSTERED
(
[Srno] 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
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20150422-160201] ON [dbo].[Entry_Jangad_Lot]
(
[Kapan] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20150422-155655] ON [dbo].[Entry_Jangad_Lot]
(
[Kapan] ASC,
[Lotno] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
EDIT
/****** Object: Statistic [_WA_Sys_00000003_47DBAE45] Script Date: 27/04/2015 12:33:24 ******/
CREATE STATISTICS [_WA_Sys_00000003_47DBAE45] ON [dbo].[Entry_Jangad_Lot]([Lotno]) WITH STATS_STREAM = 0x01000000010000000000000000000000B90F1C72000000000B19000000000000CB18000000000000380374003800000004000A00000000000000000042004300070000002508C40085A400004ECE000000000000ADA80000000000005DCA253D8A4B863B0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C1000000C1000000010000001400000000008040004E4E4700000000000080400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000005F17000000000000671700000000000008060000000000001F0600000000000036060000000000004D0600000000000064060000000000007B060000000000009206000000000000A906000000000000C006000000000000D706000000000000EE0600000000000005070000000000001C0700000000000033070000000000004A07000000000000610700000000000078070000000000008F07000000000000A607000000000000BD07000000000000D407000000000000EB0700000000000002080000000000001908000000000000300800000000000047080000000000005E0800000000000075080000000000008C08000000000000A308000000000000BA08000000000000D108000000000000E808000000000000FF0800000000000016090000000000002D0900000000000044090000000000005B0900000000000072090000000000008909000000000000A009000000000000B709000000000000CE09000000000000E509000000000000FC09000000000000130A0000000000002A0A000000000000410A000000000000580A0000000000006F0A000000000000860A0000000000009D0A000000000000B40A000000000000CB0A000000000000E20A000000000000F90A000000000000100B000000000000270B0000000000003E0B000000000000550B0000000000006C0B000000000000830B0000000000009A0B000000000000B10B000000000000C80B000000000000DF0B000000000000F60B0000000000000D0C000000000000240C0000000000003B0C000000000000520C000000000000690C000000000000800C000000000000970C000000000000AE0C000000000000C50C000000000000DC0C000000000000F30C0000000000000A0D000000000000210D000000000000380D0000000000004F0D000000000000660D0000000000007D0D000000000000940D000000000000AB0D000000000000C20D000000000000D90D000000000000F00D000000000000070E0000000000001E0E000000000000350E0000000000004C0E000000000000630E0000000000007A0E000000000000910E000000000000A80E000000000000BF0E000000000000D60E000000000000ED0E000000000000040F0000000000001B0F000000000000320F000000000000490F000000000000600F000000000000770F0000000000008E0F000000000000A50F000000000000BC0F000000000000D30F000000000000EA0F000000000000011000000000000018100000000000002F1000000000000046100000000000005D1000000000000074100000000000008B10000000000000A210000000000000B910000000000000D010000000000000E710000000000000FE1000000000000015110000000000002C1100000000000043110000000000005A11000000000000711100000000000088110000000000009F11000000000000B611000000000000CD11000000000000E411000000000000FB1100000000000012120000000000002912000000000000401200000000000057120000000000006E1200000000000085120000000000009C12000000000000B312000000000000CA12000000000000E112000000000000F8120000000000000F1300000000000026130000000000003D1300000000000054130000000000006B1300000000000082130000000000009913000000000000B013000000000000C713000000000000DE13000000000000F5130000000000000C1400000000000023140000000000003A14000000000000511400000000000068140000000000007F140000000000009614000000000000AD14000000000000C414000000000000DB14000000000000F2140000000000000915000000000000201500000000000037150000000000004E1500000000000065150000000000007C150000000000009315000000000000AA15000000000000C115000000000000D815000000000000EF1500000000000006160000000000001D1600000000000034160000000000004B16000000000000621600000000000079160000000000009016000000000000A716000000000000BE16000000000000D516000000000000EC1600000000000003170000000000001A1700000000000031170000000000004817000000000000100014005CD2ED43000000000000803F01000000040000100014004036ED43000000000000803F02000000040000100014006255E843000000000000803F03000000040000100014004036ED43000000000000803F0400000004000010001400786EEE43000000000000803F05000000040000100014006255E843000000000000803F0600000004000010001400D1C5EA43000000000000803F0700000004000010001400AFA6EF43000000000000803F0800000004000010001400B529EA43000000000000803F0900000004000010001400C5BFF543000000000000803F0A00000004000010001400AA23F543000000000000803F0B00000004000010001400E7DEF043000000000000803F0C0000000400001000140008FEEB43000000000000803F0D0000000400001000140046B9E743000000000000803F0E0000000400001000140046B9E743000000000000803F0F00000004000010001400D1C5EA43000000000000803F10000000040000100014000E81E643000000000000803F11000000040000100014000E81E643000000000000803F1200000004000010001400ED61EB43000000000000803F1300000004000010001400D748E543000000000000803F14000000040000100014001404E143000000000000803F15000000040000100014003623DC43000000000000803F1600000004000010001400FEEADA43000000000000803F1700000004000010001400DCCBDF43000000000000803F18000000040000100014004C3CE243000000000000803F19000000040000100014004C3CE243000000000000803F1A00000004000010001400A593DE43000000000000803F1B00000004000010001400F867E043000000000000803F1C0000000400001000140073DED743000000000000803F1D00000004000010001400C12FDF43000000000000803F1E00000004000010001400E24EDA43000000000000803F1F00000004000010001400FEEADA43000000000000803F2000000004000010001400F2E4E543000000000000803F21000000040000100014001A87DB43000000000000803F22000000040000100014003BA6D643000000000000803F2300000004000010001400E8D1D443000000000000803F2400000004000010001400046ED543000000000000803F2500000004000010001400CC35D443000000000000803F260000000400001000140094FDD243000000000000803F27000000040000100014007961D243000000000000803F28000000040000100014005DC5D143000000000000803F29000000040000100014009A80CD43000000000000803F2A00000004000010001400B099D343000000000000803F2B0000000400001000140094FDD243000000000000803F2C00000004000010001400D2B8CE43000000000000803F2D00000004000010001400258DD043000000000000803F2E000000040000100014007EE4CC43000000000000803F2F000000040000100014002B10CB43000000000000803F3000000004000010001400258DD043000000000000803F31000000040000100014006E4EC143000000000000803F320000000400001000140015F7C443000000000000803F3300000004000010001400B61CCE43000000000000803F3400000004000010001400DDBEC343000000000000803F3500000004000010001400C7A5BD43000000000000803F3600000004000010001400E341BE43000000000000803F3700000004000010001400A686C243000000000000803F3800000004000010001400FFDDBE43000000000000803F3900000004000010001400906DBC43000000000000803F3A000000040000100014001B7ABF43000000000000803F3B00000004000010001400E341BE43000000000000803F3C00000004000010001400B18CB743000000000000803F3D00000004000010001400E9C4B843000000000000803F3E000000040000100014007FD7B043000000000000803F3F0000000400001000140095F0B643000000000000803F40000000040000100014000AE4B343000000000000803F4100000004000010001400EF47B343000000000000803F4200000004000010001400421CB543000000000000803F4300000004000010001400643BB043000000000000803F4400000004000010001400643BB043000000000000803F4500000004000010001400F4CAAD43000000000000803F460000000400001000140016EAA843000000000000803F47000000040000100014002680B443000000000000803F48000000040000100014003286A943000000000000803F490000000400001000140069BEAA43000000000000803F4A000000040000100014004E22AA43000000000000803F4B000000040000100014004E22AA43000000000000803F4C0000000400001000140000D1A243000000000000803F4D0000000400001000140053A5A443000000000000803F4E000000040000100014000BD79743000000000000803F4F000000040000100014000BD79743000000000000803F5000000004000010001400430F9943000000000000803F510000000400001000140006549D43000000000000803F5200000004000010001400CE1B9C43000000000000803F530000000400001000140080CA9443000000000000803F5400000004000010001400B8029643000000000000803F550000000400001000140080CA9443000000000000803F56000000040000100014009C669543000000000000803F5700000004000010001400BE859043000000000000803F580000000400001000140033798D43000000000000803F59000000040000100014006AB18E43000000000000803F5A00000004000010001400864D8F43000000000000803F5B00000004000010001400A86C8A43000000000000803F5C00000004000010001400E5278643000000000000803F5D0000000400001000140017DD8C43000000000000803F5E00000004000010001400E5278643000000000000803F5F0000000400001000140038FC8743000000000000803F6000000004000010001400DFA48B43000000000000803F610000000400001000140054988843000000000000803F6200000004000010001400ADEF8443000000000000803F63000000040000100014003E7F8243000000000000803F6400000004000010001400CF0E8043000000000000803F650000000400001000140022E38143000000000000803F6600000004000010001400C03C7B43000000000000803F6700000004000010001400AA237543000000000000803F6800000004000010001400037B7143000000000000803F69000000040000100014007DF16843000000000000803F6A00000004000010001400249A6C43000000000000803F6B00000004000010001400249A6C43000000000000803F6C0000000400001000140046B96743000000000000803F6D00000004000010001400ED616B43000000000000803F6E00000004000010001400249A6C43000000000000803F6F00000004000010001400249A6C43000000000000803F7000000004000010001400D7486543000000000000803F710000000400001000140030A06143000000000000803F720000000400001000140051BF5C43000000000000803F730000000400001000140073DE5743000000000000803F7400000004000010001400CC355443000000000000803F75000000040000100014007EE44C43000000000000803F760000000400001000140068CB4643000000000000803F770000000400001000140068CB4643000000000000803F780000000400001000140068CB4643000000000000803F79000000040000100014007EE44C43000000000000803F7A00000004000010001400A0034843000000000000803F7B000000040000100014005EB83543000000000000803F7C0000000400001000140052B24043000000000000803F7D000000040000100014008AEA4143000000000000803F7E0000000400001000140095F03643000000000000803F7F00000004000010001400EF473343000000000000803F8000000004000010001400E3413E43000000000000803F8100000004000010001400CD283843000000000000803F82000000040000100014005EB83543000000000000803F8300000004000010001400489F2F43000000000000803F840000000400001000140005613943000000000000803F8500000004000010001400D92E2D43000000000000803F860000000400001000140010672E43000000000000803F8700000004000010001400B70F3243000000000000803F8800000004000010001400A1F62B43000000000000803F8900000004000010001400EF473343000000000000803F8A000000040000100014005EB83543000000000000803F8B000000040000100014007FD73043000000000000803F8C0000000400001000140010672E43000000000000803F8D00000004000010001400D92E2D43000000000000803F8E00000004000010001400ACFC2043000000000000803F8F0000000400001000140075C41F43000000000000803F9000000004000010001400ACFC2043000000000000803F9100000004000010001400CE1B1C43000000000000803F92000000040000100014008BDD2543000000000000803F9300000004000010001400CE1B1C43000000000000803F9400000004000010001400C3080B43CA0F1A43CA0F1A43960000000400001000140080CA1443000000000000803F9700000004000010001400E52706433EAA0C433EAA0C43990000000400001000140067D8E2429BF201439BF201439B000000040000100014001A87DB42000000000000803F9C00000004000010001400EE54CF42000000000000803F9D0000000400001000140089F7DD42000000000000803F9E00000004000010001400A003C8427B62DE427B62DE42A00000000400001000140052B2C0427B62DE427B62DE42A200000004000010001400D92EAD42A88DBB42A88DBB42A400000004000010001400489FAF42000000000000803FA500000004000010001400CE1B9C42EE83B342EE83B342A7000000040000100014008BDDA542000000000000803FA8000000040000100014005FAB9942000000000000803FA900000004000010001400A2E98F42000000000000803FAA000000040000100014005FAB9942000000000000803FAB0000000400001000140076B78342000000000000803FAC00000004000010001400115A9242000000000000803FAD0000000400001000140033798D42ED0A9642ED0A9642AF0000000400001000140076B78342000000000000803FB000000004000010001400F03A974290C2A04290C2A042B2000000040000100014001A875B427BDB7B427BDB7B42B400000004000010001400D7486542000000000000803FB500000004000010001400F867604207C86B4207C86B42B7000000040000100014003BA65642000000000000803FB80000000400001000140069BE2A42000000000000803FB900000004000010001400056139421DA14B421DA14B42BB00000004000010001400A0034842000000000000803FBC000000040000100014008BDD2542A88D3B42A88D3B42BE0000000400001000140033790D42BE669B42BE661B42C10000000400001000140033790D42621E2642621E2642C3000000040000100014002FADFD41000000000000803FC40000000400001000140076B70342BE661B42BE661B42C600000004000010001400F867E041000000000000803FC7000000040000100014002FADFD41000000000000803FC8000000040000100014000561B941000000000000803FC9000000040000100014000561B941AA7F7642AA7FF641CC000000040000100014007EE4CC41000000000000803FCD000000040000100014008BDDA541ED0A9642910EC841D100000004000010001400C2224341BE661B42BE669B41D400000004000010001400489F2F4107C8EB4107C86B41D7000000040000100014007D6E9940AA7F76421C552441DE000000040000100014003505C240000000000000803FDF0000000400001000140038720040356C6642480A4240F3000000040000100014000000803F000000000000803FF40000000400004ECE000000000000
GO
/****** Object: Statistic [_WA_Sys_0000000A_47DBAE45] Script Date: 27/04/2015 12:33:24 ******/
CREATE STATISTICS [_WA_Sys_0000000A_47DBAE45] ON [dbo].[Entry_Jangad_Lot]([Status]) WITH STATS_STREAM = 0x01000000010000000000000000000000EE2F86BC000000006C020000000000002C02000000000000E7020000E7000000640000000000000008D000346C080000070000008A27C40085A400004ECE00000000000003AA000000000000000000000000003F000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000020000000100000010000000E7C17E41004E4E4700000000E7C17E410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000005C00000000000000B800000000000000C0000000000000001000000000000000350000000000000030001000522C0045000000000000803F04000001002500500045004E00440049004E004700300010003B4B4647000000000000803F0400000100270052004500430045004900560045004400FF01000000000000008A08000002000000280000002800000000000000000000000F000000500045004E00440049004E00470052004500430045004900560045004400030000004000000000900700000055200807000035080000004ECE000000000000
GO
/****** Object: Statistic [_WA_Sys_0000000E_47DBAE45] Script Date: 27/04/2015 12:33:24 ******/
CREATE STATISTICS [_WA_Sys_0000000E_47DBAE45] ON [dbo].[Entry_Jangad_Lot]([ReceivedType]) WITH STATS_STREAM = 0x010000000100000000000000000000009D06313800000000F602000000000000B602000000000000E7020000E7000000640000000000000008D0003400000000070000000208C40085A400004ECE00000000000003AA00000000000000000000ABAA2A3E000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005000000050000000100000010000000798F0841004E4E4700000045798F0841000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000C50000000000000042010000000000004A010000000000002800000000000000470000000000000062000000000000008100000000000000A40000000000000030001000DF227F44000000000000803F04000001001F00320049004E00310030001000238D8B46000000000000803F04000001001B00340050003000100042A10044000000000000803F04000001001F00470048004100540030001000256DD046000000000000803F0400000100230050004F004C00490053004800300010001A919245000000000000803F040000010021005400410042004C004500FF01000000550000008A080000060000002800000028000000000000000000000015000000320049004E00310034005000470048004100540050004F004C004900530048005400410042004C00450006000000400000000090040000002AA002040000E4020000900406000015A0060A0000500400001005100000C2004ECE000000000000
GO
/****** Object: Statistic [_WA_Sys_0000000F_47DBAE45] Script Date: 27/04/2015 12:33:24 ******/
CREATE STATISTICS [_WA_Sys_0000000F_47DBAE45] ON [dbo].[Entry_Jangad_Lot]([Position]) WITH STATS_STREAM = 0x01000000010000000000000000000000064DC76D000000002702000000000000E701000000000000E7020050E7000000640000000000000008D0003400000000070000007927C40085A400004ECE00000000000003AA000000000000000000000000003F0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000100000001000000100000004130FC40004E4E470058B4464130FC400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000002D0000000000000073000000000000007B00000000000000080000000000000030001000B444E846000000000000803F04000001002500430055005200520045004E005400FF01000000BC0300008A080000020000002800000028000000000000000000000007000000430055005200520045004E0054000200000040000000002007000000CE040000004ECE000000000000
GO
/****** Object: Statistic [_WA_Sys_00000010_47DBAE45] Script Date: 27/04/2015 12:33:24 ******/
CREATE STATISTICS [_WA_Sys_00000010_47DBAE45] ON [dbo].[Entry_Jangad_Lot]([Process]) WITH STATS_STREAM = 0x01000000010000000000000000000000DB5E8FA9000000009C020000000000005C02000000000000E7020000E7000000640000000000000008D0003400000000070000001808C40085A400004ECE00000000000003AA00000000000000000000ABAAAA3E000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000030000000100000010000000961F6241004E4E4700000000961F62410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000008500000000000000E800000000000000F00000000000000018000000000000004100000000000000640000000000000030001000A282A046000000000000803F040000010029004E004F00540050004F004C00490053004800300010007C8ED246000000000000803F0400000100230050004F004C0049005300480030001000852BA645000000000000803F040000010021005400410042004C004500FF01000000000000008A08000003000000280000002800000000000000000000000E0000004E004F00540050004F004C004900530048005400410042004C004500040000004000000000A00900000053030000A0060300005B0400001005090000DC004ECE000000000000
GO
EDIT
DECLARE #return_value int
EXEC #return_value = [dbo].[sp_add_update_lot_receive] #mode =
N'ADD', #Kapan = N'299/SL', #Lotno = 164, #ReceivedPcs = 300,
#ReceivedCaret = 2.90, #ReceivedDate = N'27/04/2015',
#ReceivedType = N'POLISH'
SELECT 'Return Value' = #return_value
GO
If I am understanding your logic, doesn't this do the trick much more simply:
CREATE procedure [dbo].[sp_add_update_lot_receive]
(
#mode nvarchar(50),
#Kapan nvarchar(50),
#Lotno int,
#ReceivedPcs int,
#ReceivedCaret decimal(18,2),
#ReceivedDate nvarchar(50),
#ReceivedType nvarchar(50)
)
as
if #mode='ADD'
begin
update Entry_Jangad_Lot set
ReceivedPcs=#ReceivedPcs,
ReceivedCaret=#ReceivedCaret,
ReceivedDate=#ReceivedDate,
ReceivedType=#ReceivedType,
Status='RECEIVED',
Process = coalesce (t.Process, 'NOTPOLISH')
from Entry_Jangad_Lot
outer apply (values
('TABLE'),
('POLISH')
) t(Process)
where Kapan=#Kapan
and Lotno=#Lotno
and Position='CURRENT'
and t.Process = Entry_Jangad_Lot.Process
end
It doesn't look too bad. If I had to guess (and I do because I am not near SQL Server Express) I'd say it's because you've got an INT srno converted to a BIGINT version on the index. That could be resulting in an implicit conversion that stops the index being used.
Also, you have an unneeded index (the one with Kapan and Lotno will serve as just Kapan too).
Also, define "long time" :)
Im writing Scalar-Variable function in sql to return a strName mapped by an integer. The following is my script:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Lookup_BSource_Value]
(
-- Add the parameters for the function here
#AVal nvarchar(100)
)
RETURNS nvarchar(100)
AS
BEGIN
-- Declare the return variable here
DECLARE #Val nvarchar(100)
SELECT #Val = Val
FROM SMBase
WHERE AName = 'a_source'
AND (OTypeCode = 1084)
AND (AVal = (#AVal))
RETURN #Val
END
I should be getting the result of "BA" but am receiving a "NULL". Is my syntax correct for the multiple AND statements?
EDIT*
Before use the function in my SSIS package I am just doing a simple
SELECT dbo.Lookup_BSource_Value(XXXXXXXXX)
This gives me the null.
The following is the logical schema of the table I am querying:
USE [MSCRM_M_RC]
GO
/****** Object: Table [dbo].[SMBase] Script Date: 1/21/2014 6:29:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SMBase](
[OTypeCode] [int] NOT NULL,
[AName] [nvarchar](100) NOT NULL,
[AVal] [int] NOT NULL,
[LaId] [int] NOT NULL,
[OrgId] [uniqueidentifier] NOT NULL,
[Val] [nvarchar](4000) NULL,
[DOrder] [int] NULL,
[VNumber] [timestamp] NULL,
[SMId] [uniqueidentifier] NOT NULL,
CONSTRAINT [cndx_PrimaryKey_SMap] PRIMARY KEY CLUSTERED
(
[SMId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY],
CONSTRAINT [UQ_SMap] UNIQUE NONCLUSTERED
(
[OTCode] ASC,
[AName] ASC,
[AValue] ASC,
[LaId] ASC,
[OrgId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SMBase] ADD CONSTRAINT [DF_SMa_SMId] DEFAULT (newid()) FOR [SMId]
GO
*EDIT**
The following is my logical schema of my StringMapTable:
StringMap(SMID pk, OTypeCode,AName, AVal, LaID, OrgID fk, Val, DOrder, VNumber)
The following is my logical schema of my TPTRepair Table:
TPTRepair(TPTRepairID pk, Name, Source, TT, LTHrs, CustID fk, PID fk)
The problem is the following:
The "Source" field is an integer in TPTRepair. I'm writing the following scalar-function to reference StringMap to return the string value from Value column in StringMap. I have to have a couple ands because AName hase to = 'a_source' and OTypeCode has to = 1084.
My scalar-function results in a NULL and shouldn't be. Is my syntax correct from the multiple AND statement in my scalar-function above?
So Im apparently an idiot. It was the most simplest thing. OTypeCode was to = '10084' rather than '1084'. Im an intern. Please forgive me ;) This was the answer.
EDIT:
We're in the process of moving server and I've just tested this on the new server. There's no performance problem there. This seems to be down to an underpowered, badly organised server.
One of our processes suddenly ran very slowly last night. The slow step was tracked down to an update statement on a table that was admittedly not too cleverly indexed.
So today I've added indexes to all the tables involved, but I'm still getting terrible performance.
I really don't understand it - possibly I'm still doing something less than smart.
Any suggestions welcomed.
update is as follows:
update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
inner join PHD.migration.PatScope b
on a.patient_pas_no = b.TrustNumber
patscope is 2474147 rows, test_hdm_rtt is 815278
definition of tables:
CREATE TABLE [dbo].[test_HDM_RTT](
[pk_episode_id] [int] NULL,
[pk_event_id] [int] NOT NULL,
[activity_date] [datetime] NULL,
[activity_datetime] [datetime] NULL,
[activity_subtype1] [nvarchar](50) NULL,
[activity_subtype1_code] [nvarchar](50) NULL,
[activity_subtype2] [nvarchar](50) NULL,
[activity_subtype2_code] [nvarchar](50) NULL,
[activity_type] [nvarchar](50) NULL,
[activity_type_code] [nvarchar](50) NULL,
[clock_start_date] [datetime] NULL,
[clock_stop_date] [datetime] NULL,
[dir_code] [nvarchar](10) NULL,
[div_code] [nvarchar](10) NULL,
[episode_id_ext] [nvarchar](50) NULL,
[episode_id_appt] [nvarchar](50) NULL,
[episode_id_ref] [nvarchar](50) NULL,
[episode_id_ref_medway] [nvarchar](50) NULL,
[episode_id_wl] [nvarchar](50) NULL,
[erod] [datetime] NULL,
[nhs_number] [nvarchar](20) NULL,
[patient_id] [int] NULL,
[patient_district_no] [nvarchar](20) NULL,
[patient_pas_no] [nvarchar](50) NULL,
[pathway_id] [nvarchar](50) NULL,
[pct_code] [nvarchar](10) NULL,
[ref_source_code] [nvarchar](10) NULL,
[rtt_episode_id] [nvarchar](50) NULL,
[rtt_outcome_code] [nvarchar](50) NULL,
[rtt_outcome_desc] [nvarchar](50) NULL,
[rtt_start_date] [datetime] NULL,
[rtt_start_ind] [nvarchar](10) NULL,
[rtt_stop_date] [datetime] NULL,
[site_code] [nvarchar](10) NULL,
[spec_natcode] [nvarchar](10) NULL,
[spec_pascode] [nvarchar](10) NULL,
[transfer_text] [nvarchar](100) NULL,
[op_rtt_count] [int] NULL,
[app_rec_date] [datetime] NULL,
[cons_code] [varchar](10) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
USE [PIP]
/****** Object: Index [pk_event_id_clustered] Script Date: 03/06/2013 14:46:52 ******/
CREATE CLUSTERED INDEX [pk_event_id_clustered] ON [dbo].[test_HDM_RTT]
(
[pk_event_id] 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 [PIP]
/****** Object: Index [idx_episode_id_appt] Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_appt] ON [dbo].[test_HDM_RTT]
(
[episode_id_appt] 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 [PIP]
/****** Object: Index [idx_episode_id_ref] Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_ref] ON [dbo].[test_HDM_RTT]
(
[episode_id_ref] 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 [PIP]
/****** Object: Index [idx_episode_id_wl] Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [idx_episode_id_wl] ON [dbo].[test_HDM_RTT]
(
[episode_id_wl] 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 [PIP]
/****** Object: Index [patient_pas_no] Script Date: 03/06/2013 14:46:52 ******/
CREATE NONCLUSTERED INDEX [patient_pas_no] ON [dbo].[test_HDM_RTT]
(
[patient_pas_no] 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
and
USE [PHD]
GO
/****** Object: Table [migration].[PatScope] Script Date: 03/06/2013 14:47:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [migration].[PatScope](
[RID] [varchar](7) NOT NULL,
[Number] [varchar](17) NOT NULL,
[TrustNumber] [varchar](10) NULL,
[NumberType] [nvarchar](10) NULL,
[legacy_number] [varchar](10) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
USE [PHD]
/****** Object: Index [TrustNoClustered] Script Date: 03/06/2013 14:47:57 ******/
CREATE CLUSTERED INDEX [TrustNoClustered] ON [migration].[PatScope]
(
[TrustNumber] 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 [PHD]
/****** Object: Index [TrustNo] Script Date: 03/06/2013 14:47:57 ******/
CREATE NONCLUSTERED INDEX [TrustNo] ON [migration].[PatScope]
(
[TrustNumber] ASC,
[Number] 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 [PHD]
/****** Object: Index [TrustNumber_legacy_lookup] Script Date: 03/06/2013 14:47:57 ******/
CREATE UNIQUE NONCLUSTERED INDEX [TrustNumber_legacy_lookup] ON [migration].[PatScope]
(
[TrustNumber] ASC,
[legacy_number] 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
personally, I would only update if the value is not the same as existing. That should speed up the query
update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
inner join PHD.migration.PatScope b
on a.patient_pas_no = b.TrustNumber
where a.patient_district_no <> b.legacy_number
I would also check out the EXPLAIN results (ctrl + l) your query may be using the wrong index.
Show Execution Plan & see if the plan uses indexes. If it doesn't, you can force it to use a particular index:
SET ANSI_NULLS OFF
GO
update test_HDM_RTT
set patient_district_no = b.legacy_number
from test_HDM_RTT a
inner join PHD.migration.PatScope b WITH (INDEX(TrustNumber_legacy_lookup))
on a.patient_pas_no = b.TrustNumber
where a.patient_district_no <> b.legacy_number
You'll want to see if you need to force an index on test_HDM_RTT instead because it looks like it's already going to do an index scan of TrustNumber_legacy_lookup to get its data.