Thread: When your query takes too long ...


Permlink Replies: 44 - Pages: 3 [ 1 2 3 | Next ] - Last Post: Sep 7, 2009 2:24 AM Last Post By: user11876624
Rob van Wijk

Posts: 5,305
Registered: 08/17/06
When your query takes too long ...
Posted: Apr 27, 2007 2:17 PM
Click to report abuse...   Click to reply to this thread Reply
This thread is not a question.

I will use this thread to refer to in other threads, to keep me from saying the same thing over and over again.

What to do when your query is too slow?

First of all, you have to know why it is slow. What is the real cause of your problem. If the reason why is not known, suggesting to rewrite the query, or hinting the query, suggesting parallellization et cetera is not very productive. Once in a while you may get lucky. But even then you have to realize that if your problem seems "solved", but you don't know why, nothing guarantees you that the problem won't come back tomorrow. So the first step should always be to investigate the root cause.

The tools at your disposal are, among more:
- dbms_profiler
- explain plan
- SQL*Trace / tkprof
- statspack

Use dbms_profiler if you want to know where time is being spent in PL/SQL code. Statspack is a must if you are a dba and want to know what is going on in your entire database. For a single query or a small process, explain plan and SQL*Trace and tkprof are your tools.

explain plan

in SQL*Plus you have to type:
explain plan for <your query>;
select * from table(dbms_xplan.display);


When you get error messages or a message complaining about an old version of plan_table, make sure you run the script utlxplan.sql.

The output you get here basically shows you what the cost based optimizer expects. It gives you an idea on why the cost based optimizer chooses an access path.

SQL*Trace/tkprof

For this you have to type in SQL*Plus:
- alter session set sql_trace true;
- <run your query>
- disconnect (this step is important, because it ensures all cursors get closed, and "row source operation" is generated)
- identify your trace file in the server directory as specified in the parameter user_dump_dest
- on your operating system: tkprof <trace file> a.txt sys=no sort=prsela exeela fchela

The file a.txt will now give you valuable information on what has actually happened. No predictions but the truth.

By comparing the output from explain plan with the output from tkprof, you are able to identify the possible problem areas.

So before rushing into possible solutions, always post the output of explain plan and tkprof with your question and don't forget to post them between the tags [code] and [/code] for readability.

PS: I am fully aware that this text is only a tiny fraction of what can be done, and that other people may choose different tools and actions, but the above gives you a very reasonable start at solving your performance problem.

Edited by: Rob van Wijk on 17-sep-2008 16:32

Adjusted the layout for the new forum software
Ljuba

Posts: 1,106
Registered: 01/01/07
Re: When your query takes too long ...
Posted: Apr 27, 2007 2:33 PM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Thanks a lot for your post Rob.

Simplifying thing is right way!

And your post doing exactly that on the theme "Performance".

Regards!
sgalaxy

Posts: 4,941
Registered: 01/26/06
Re: When your query takes too long ...
Posted: Apr 28, 2007 2:20 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hi Rob...
Nice try ... but i'm afraid that this thread will be lost in the great number of threads posted every day......, so you may refer to this thread to each post as needed....!!!!!!!!
It might be better ...(????) if this could be posted as an announcement in the top of the thread lists......

My greetings,
Simon
Nicloei W

Posts: 1,752
Registered: 04/13/07
Re: When your query takes too long ...
Posted: Apr 28, 2007 2:27 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Hi Rob,

This thread is great,

Is it also possible to post the the relevant links, for tuning tools
like dbms_profilter, and explain plan, etc,

also most importantly is how to use "tkprof" and to read explain plan,

I completed nearly 4 yrs working on oracle as developer but still dont have enough confidence to understand the explain plan and never used tkprof till now,

dont know how much time other guys take ;)

also i read somewhere the sequence of statments we should follow while writing a sql query,
ex
1. filter column shold come first,
2. Thn join conditions,
3 etc

and finally LIKE operater,

a blog or post would be useful,

thanks for taking this effort

Message was edited by:
Nicloei W
sgalaxy

Posts: 4,941
Registered: 01/26/06
Re: When your query takes too long ...
Posted: Apr 28, 2007 2:40 AM   in response to: Nicloei W in response to: Nicloei W
Click to report abuse...   Click to reply to this thread Reply
Hi ,
The whole library of docs describing almost everything is found on the first page of OTN.
http://www.otn.oracle.com.....
There is a menu titled "Documentation" where directs to other web page containing all Oracle products... You simply select the product , version and get the list of all material of this....
Especially , regarding Oracle db10g , there is a list of 50-60 e-books and "search" utility to find any key-word you are looking for.....Try it ...it's simple...!!!!!!!

My greetings,
Simon
Asif Momen

Posts: 1,906
Registered: 02/10/07
Re: When your query takes too long ...
Posted: Apr 28, 2007 2:50 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Good work Rob !!
SID3

Posts: 369
Registered: 09/27/06
Re: When your query takes too long ...
Posted: May 8, 2007 11:08 PM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Rob,

This is an excellent thread. Atleast it gave us an idea of best approaches for performance related issues.
Satyaki_De

Posts: 6,686
Registered: 12/20/06
Re: When your query takes too long ...
Posted: May 8, 2007 11:25 PM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Excellent.....

Regards.

Satyaki De.
DEV3

Posts: 91
Registered: 05/14/07
Re: When your query takes too long ...
Posted: Jun 9, 2007 3:16 AM   in response to: Satyaki_De in response to: Satyaki_De
Click to report abuse...   Click to reply to this thread Reply
Although i was late to read this post..i found it pretty handy n useful..

great job Rob
Rosario Vigilante

Posts: 2,377
Registered: 02/07/01
Re: When your query takes too long ...
Posted: Jun 10, 2007 3:13 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Thanks Rob,

It is really a lot of profit.
For me, I would insert it in the Oracle's documentation.
Pankaj M

Posts: 277
Registered: 06/27/07
Re: When your query takes too long ...
Posted: Jul 5, 2007 12:34 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Thanks Rob
greate work n very useful


Thanks Again

Regards,
Pankaj

wallison

Posts: 79
Registered: 10/18/99
Re: When your query takes too long ...
Posted: Jul 10, 2007 8:37 AM   in response to: Rob van Wijk in response to: Rob van Wijk
Click to report abuse...   Click to reply to this thread Reply
Rob

Great thread.

Wayne
fbortel

Posts: 408
Registered: 11/26/98
Snowiwng under?
Posted: Jul 12, 2007 6:18 AM   in response to: sgalaxy in response to: sgalaxy
Click to report abuse...   Click to reply to this thread Reply
Sticky - make it sticky!
Where's the sticky option?
KarTiK

Posts: 121
Registered: 07/10/07
Re: Snowiwng under?
Posted: Jul 12, 2007 11:52 AM   in response to: fbortel in response to: fbortel
Click to report abuse...   Click to reply to this thread Reply
Hi

I really appreciate your work.Great Thread.

Thanks.
KarTiK.
dmill

Posts: 197
Registered: 01/11/07
Re: Snowiwng under?
Posted: Jul 12, 2007 12:19 PM   in response to: KarTiK in response to: KarTiK
Click to report abuse...   Click to reply to this thread Reply
Seriously, can a moderator make this a sticky? It could prevent 20 posts a day asking 'PLZ HELP W/ SQL TUNING'
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums