Skip to Main Content
  • Questions
  • How to know the number of records to be retrieved and cost of the query without really executing in the database

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Jagan.

Asked: February 28, 2018 - 11:03 am UTC

Last updated: February 28, 2018 - 3:31 pm UTC

Version: 12.1.0.2

Viewed 1000+ times

You Asked

Dear Team,

I would like to know ,would it be possible to know number of records fetched as well as cost incurred before directly executing the query in the database via SQLPLUS.

I have worked with explain plan utility which returns the number of records and cost but can this provide accurate results or if any different approach/methodology and any packages available.

Please explain with a quick example.

Thanks a lot.
Regards
Jagan.

and Chris said...

Q: How long will it take you to run one mile?

A: ¯\_(ツ)_/¯

Q: OK, so what's the average time it takes you run a mile?

A: 8 minutes

Q: So if you start now, it'll take you 8 minutes, right?

A: The route is all uphill into a strong breeze.

Q: So 9 minutes?

A: And I've hurt my knee

Q: 10 minutes?

A: Let's stop guessing and time it!

If you want to know how long it'll take someone to run a mile on a specific route & date, the only way to know for sure is to time the run. Sure, you can look at their past runs, the route and conditions to get a reasonable estimate. But that's the best you'll get - a guess. The exact time depends on the conditions on the day.

It's the same with running a query. You could do extensive analysis of what's in your tables and how they relate to your query. And come up with The Number of Rows This Query Will Return.

But after you complete this and before you run it, someone else inserts 10,000 rows. Making all that analysis useless.

An explain plan uses stats to (hopefully) generate a reasonable estimate of the number of rows it'll return. But this can be wrong for many reasons:

https://asktom.oracle.com/Misc/when-explanation-doesn-sound-quite.html

So, the only way to know for certain how many rows a query fetches is to:

Run the query!

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Jagan Kottakota, February 28, 2018 - 11:50 am UTC

Thanks for the response

Workarround

Gh, February 28, 2018 - 12:31 pm UTC

Another solution is to make it run by a collegue.
Chris Saxon
February 28, 2018 - 3:31 pm UTC

:)

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database