How to rewrite this correlated subquery using joins

How to rewrite this correlated subquery using joins . This query is taking more time to execute. i want to reduce the execution time.

SELECT n.*, m.ext_id, m.version, m.cust_id, m.prd_id
FROM ext_pln n, ext_plc_ver m
WHERE m.ext_pln_id = n.ext_pln_id
AND m.plcy_version = (SELECT max(plcy_version) FROM ext_plc_ver WHERE EXTN_CD = m.EXTN_CD)
Surya MAsked:
Who is Participating?
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..

Changed to JOIN.

SELECT n.*, m.ext_id, m.version, m.cust_id, m.prd_id
FROM ext_pln n INNER JOIN ext_plc_ver m ON m.ext_pln_id = n.ext_pln_id
WHERE m.plcy_version = (SELECT max(plcy_version) FROM ext_plc_ver WHERE EXTN_CD = m.EXTN_CD)

Open in new window

0
Pawan KumarDatabase ExpertCommented:
This should help in performance..

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

SELECT max(plcy_version) plcy_version , EXTN_CD INTO #ext_plc_ver FROM ext_plc_ver GROUP BY EXTN_CD

CREATE CLUSTERED INDEX ix_plcy_version_extn ON #ext_plc_ver(plcy_version , EXTN_CD)

SELECT n.*, m.ext_id, m.version, m.cust_id, m.prd_id
FROM ext_pln n 
INNER JOIN ext_plc_ver m ON m.ext_pln_id = n.ext_pln_id
INNER JOIN #ext_plc_ver ev ON ev.plcy_version = m.plcy_version AND ev.EXTN_CD = m.EXTN_CD

Open in new window

0
Pawan KumarDatabase ExpertCommented:
Another could be --

SELECT n.*, m.ext_id, m.version, m.cust_id, m.prd_id
FROM ext_pln n INNER JOIN ( SELECT * FROM 
							( SELECT * , ROW_NUMBER() OVER (PARTITION BY EXTN_CD ORDER BY plcy_version DESC) rnk FROM ext_plc_ver )r WHERE rnk = 1
						  )m
ON m.ext_pln_id = n.ext_pln_id

Open in new window

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.