简体   繁体   English

临时表和用户定义的表或使用c#linq代码可提高性能

[英]better performance with temp table and user defined table or use c# linq code

i have some stored procedure that i use it too much , i want to know that its better that to use temp table in tempdb database or use user defined table and get it from c# dataTable or forget them and use linq c# to handle it in c# code . 我有一些使用过多的存储过程,我想知道最好在tempdb数据库中使用temp表或使用用户定义的表并从c#dataTable中获取它,或者忘记它们并使用linq c#在c#中处理它。代码。 which of them have a better performance in below situation : 1. a litter record (one record too 1000) 2. 1000 to 1000000 3. larger 1000000 record some sample of my stored procedure : 在下面的情况下,它们中哪个具有更好的性能:1.垃圾记录(一个记录也为1000)2. 1000到1000000 3.较大的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

If you are performing lot of operations on your temp table then it will better to use Linq because SQL is good for set based operation while C# has better performance in logical operations. 如果在临时表上执行大量操作,则最好使用Linq,因为SQL适合基于集合的操作,而C#在逻辑操作中具有更好的性能。 But in you'r SP you are mostly using set based operation so it is good to have them in SQL. 但是在您的SP中,您大多使用基于集合的操作,因此最好在SQL中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM