Avatar of Mayank Tripathi
Mayank Tripathi

asked on 

Help required to optimize the below sql query .

SELECT  *
FROM
(
SELECT EntityId, [Key],Value FROM [dbo].[GA] with (nolock))t
PIVOT(MIN(Value)
      FOR [Key]
      IN (FirstName,LastName,Organization,StreetAddress,TrainingInstitution,EmploymentInstitution,SecondaryDegree,ZipPostalCode,CountryId,[city],StateProvinceId,[degree],LastVisitedPage,
UseRewardPointsDuringCheckout,SelectedShippingOption,SelectedPaymentMethod)
      )p
                where EntityId in (select id from dbo.cust with (nolock) where LastUpdated >=getdate()-5)

Open in new window

Microsoft SQL Server

Avatar of undefined
Last Comment
Mayank Tripathi
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Please try this and see it helps -

SELECT  *
FROM
(
     SELECT EntityId, [Key],Value FROM [dbo].GA g with (nolock)
     WHERE EXISTS ( select NULL from dbo.cust c with (nolock) where c.id = g.EntityId AND c.LastUpdated >= getdate()-5 )
     
)t
PIVOT
(
     MIN(Value) FOR [Key]
           IN (  FirstName,LastName,Organization,StreetAddress,TrainingInstitution,EmploymentInstitution,SecondaryDegree,ZipPostalCode,
                     CountryId,[city],StateProvinceId,[degree],LastVisitedPage,
                UseRewardPointsDuringCheckout,SelectedShippingOption,SelectedPaymentMethod
                )
)p         

Open in new window

Avatar of Mayank Tripathi
Mayank Tripathi

ASKER

Above solution is also taking too long time to execute.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mayank Tripathi
Mayank Tripathi

ASKER

Thanks
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo