SQL Server低CPU和读取计数,并行运行时持续时间很长

时间:2015-11-24 16:15:26

标签: sql-server asp.net-web-api

我有两个程序是从web api调用的(使用async / await),而第二个程序在两个程序并行运行时总是花费大约2秒。如果我一次运行一个,它们都非常快 - 对这里可能发生的事情有任何想法吗?

以下是分析器显示的内容: enter image description here

这是第二个单独执行它只需要几毫秒: enter image description here

以下是有问题的程序:

CREATE PROCEDURE [dbo].[UserAccessGetList]
(
  @AuthenticatedUserID INT ,
  @PageNumber INT ,
  @PageSize INT ,
  @UserName VARCHAR(255) ,
  @FirstName VARCHAR(255) ,
  @LastName VARCHAR(255) ,
  @CompanyID INT ,
  @UserStatusID INT
)
AS
BEGIN   
    SET NOCOUNT ON
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

    DECLARE @CurrentCompany HIERARCHYID ,
        @UserTypeID INT ,
        @UserCompanyID INT ,
        @SingleUser BIT = 0

    -- Retrieve the UserTypeID and Company for the user passed in.
    SELECT  @UserTypeID = [userType].[UserTypeID] ,
            @UserCompanyID = [user].[CompanyID] ,
            @CurrentCompany = [OrganizationNode]
    FROM    [dbo].[User] [user]
            INNER JOIN [dbo].[UserType] [userType] ON [user].[UserTypeID] = [userType].[UserTypeID]
            INNER JOIN [dbo].[Company] [company] ON [company].[CompanyID] = [user].[CompanyID]
    WHERE   [user].[UserID] = @AuthenticatedUserID

    -- Returns a result set that lists all the users the user passed in has access to.
    SELECT  [user].[UserID] ,
            [user].[CommissionRate] ,
            [user].[PayrollID] ,
            [user].[PhotoURL] ,
            [user].[UserName] ,
            [user].[PasswordHash] ,
            [user].[UserStatusID] ,
            [user].[ReceivesCommission] ,
            [company].[CompanyID] ,
            [company].[CompanyName] ,
            [company].[CompanyLegalName] ,
            [company].[ShortName] [CompanyShortName] ,
            [company].[CommissionRate] [CompanyCommissionRate] ,                        
            [user].[UserTypeID]
            INTO #users
    FROM    [dbo].[User] [user]
            INNER JOIN [dbo].[Company] [company] ON company.CompanyID = [user].CompanyID
    WHERE   ( ( @UserTypeID = 1 AND @SingleUser = 0 ) OR ( ( @SingleUser = 1 OR @UserTypeID <> 1 ) AND [user].[UserID] = @AuthenticatedUserID ) )
            AND [company].[OrganizationNode].IsDescendantOf(@CurrentCompany) = 1
            AND [company].[CompanyTypeID] = 1

    SELECT  COUNT(1)
    FROM    #users

    SELECT  COUNT(1)
    FROM    [#users] [user]
            INNER JOIN [dbo].[UserStatus] [userStatus] ON [userStatus].[UserStatusID] = [user].[UserStatusID]
            INNER JOIN [dbo].[Contact] [contact] ON [contact].[ContactID] = [user].[UserID]
    WHERE   ( @UserName IS NULL OR [user].[UserName] LIKE '%' + @UserName + '%' )
            AND ( @FirstName IS NULL OR [contact].[FirstName] LIKE '%' + @FirstName + '%' )
            AND ( @LastName IS NULL OR [contact].[LastName] LIKE '%' + @LastName + '%' )
            AND ( @CompanyID IS NULL OR [user].[CompanyID] = @CompanyID )
            AND ( @UserStatusID IS NULL OR [user].[UserStatusID] = @UserStatusID )

    SELECT  [user].[UserID] ,
            [user].[UserName] ,
            [contact].[FirstName] ,
            [contact].[LastName] ,
            [contact].[EmailAddress] ,
            [contact].[TelephoneCount] ,
            [contact].[Telephone] ,
            [contact].[EmailAddressCount] ,
            [contact].[EmailAddress] ,
            [contact].[Address1] ,
            [contact].[Address2] ,
            [contact].[City] ,
            [contact].[State] ,
            [contact].[ZipCode] ,
            [user].[CommissionRate] ,
            [user].[PhotoURL] ,
            [userStatus].[UserStatusID] ,
            [userStatus].[UserStatus] ,
            [user].[ReceivesCommission] ,
            --[user].[CreatedBy]
            --[user].[CreatedDateTime]
            [userType].[UserTypeID] ,
            [userType].[UserType] ,
            [user].[CompanyID] ,
            [user].[CompanyName] ,
            [user].[CompanyName] [CompanyLegalName],
            [user].[CompanyName] [CompanyShortName]
    FROM    [#users] [user]
            INNER JOIN [dbo].[UserStatus] [userStatus] ON [userStatus].[UserStatusID] = [user].[UserStatusID]
            INNER JOIN [dbo].[UserType] [userType] ON [user].[UserTypeID] = [userType].[UserTypeID]
            INNER JOIN [dbo].[vwContact] [contact] ON [contact].[ContactID] = [user].[UserID]
    WHERE   ( @UserName IS NULL OR [user].[UserName] LIKE '%' + @UserName + '%' )
            AND ( @FirstName IS NULL OR [contact].[FirstName] LIKE '%' + @FirstName + '%' )
            AND ( @LastName IS NULL OR [contact].[LastName] LIKE '%' + @LastName + '%' )
            AND ( @CompanyID IS NULL OR [user].[CompanyID] = @CompanyID )
            AND ( @UserStatusID IS NULL OR [user].[UserStatusID] = @UserStatusID )
    ORDER BY [contact].[LastName] ,
            [contact].[FirstName] ,
            [user].[CompanyName]
            OFFSET ( @PageNumber - 1 ) * @PageSize ROWS
    FETCH NEXT @PageSize ROWS ONLY
END

谢谢,

0 个答案:

没有答案