Skip to Main Content
  • Questions
  • Protection against long running query

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: April 11, 2017 - 1:09 pm UTC

Last updated: January 27, 2021 - 4:51 pm UTC

Version: 11 g

Viewed 10K+ times! This question is

You Asked

Hi,

I have a web application use to created report on database table. My concern is about database performance. I don't whant user create report that will kill the database. I know, I can use use the max rownum in the SQL, but eaven a max row can have negative effet on the load of the database. So, I am looking for a more complete solution.

Is there a way to prevent long running query to be executed on the database. Like setting a timeout of 1 minute for the query before killing it. Or anything like that?

Thank you.

and Chris said...

You can use the resource manager to stop queries running too long, using too many resources, etc.

You can read all about it in this whitepaper:

http://www.oracle.com/technetwork/database/performance/resource-manager-twp-133705.pdf

For a worked example of limiting query run time, see this nice article:

https://blog.pythian.com/oracle-limiting-query-runtime-without-killing-the-session/

Rating

  (4 ratings)

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

Comments

No answering the question

Martin, April 11, 2017 - 4:51 pm UTC

I ask about how to prevent long query and the answer is about how to stop long query.
Connor McDonald
April 12, 2017 - 5:02 am UTC

Well...you had plenty of ambiguity :-)

"Is there a way to prevent long running query to be executed on the database. Like setting a timeout of 1 minute for the query before killing it. "

You plan to kill it after 1minute without running it ? .... uh huh.

And of course ... I'm sure that once Chris gave you a link to Resource Manager, you went and read up on the feature, yes ?

Perhaps during the reading you would have seen the MAX_EST_EXEC_TIME directive which can be used to stop a query based on the *estimated* execution time.

Of course you would have seen that yes ?


Preventing V Starting

Stephen, April 11, 2017 - 6:26 pm UTC

The Oracle DB, currently does not have a way to say that this particular query will take x amount of time to run.

It can only tell us how long it is running or has run.

Therefore, Oracle can stop a running query, but not prevent it from starting.

Here is a simple example. You decide to go to the store you have 30 minutes.

You know the average time to get there and back, where the item you want is located, and an average time to checkout(estimates). Your estimate is less than 30 minutes.

Now you actually go to the store. On this particular occasion, you have to stop for every stoplight. The store is crowded, so parking is difficult, and the checkout lines are long. (actual). Your total time turns into 45 minutes.

The next time you go to the store, all the lights are green, the store is almost empty, your actual total time take less than 30 minutes.

Just like this example, there are some things that Oracle cannot predict during the estimation phase. Maybe everything will align just right and the query will return in the time I have allotted. Then again, maybe not, but that is not something that could be determined before deciding to run the query or in this example go to the store.

Stephen Miller

Logic Question

Jeff, January 25, 2021 - 7:36 pm UTC

Seems like a logic question, with a bit of derision thrown in with the answers. Also the provided links don't seem to actually address the question.

Summarizing....

J. Laurindo Chiappa, January 27, 2021 - 2:51 pm UTC

I believe we can summarize the topic in this way : there is NO method to Prevent the database in trying to execute a long query (due to the fact that the database is unable to foresee the real performance of a given query), BUT there is a LOT of possibilities to stop a long-running query, via resource manager , https://asktom.oracle.com/pls/apex/asktom.search?tag=cancelling-long-running-queries explains a little about it) OR (in 18c and newer releases) via CANCEL QUERY , https://www.thegeekdiary.com/how-to-cancel-a-sql-query-in-oracle-database-18c/ is the example)...

Regards,

J. Laurindo Chiappa
Chris Saxon
January 27, 2021 - 4:51 pm UTC

You got it

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.