query taking a lot of time

WITH ISO_DR(PolicyID,  CompanyID, Persistency) AS
            (SELECT P.policyID, P.companyID, (SELECT effectiveDate from dbo.Policy where policyID =(select dbo.fnOriginalPolicyID(@policynum))) as PERSISTENCY
                  FROM Hallmark..Policy P WITH (NOLOCK))

Is there a better way to write this?
sqlcuriousAsked:
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.

LowfatspreadCommented:
please post the actual query you are using ... you've only posted the cte
0
LowfatspreadCommented:
also post the definition of dbo.fnOriginalPolicyID
0
LowfatspreadCommented:
is this a cross database query?

or are the policy tables actually the same one?
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

jogosCommented:
Basicly for each resultrow of the hallmark..policy table you do a select in (same/other as question of lowfatspread) a table that must return a specific value.

My reading this part of code will always returen the same value, one value
It could better be executed on it's own to assign its result in a variable and use that variable in the select.
declare @effectivedate as datetime
SELECT @effectivedate= effectiveDate from dbo.Policy where policyID =(select dbo.fnOriginalPolicyID(@policynum))
WITH ISO_DR(PolicyID,  CompanyID, Persistency) AS
(SELECT P.policyID, P.companyID,@effectivedate as PERSISTENCY 
                  FROM Hallmark..Policy P WITH (NOLOCK))

Open in new window

That would be a gain that counts.


In case there is a link between the 2 tables it can be rewriten as a join , now chosen for inner join and because I don't see the link with a dummy join-condition.
SELECT P.policyID, P.companyID,  a. PERSISTENCY 
                  FROM Hallmark..Policy P WITH (NOLOCK)
inner join (SELECT effectiveDate from dbo.Policy where policyID =(select dbo.fnOriginalPolicyID(@policynum))) as a on 1 = 1

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
jogosCommented:
sorry, forgot the ;  before the WITH
0
jogosCommented:
Anny feedback?
0
sqlcuriousAuthor Commented:
thnks
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.