使用临时表和用户定义表或使用c# linq代码可以获得更好的性能
本文关键字:更好 性能 代码 临时表 用户 定义 linq | 更新日期: 2023-09-27 18:13:43
我有一些存储过程,我使用它太多了,我想知道它更好的是在tempdb数据库中使用临时表或使用用户定义的表,并从c# dataTable中获取它,或者忘记它们并使用linq c#在c#代码中处理它。在下列情况下,哪一种表现更好:1. 一个垃圾记录(一个记录超过1000)2. 1000 ~ 10000003.更大的1000000条记录我的存储过程的一些示例:
ALTER PROCEDURE [dbo].[doc_SyncServer_GetTablesFromTemps]
(
@SyncMode int,
@LastExportDate datetime,
@CurrentDate datetime,
@ThisServerGuid uniqueidentifier,
@PartnerServerGuid uniqueidentifier,
@MasterServerGuid uniqueidentifier
) AS
IF OBJECT_ID('tempdb..##SyncLetterWithoutFiles') IS NOT NULL --Drop Temp Table for Candidate Letter Files
DROP TABLE ##SyncLetters
--Letters Without Files--
SELECT
files.[ID], files.[ReferenceID],
files.[ReferenceGuid],
files.[ReferenceFlag], files.[ReferenceSoftwareGuid],
files.[FileTypeID], files.[FileName], files.[Date], files.[Size], files.[Comments], files.[ModificationDate],
files.[FileNatureID], files.[Guid], files.[ModificationServerGuid], files.[RepositoryGuid], files.[Hash], files.[Age]
INTO ##SyncLetterWithoutFiles
FROM
doc_SyncInquiryList inquiry LEFT JOIN
com_Files files ON inquiry.[Guid] = files.[ReferenceGuid]
WHERE
inquiry.[ReferenceFlag] = 75 AND inquiry.[ServerGuid] = @PartnerServerGuid
DELETE
doc_SyncInquiryList
FROM
doc_SyncInquiryList inquiry INNER JOIN
##SyncLetterWithoutFiles temp ON inquiry.[Guid] = temp.[Guid]
WHERE
inquiry.[ReferenceFlag] = 75
-- Letters --
SELECT
letters.[ID], [CentralDeptNo], [IncomingNo], [IncomingDate], [SequenceNo], [Subject], [Summary], [Keywords], [DeliverFrom], [DeliverTo],
[DeliverDate], [DeliverComments], [SourceFlag], [Type], [LetterNo], [Security], [LetterType], [Indicator], [AttachmentCount],
[BodyFileCount], [WordDocCount], [SenderTitle], [ToReceiverList], [Date], [PartitionFactor], [Priority], [RegisterarUserFullName],
[RegisterationDate], [Archived], letters.[Guid], letters.[ModificationDate], letters.[Age], [ModificationServerGuid], [FirstRootInstanceGuid],
[FirstRootInstanceOwnerGuid], [FirstRootInstanceOwnerSecretarialGuid], [SecurityGuid], [LetterTypeGuid], [PriorityGuid],
[TrackingStatusGuid], [IndicatorGuid], [SecretarialGuid], [RegisterarUserGuid], [SourceGuid]
FROM
doc_vwLettersForSync letters INNER JOIN
##SyncLetters ON ##SyncLetters.[ID] = letters.[ID]
-- LetterInstances --
SELECT
instances.[ID], [InstanceDate], [DeadlineDate], instances.[Comments], [Flag], [ViewDate], [SenderTitle], [ToReceiverList], [BrowseSenderTitle],
[BrowseToReceiverList], [FolderTitle], [FolderFlag], [OwnerTitle], [Priority], [ForwardedToOwner], [AttachmentCount], [HasDrawing],
[ForwardDate], [ChildCount], [ChildToReceiversList], [BrowseChildToReceiversList], [ForwarderFullname], [Hierarchy], [Unlisted],
[Unread], [AlertCount], [NoteCount], [OwnerNote], [OwnerNotePublic], [InWorkflow], [Conditions], [AttachedObjects], [PartitionFactor],
[LetterPartitionFactor], [TerminationDate], instances.[Guid], instances.[ModificationDate], instances.[Age], [ModificationServerGuid], [LetterGuid], [OwnerGuid],
[OwnerDeptGuid], [ParentInstanceGuid], [PriorityGuid], [FolderGuid], [TopChildrenGuid], [OrderIndex],[Status]
FROM
doc_vwLetterInstancesForSync instances INNER JOIN
##SyncLetterInstances ON ##SyncLetterInstances.[ID] = instances.[ID]
-- Drafts --
SELECT
drafts.[ID], [Date], [Subject], [Body], [SenderTitle], [ToReceiverList], [BrowseToReceiverList], [CCReceiverList], [DestinationLetterReceivers],
[Priority], [Security], [AttachmentCount], [BodyFileCount], [ReferenceCount], [DestinationLetterType], [PartitionFactor], drafts.[Guid],
drafts.[ModificationDate], drafts.[Age], [ModificationServerGuid], [ParentDraftGuid], [PriorityGuid], [SecurityGuid], [SenderGuid],
[SenderInstanceGuid], [SenderSecretarialGuid], [RegisterarUserGuid], [DestinationSenderGuid], [DestinationSecretarialGuid]
FROM
doc_vwDraftsForSync drafts INNER JOIN
##SyncDrafts ON ##SyncDrafts.[ID] = drafts.[ID]
-- DraftInstances --
SELECT
instances.[ID], [Flag], [ViewDate], [OwnerTitle], [FolderFlag], [Unlisted], [Unread], [AlertCount], [NoteCount], [OwnerNote],
[OwnerNotePublic], [Relapsed], [RelapseComments], [PartitionFactor], instances.[Guid], instances.[ModificationDate], instances.[Age], [ModificationServerGuid],
[DraftGuid], [OwnerGuid], [FolderGuid], [RelapsedPendingLetterGuid], [TargetPendingLetterGuid], [TargetLetterGuid], instances.[Comments]
FROM
doc_vwDraftInstancesForSync instances INNER JOIN
##SyncDraftInstances ON ##SyncDraftInstances.[ID] = instances.[ID]
-- Messages --
SELECT
messages.[ID], [Date], [Subject], [Body], [SenderTitle], [ToReceiverList], [BrowseToReceiverList], [CCReceiverList], [BCCReceiverList], [AttachmentCount],
[PartitionFactor], messages.[Guid], messages.[ModificationDate], messages.[Age], [ModificationServerGuid], [ParentMessageGuid], [SenderGuid]
FROM
doc_vwMessagesForSync messages INNER JOIN
##SyncMessages ON ##SyncMessages.[ID] = messages.[ID]
-- MessageInstances --
SELECT
instances.[ID], [Flag], [ViewDate], [RequestReadReceipt], [Unlisted], [Unread], [AlertCount], [NoteCount], [OwnerNote], [OwnerNotePublic], [PartitionFactor],
instances.[Guid], instances.[ModificationDate], instances.[Age], [ModificationServerGuid], [MessageGuid], [FolderGuid], [OwnerGuid]
FROM
doc_vwMessageInstancesForSync instances INNER JOIN
##SyncMessageInstances ON ##SyncMessageInstances.[ID] = instances.[ID]
-- PendingLetters --
SELECT
pendings.[ID], [TargetLetterGuid], [TargetLetterDate], [TargetLetterNo], [SourceTitle], [SourceType], [ReferenceNo], [Date], [Subject], [Body], [Keywords], [Processed], [ReferenceFlag], [ViewDate],
[Uid], [Sent], [CentralDeptNo], [DestinationLetterReceivers], [Status], [ModificationSyncSent], [NoteCount], [PartitionFactor], pendings.[Guid],
pendings.[ModificationDate], pendings.[Age], [ModificationServerGuid], [DepartmentGuid], [SourceGuid], [PriorityGuid], [SecurityGuid],
[LetterTypeGuid], [ReferenceGuid], [ViewerUserGuid], [RegisterarUserGuid]
FROM
doc_vwPendingLettersForSync pendings INNER JOIN
##SyncPendingLetters ON ##SyncPendingLetters.[ID] = pendings.[ID]
-- LetterReferences --
SELECT
[ReferenceFlag], [ReferenceSoftwareGuid], [ReferenceNo], [ReferenceDate], [ReferenceObjectType], letters.[Guid] AS [ReferenceGuid],
types.[Guid] AS [ReferenceTypeGuid], objects.[Guid] AS [ReferenceObjectGuid],[ReferenceNoType]
FROM
(SELECT * FROM doc_LetterReferences WHERE [ReferenceFlag] = 1 /*ReferenceFlag.Letter*/) ref INNER JOIN
##SyncLetters letters ON letters.[ID] = ref.[ReferenceID] INNER JOIN
com_Permanents types ON types.[ID] = ref.[ReferenceTypeID] LEFT JOIN
doc_Letters objects ON objects.[ID] = ref.[ReferenceObjectID]
UNION
SELECT
[ReferenceFlag], [ReferenceSoftwareGuid], [ReferenceNo], [ReferenceDate], [ReferenceObjectType], drafts.[Guid] AS [ReferenceGuid],
types.[Guid] AS [ReferenceTypeGuid], objects.[Guid] AS [ReferenceObjectGuid],[ReferenceNoType]
FROM
(SELECT * FROM doc_LetterReferences WHERE [ReferenceFlag] = 3 /*ReferenceFlag.Draft*/) ref INNER JOIN
##SyncDrafts drafts ON drafts.[ID] = ref.[ReferenceID] INNER JOIN
com_Permanents types ON types.[ID] = ref.[ReferenceTypeID] LEFT JOIN
doc_Letters objects ON objects.[ID] = ref.[ReferenceObjectID]
UNION
SELECT
[ReferenceFlag], [ReferenceSoftwareGuid], [ReferenceNo], [ReferenceDate], [ReferenceObjectType], pendings.[Guid] AS [ReferenceGuid],
types.[Guid] AS [ReferenceTypeGuid], objects.[Guid] AS [ReferenceObjectGuid],[ReferenceNoType]
FROM
(SELECT * FROM doc_LetterReferences WHERE [ReferenceFlag] = 14 /*ReferenceFlag.PendingLetter*/) ref INNER JOIN
##SyncPendingLetters pendings ON pendings.[ID] = ref.[ReferenceID] INNER JOIN
com_Permanents types ON types.[ID] = ref.[ReferenceTypeID] LEFT JOIN
doc_Letters objects ON objects.[ID] = ref.[ReferenceObjectID]
-- LetterInstancePersons --
SELECT
[Flag], [Comments], [Sent], [Forwarded], instances.[Guid] AS [LetterInstanceGuid], persons.[Guid] AS [PersonGuid], instancePersons.[OrderIndex]
FROM
doc_LetterInstancePersons instancePersons INNER JOIN
##SyncLetterInstances instances ON instances.[ID] = instancePersons.[LetterInstanceID] INNER JOIN
com_OrganizationRelatedPeople persons ON persons.[ID] = instancePersons.[PersonID]
-- LetterArchiveFolders --
SELECT
letters.[Guid] AS [LetterGuid], archives.[Guid] AS [ArchiveFolderGuid]
FROM
doc_LetterArchiveFolders letterArchives INNER JOIN
##SyncLetters letters ON letters.[ID] = letterArchives.[LetterID] INNER JOIN
doc_ArchiveFolders archives ON archives.[ID] = letterArchives.[ArchiveFolderID]
-- DraftDestinationReceivers --
SELECT
drafts.[Guid] AS [DraftGuid], receivers.[ReceiverTitle], receivers.[SendType], receivers.[OrderIndex], receivers.[ReceiverType], receivers.[Comments],
CASE
WHEN [ReceiverType] = 1 /*DocumentOwnerType.Staff*/ THEN staff.[Guid]
WHEN [ReceiverType] = 4 /*DocumentOwnerType.ExternalPerson*/ THEN externalPersons.[Guid]
WHEN [ReceiverType] = 5 /*DocumentOwnerType.Department*/ THEN depts.[Guid]
END AS [ReceiverGuid]
FROM
doc_DraftDestinationReceivers receivers INNER JOIN
##SyncDrafts drafts ON drafts.[ID] = receivers.[DraftID] LEFT JOIN
com_Staff staff ON staff.[ID] = receivers.[ReceiverID] LEFT JOIN
com_OrganizationRelatedPeople externalPersons ON externalPersons.[ID] = receivers.[ReceiverID] LEFT JOIN
com_Departments depts ON depts.[ID] = receivers.[ReceiverID]
-- PendingLetterDestinationReceivers --
SELECT
pendings.[Guid] AS [PendingLetterGuid], receivers.[ReceiverTitle], receivers.[SendType], receivers.[OrderIndex], receivers.[ReceiverType], receivers.[Comments],
CASE
WHEN [ReceiverType] = 1 /*DocumentOwnerType.Staff*/ THEN staff.[Guid]
WHEN [ReceiverType] = 4 /*DocumentOwnerType.ExternalPerson*/ THEN externalPersons.[Guid]
WHEN [ReceiverType] = 5 /*DocumentOwnerType.Department*/ THEN depts.[Guid]
END AS [ReceiverGuid]
FROM
doc_PendingLetterDestinationReceivers receivers INNER JOIN
##SyncPendingLetters pendings ON pendings.[ID] = receivers.[PendingLetterID] LEFT JOIN
com_Staff staff ON staff.[ID] = receivers.[ReceiverID] LEFT JOIN
com_OrganizationRelatedPeople externalPersons ON externalPersons.[ID] = receivers.[ReceiverID] LEFT JOIN
com_Departments depts ON depts.[ID] = receivers.[ReceiverID]
--AttachedFiles--
SELECT * FROM
(SELECT
files.[ID],
files.[ReferenceID],
messageInstances.[Guid] AS [ReferenceGuid],
files.[ReferenceFlag], files.[ReferenceSoftwareGuid],
files.[FileTypeID], files.[FileName], files.[Date], files.[Size], files.[Comments], files.[ModificationDate],
files.[FileNatureID], files.[Guid], files.[ModificationServerGuid], files.[RepositoryGuid], files.[Hash], files.[Age]
FROM
##SyncAttachedFiles attachedFiles INNER JOIN
com_Files files ON attachedFiles.[Guid] = files.[Guid] INNER JOIN
doc_MessageInstances messageInstances ON files.[ReferenceID] = messageInstances.[ID]
WHERE
files.[ReferenceFlag] = 30
UNION ALL
SELECT
files.[ID], files.[ReferenceID],
files.[ReferenceGuid],
files.[ReferenceFlag], files.[ReferenceSoftwareGuid],
files.[FileTypeID], files.[FileName], files.[Date], files.[Size], files.[Comments], files.[ModificationDate],
files.[FileNatureID], files.[Guid], files.[ModificationServerGuid], files.[RepositoryGuid], files.[Hash], files.[Age]
FROM
##SyncAttachedFiles attachedFiles INNER JOIN
com_Files files ON attachedFiles.[Guid] = files.[Guid]
WHERE
files.[ReferenceFlag] != 30
UNION ALL
SELECT
[ID], [ReferenceID],
[ReferenceGuid],
[ReferenceFlag], [ReferenceSoftwareGuid],
[FileTypeID],[FileName],[Date], [Size], [Comments], [ModificationDate],
[FileNatureID], [Guid], [ModificationServerGuid],[RepositoryGuid], [Hash],[Age]
FROM
##SyncLetterWithoutFiles) SyncAttachedFiles
ORDER BY [ID]
ALTER PROCEDURE [dbo].[doc_Letters_SyncBulkInsert]
@ObjectTable doc_Sync_Letters READONLY
AS
INSERT INTO doc_Letters
([CentralDeptNo], [IncomingNo], [IncomingDate], [SecurityID], [LetterTypeID], [IndicatorID], [SequenceNo], [Subject], [Summary], [Keywords], [DeliverFrom], [DeliverTo],
[DeliverDate], [DeliverComments], [SourceFlag], [SecretarialID], [Type], [LetterNo], [Security], [LetterType], [Indicator], [AttachmentCount], [BodyFileCount], [WordDocCount],
[SenderTitle], [ToReceiverList], [Date], [PartitionFactor], [PriorityID], [Priority], [RegisterarUserID], [RegisterarUserFullName], [RegisterationDate], [Archived], [TrackingStatusID],
[ModificationServerGuid], [Guid], [ModificationDate], [Age],[FirstRootInstanceOwnerID], [FirstRootInstanceOwnerSecretarialID])
SELECT
tempLetters.[CentralDeptNo],
tempLetters.[IncomingNo],
tempLetters.[IncomingDate],
0 AS [SecurityID],
0 AS [LetterTypeID],
indicators.[ID] AS [IndicatorID],
tempLetters.[SequenceNo],
tempLetters.[Subject],
tempLetters.[Summary],
tempLetters.[Keywords],
tempLetters.[DeliverFrom],
tempLetters.[DeliverTo],
tempLetters.[DeliverDate],
tempLetters.[DeliverComments],
tempLetters.[SourceFlag],
0 AS [SecretarialID],
tempLetters.[Type],
tempLetters.[LetterNo],
tempLetters.[Security],
tempLetters.[LetterType],
tempLetters.[Indicator],
tempLetters.[AttachmentCount],
tempLetters.[BodyFileCount],
tempLetters.[WordDocCount],
tempLetters.[SenderTitle],
tempLetters.[ToReceiverList],
tempLetters.[Date],
tempLetters.[PartitionFactor],
0 AS [PriorityID],
tempLetters.[Priority],
0 AS RegisterarUserID,
tempLetters.[RegisterarUserFullName],
tempLetters.[RegisterationDate],
tempLetters.[Archived],
0 AS [TrackingStatusID],
tempLetters.[ModificationServerGuid],
tempLetters.[Guid],
tempLetters.[ModificationDate],
tempLetters.[Age],
0 AS [FirstRootInstanceOwnerID],
0 AS [FirstRootInstanceOwnerSecretarialID]
FROM @ObjectTable tempLetters LEFT JOIN
doc_Letters letters ON tempLetters.[Guid] = letters.[Guid] LEFT JOIN
doc_Indicators indicators ON indicators.[Guid] = tempLetters.[IndicatorGuid]
WHERE
letters.[Guid] IS NULL
如果你在临时表上执行很多操作,那么最好使用Linq,因为SQL适合基于集合的操作,而c#在逻辑操作方面有更好的性能。但是在你的SP中,你主要使用基于集合的操作,所以在SQL中使用它们是很好的。