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

Mayank TripathiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
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

0
Mayank TripathiAuthor Commented:
Above solution is also taking too long time to execute.
0
Pawan KumarDatabase ExpertCommented:
ohh.....
try adding temp table..

IF OBJECT_ID('tempdb..#YourTempTableName') IS NOT NULL
    DROP TABLE #YourTempTableName

SELECT EntityId, [Key],Value INTO [#YourTempTableName] FROM yourTablName [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 )

SELECT  *
FROM #YourTempTableName 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

also can you share the execution plan.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank TripathiAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.