2016-08-05 30 views
1

我想快速存储过程。通过sql获取数据时需要太多时间的存储过程

在获取记录时,存储过程花费的时间太多。

即它将花费太多的时间(超过1分钟).. 那么如何快速存储过程?

这里是我的存储过程:

ALTER procedure [dbo].[usp_PostedAds_GetAllByCountryCityCategoryIdAdType](@countryId int,@cityId int, @CategoryId int,@AdType int) As 
begin 
    DECLARE @ParentCatID int 

    set @ParentCatID =isnull((SELECT ParentID FROM Category WHERE ID = @CategoryId),0) 

     if(@ParentCatID>0) 
      begin 
      select PostedAds.*,isnull((select top 1 PostedAdsImages.AdsImage from PostedAdsImages where PostedAdsImages.PostedAdsID=PostedAds.ID),'noimage.jpg') as AdsImage 
      ,(select Name from dbo.Category where id=PostedAds.CategoryID) as CategoryName,(select ShowReply from dbo.Category where id=PostedAds.CategoryID) as ShowReply, 
      isnull((select mf.ID from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),0) as FeatureId, 
      isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') as FeatureTitle 
      ,(select isnull(dbo.ConcatDynamicFields(PostedAds.Id),'')) as DynamicFields, 
      isnull((select top 1 ul.IsLogin from UserLogin ul where ul.UserId=PostedAds.UserId order by ul.ID desc),0) as IsLoggedinUser 
      ,isnull((select dbo.ConcatLocations1(postedAds.Id)),'') as location, 
      (select Country_State.currency from Country_State where Country_State.ID=PostedAds.CountryID) as Currency 
      ,c.IsPriceAvailable 
      from PostedAds join Category c on c.ID=PostedAds.CategoryID 
      where PostedAds.Status=1 and [email protected] and 
      ([email protected] or PostedAds.VisiblityRestriction=3)and [email protected] and [email protected] 
      ORDER BY CASE 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Premium Ads' THEN '1' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Urgent Ads' THEN '2' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = '' THEN '3' 
       ELSE isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') 
       END 
       ,PostedAds.CreatedDate desc 
      end 
     else 
      begin 
      if(@CategoryId>0) 
       begin 
       select PostedAds.*,isnull((select top 1 PostedAdsImages.AdsImage from PostedAdsImages where PostedAdsImages.PostedAdsID=PostedAds.ID),'noimage.jpg') as AdsImage 
       ,(select Name from dbo.Category where id=PostedAds.CategoryID) as CategoryName,(select ShowReply from dbo.Category where id=PostedAds.CategoryID) as ShowReply, 
       isnull((select mf.ID from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),0) as FeatureId, 
       isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') as FeatureTitle 
       ,(select isnull(dbo.ConcatDynamicFields(PostedAds.Id),'')) as DynamicFields, 
       isnull((select top 1 ul.IsLogin from UserLogin ul where ul.UserId=PostedAds.UserId order by ul.ID desc),0) as IsLoggedinUser 
       ,isnull((select dbo.ConcatLocations1(postedAds.Id)),'') as location, 
       (select Country_State.currency from Country_State where Country_State.ID=PostedAds.CountryID) as Currency 
       ,c.IsPriceAvailable 
       from PostedAds join Category c on c.ID=PostedAds.CategoryID 
       where PostedAds.Status=1 and [email protected] and 
       ([email protected] or PostedAds.VisiblityRestriction=3) 
       and [email protected] 
       and [email protected] 
       ORDER BY CASE 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Premium Ads' THEN '1' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Urgent Ads' THEN '2' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = '' THEN '3' 
       ELSE isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') 
       END 
       ,PostedAds.CreatedDate desc 
       End 
      else 
       Begin 
       select PostedAds.*,isnull((select top 1 PostedAdsImages.AdsImage from PostedAdsImages where PostedAdsImages.PostedAdsID=PostedAds.ID),'noimage.jpg') as AdsImage 
       ,(select Name from dbo.Category where id=PostedAds.CategoryID) as CategoryName,(select ShowReply from dbo.Category where id=PostedAds.CategoryID) as ShowReply, 
       isnull((select mf.ID from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),0) as FeatureId, 
       isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') as FeatureTitle 
       ,(select isnull(dbo.ConcatDynamicFields(PostedAds.Id),'')) as DynamicFields, 
       isnull((select top 1 ul.IsLogin from UserLogin ul where ul.UserId=PostedAds.UserId order by ul.ID desc),0) as IsLoggedinUser 
       ,isnull((select dbo.ConcatLocations1(postedAds.Id)),'') as location, 
       (select Country_State.currency from Country_State where Country_State.ID=PostedAds.CountryID) as Currency 
       ,c.IsPriceAvailable 
       from PostedAds join Category c on c.ID=PostedAds.CategoryID 
       where PostedAds.Status=1 and [email protected] and 
       ([email protected] or PostedAds.VisiblityRestriction=3) 
       --and [email protected] 
       and [email protected] 
       ORDER BY CASE 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Premium Ads' THEN '1' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = 'Urgent Ads' THEN '2' 
       WHEN isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') = '' THEN '3' 
       ELSE isnull((select mf.Title from MembershipSuscription ms left join dbo.MembershipPlan mp on mp.ID =ms.MembershipPlanID join MembershipFeature mf on mf.ID =mp.FeatureID where ms.ID=PostedAds.SubscriptionId),'') 
       END 
       ,PostedAds.CreatedDate desc 
       End 
      end  


end 

这是我的表架构:

CREATE TABLE [dbo].[Category](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [ParentID] [int] NOT NULL, 
    [Name] [varchar](max) NULL, 
    [CategoryImage] [varchar](max) NULL, 
    [Active] [bit] NOT NULL CONSTRAINT [DF_Category_Active] DEFAULT ((1)), 
    [Offer_Label] [varchar](100) NULL, 
    [Wanted_Label] [varchar](100) NULL, 
    [seo_keywords] [varchar](255) NULL, 
    [seo_description] [varchar](255) NULL, 
    [IsProduct] [bit] NULL, 
    [Order] [int] NULL DEFAULT ((0)), 
    [IsPriceAvailable] [bit] NOT NULL DEFAULT ((1)), 
    [ShowReply] [bit] NOT NULL DEFAULT ((1)), 
    [AllowImages] [bit] NOT NULL DEFAULT ((1)), 
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED 
(
    [ID] 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 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[Country_State] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[Country_State](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [varchar](200) NULL, 
    [ParentID] [int] NULL, 
    [HasState] [bit] NULL, 
    [Currency] [varchar](50) NULL, 
    [IsActive] [bit] NOT NULL CONSTRAINT [DF_Country_State_IsActive] DEFAULT ((1)), 
    [IsTopCountry] [bit] NULL CONSTRAINT [DF_Country_State_IsTopCountry] DEFAULT ((0)), 
CONSTRAINT [PK_Country_State] PRIMARY KEY CLUSTERED 
(
    [ID] 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 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[MembershipFeature] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[MembershipFeature](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [Title] [nvarchar](255) NULL, 
    [Type] [nvarchar](255) NULL, 
    [Status] [bit] NULL, 
CONSTRAINT [PK_MembershipFeature] PRIMARY KEY CLUSTERED 
(
    [ID] 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].[MembershipPlan] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[MembershipPlan](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [FeatureID] [int] NULL, 
    [NumberOfAds] [int] NULL, 
    [AdValidity] [int] NULL, 
    [PlanValidity] [int] NULL CONSTRAINT [DF_MembershipPlan_PlanValidity] DEFAULT ((0)), 
    [Price] [decimal](18, 2) NULL, 
    [Discount] [decimal](18, 2) NOT NULL, 
CONSTRAINT [PK_Plan_1] PRIMARY KEY CLUSTERED 
(
    [Id] 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].[MembershipSuscription] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[MembershipSuscription](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [UserID] [bigint] NOT NULL, 
    [MembershipPlanID] [int] NOT NULL, 
    [StartDate] [datetime] NULL CONSTRAINT [DF_MembershipSuscription_StartDate] DEFAULT (getdate()), 
    [EndDate] [datetime] NULL CONSTRAINT [DF_MembershipSuscription_EndDate] DEFAULT (getdate()), 
    [AmountPaid] [decimal](18, 2) NULL, 
    [ServiceTax] [decimal](18, 2) NULL, 
    [Status] [bit] NULL, 
    [CreateDate] [datetime] NULL CONSTRAINT [DF_MembershipSuscription_CreateDate] DEFAULT (getdate()), 
    [AdRemaning] [int] NULL, 
CONSTRAINT [PK_MerchantMembershipSuscription] PRIMARY KEY CLUSTERED 
(
    [ID] 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].[PostedAds] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[PostedAds](
    [ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [UserId] [bigint] NOT NULL, 
    [CategoryID] [int] NOT NULL, 
    [Price] [decimal](18, 2) NULL, 
    [Title] [nvarchar](max) NULL, 
    [Description] [nvarchar](max) NULL, 
    [MAPAddress] [nvarchar](max) NULL, 
    [VisiblityRestriction] [int] NULL, 
    [CountryID] [int] NOT NULL, 
    [CityId] [int] NOT NULL, 
    [Locality] [nvarchar](max) NULL, 
    [Status] [int] NOT NULL CONSTRAINT [DF_PostedAds_Status] DEFAULT ((0)), 
    [Seo_keyword] [nvarchar](555) NULL, 
    [Seo_description] [nvarchar](555) NULL, 
    [AdType] [int] NULL, 
    [PromotionType] [int] NOT NULL CONSTRAINT [DF_PostedAds_PromotionType] DEFAULT ((0)), 
    [SubscriptionId] [int] NULL CONSTRAINT [DF_PostedAds_SubscriptionId] DEFAULT ((0)), 
    [CreatedDate] [datetime] NULL CONSTRAINT [DF_PostedAds_CreatedDate] DEFAULT (getdate()), 
CONSTRAINT [PK_PostedAds] PRIMARY KEY CLUSTERED 
(
    [ID] 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 
/****** Object: Table [dbo].[PostedAdsImages] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[PostedAdsImages](
    [ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [PostedAdsID] [bigint] NOT NULL, 
    [AdsImage] [nvarchar](max) NULL, 
    [ImageTitle] [nvarchar](500) NULL, 
    [CreatedDate] [datetime] NULL CONSTRAINT [DF_PostedAdsImages_CreatedDate] DEFAULT (getdate()), 
CONSTRAINT [PK_PostedAdsImages] PRIMARY KEY CLUSTERED 
(
    [ID] 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 
/****** Object: Table [dbo].[UserLogin] Script Date: 8/5/2016 5:14:36 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[UserLogin](
    [ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [UserId] [bigint] NULL, 
    [LoginDate] [date] NULL CONSTRAINT [DF_UserLogin_LoginDate] DEFAULT (getdate()), 
    [LoginTime] [varchar](50) NULL, 
    [LogOutTime] [varchar](50) NULL, 
    [IpAddress] [varchar](50) NULL, 
    [IsLogin] [bit] NULL, 
CONSTRAINT [PK_UserLogin] PRIMARY KEY CLUSTERED 
(
    [ID] 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 
SET ANSI_PADDING OFF 
GO 

如何快速此SP?

有没有解决方法?

+0

请执行后的计划,要知道如何使用此链接:http://stackoverflow.com/questions/7359702/how-do-i-obtain-a-query-execution-plan – TheGameiswar

+0

还请张贴涉及的表格模式和计数 – TheGameiswar

+0

你能告诉我有什么变化需要在我的sp?我不知道如何发布架构或执行计划? –

回答

0
ALTER PROCEDURE [dbo].[usp_PostedAds_GetAllByCountryCityCategoryIdAdType] (@countryId int, @cityId int, @CategoryId int, @AdType int) 
AS 
BEGIN 
    DECLARE @ParentCatID int 

    SET @ParentCatID = ISNULL((SELECT 
    ParentID 
    FROM Category 
    WHERE ID = @CategoryId) 
    , 0) 

    SELECT 
    pa.*, 
    ISNULL(t1.AdsImage, 'noimage.jpg') AS AdsImage, 
    ct.Name AS CategoryName, 
    ct.ShowReply AS ShowReply, 
    ISNULL(mf.ID, 0) AS FeatureId, 
    ISNULL(mf.Title, '') AS FeatureTitle, 
    (SELECT 
     ISNULL(dbo.ConcatDynamicFields(pa.Id), '')) 
    AS DynamicFields, 
    ISNULL(t2.IsLogin, 0) AS IsLoggedinUser, 
    ISNULL((SELECT 
     dbo.ConcatLocations1(pa.Id)), '') AS location, 
    cs.currency AS Currency, 
    ct.IsPriceAvailable 
    FROM PostedAds pa 
    LEFT JOIN Category ct 
    ON pa.CategoryID = ct.ID 
    LEFT JOIN Country_State cs 
    ON pa.CountryID = cs.ID 
    LEFT JOIN (SELECT TOP 1 pai.AdsImage, pai.PostedAdsID 
    FROM PostedAdsImages pai inner join PostedAds pa2 on 
    pai.PostedAdsID = pa2.ID) t1 
    ON t1.PostedAdsID = pa.ID 
    LEFT JOIN MembershipSuscription ms 
    ON pa.SubscriptionId = ms.ID 
    LEFT JOIN dbo.MembershipPlan mp 
    ON ms.MembershipPlanID = mp.ID 
    LEFT JOIN MembershipFeature mf 
    ON mp.FeatureID = mf.ID 
    LEFT JOIN (SELECT TOP 1 ul.IsLogin, ul.UserId 
    FROM UserLogin ul inner join PostedAds pa2 on 
    ul.UserId = pa2.UserId 
    ORDER BY ul.ID DESC) t2 
    ON t2.UserId = pa.UserId 
    WHERE pa.Status = 1 
    AND pa.CountryID = @countryId 
    AND (pa.CityId = @cityId 
    OR pa.VisiblityRestriction = 3) 
    AND pa.AdType = @AdType 
    AND 
    CASE 
     WHEN @ParentCatID > 0 AND 
     pa.CategoryID = @CategoryId THEN 1 
     WHEN @CategoryId > 0 AND 
     ct.ParentID = @CategoryId THEN 1 
     ELSE 0 
    END = 1 
    ORDER BY CASE 
    WHEN ISNULL(mf.Title, '') = 'Premium Ads' THEN '1' 
    WHEN ISNULL(mf.Title, '') = 'Urgent Ads' THEN '2' 
    WHEN ISNULL(mf.Title, '') = '' THEN '3' 
    ELSE ISNULL(mf.Title, '') 
    END 
    , pa.CreatedDate DESC 

END 
+0

@jBaby ....给出错误.... Msg 4104,级别16,状态1,过程usp_PostedAds_GetAllByCountryCityCategoryIdAdType_new,行36 无法绑定多部分标识符“pa.ID”。 Msg 4104,Level 16,State 1,Procedure usp_PostedAds_GetAllByCountryCityCategoryIdAdType_new,Line 48 无法绑定多部分标识符“pa.UserId”。 –

+0

plz给一些建议.... –

+0

@RizwanaAhmed我已更正alter alter,请再次检查 – JBaby

相关问题