cost of complex query that returns zero records.

if a complex SELECT statement should return 0 rows, does it still consume approximately the same level of resources to finish the query to say 'no records'.
LVL 5
25112Asked:
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.

Lee SavidgeCommented:
The cost of the query is the actual running of the query, not specifically the number of rows returned. The number of rows returned is the result of the query and this constitutes almost nothing to the query cost.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sure. How the engine knows that there's no record to return? It needs to process the query to return records or no records in the case of no rows found that satisfies the query.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can activate the option "Include Actual Execution Plan" to get the Query Plan and see what the engine is performing in background.
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Of course the overall costs are including retrieving the results. So, if the Optimizer is able to supply an execution plan having index-only access, you will see not much of a difference, and execution is "fast".
If a full table scan is involved, you won't see a difference either - but because the full table scan will have high costs, the query is expensive, no matter how many rows will result.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If you look at SQL Server's Query Order of Execution, the only savings with a zero-row query vs. a non-zero row query is that it doens't process the SELECT on down, as there are no rows.  It still has to process FROM with JOINs, WHERE, GROUP BY, HAVING, etc.

< kind of piling on, I know.. >
0
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:
There is no direct relationship between resource consumption and number of rows returned (server wise).  The indirect relationship is made by the optimizer whom based on statistics of existing rows will determine the best course of action to retrieve rows at the lowest cost.
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
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 2008

From novice to tech pro — start learning today.