Home » SQL & PL/SQL » SQL & PL/SQL » Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2]
Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642464] Fri, 11 September 2015 07:11 Go to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
HI All,

We are facing this issue of an query running extremely well and returing all records in 25-30 mins in TOAD and SQL developer
However when we are scheduling it in unix server ( oracle DB), it is taking around 3 hours

Any idea where and what to look.

Thanks and Regards
Saurabh
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [message #642466 is a reply to message #642464] Fri, 11 September 2015 07:25 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
trace the session and find out where the time is being spent
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [message #642467 is a reply to message #642466] Fri, 11 September 2015 07:27 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And please don't post the same topic in multiple sub-forums. I've now merged them.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642468 is a reply to message #642464] Fri, 11 September 2015 07:51 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
what does "scheduling it in unix server" mean?

But apart from that, I should mention that most people find it difficult to tune SQL that they have not seen. In your previous topics, you did at least post the query. Though I see that you didn't provide any of the other necessary information.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642469 is a reply to message #642468] Fri, 11 September 2015 07:58 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Hi James/Moderator,'
Apologies for mistakenly posting the query at 2 forums, i didn;t post the query as here we are not looking to tune the query as query is running fine and in acceptable time in clients ( Toad or sql developer), it just that when we schedule it in server which have DB hosted ( using the SQLplus connection) the query is taking very long to run.

Ideally we were expecting the query to run faster as it is directly hitting the oracle DB engine and not making connection through client.

please let me know if you feel i am not clear.

Thanks and Regards
Saurabh
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642470 is a reply to message #642469] Fri, 11 September 2015 08:15 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Not even close to clear.
All you've told us is:
You have a query
It's fast when you run it in toad / sql developer
It's slow when you run it on the server.

You haven't told us how you run it on the server (sqlplus is apparently involved somehow)
You have told us nothing about the query itself.
You haven't said if the query is literally the only thing run on the server or if other code is involved.

You've given us nothing to work on what so ever.

So the only useful advice we can give is to trace the session.

If you want any more advice you're going to have to supply some actual details about the query and how exactly it's being run.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642471 is a reply to message #642469] Fri, 11 September 2015 08:28 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
saurabh.sha wrote on Fri, 11 September 2015 13:58
Hi James/Moderator,'
Apologies for mistakenly posting the query at 2 forums, i didn;t post the query as here we are not looking to tune the query as query is running fine and in acceptable time in clients ( Toad or sql developer), it just that when we schedule it in server which have DB hosted ( using the SQLplus connection) the query is taking very long to run.

Ideally we were expecting the query to run faster as it is directly hitting the oracle DB engine and not making connection through client.

please let me know if you feel i am not clear.

Thanks and Regards
Saurabh

UNIX is case sensitive. So what you need to do is re-write the query to put all the key words into upper case.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642501 is a reply to message #642469] Sat, 12 September 2015 14:48 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
saurabh.sha

query is running fine and in acceptable time in clients ( Toad or sql developer)

TOAD, by default, returns only 500 rows (at least, in version I use - 12.5. If I remember well, some of previous versions used to return 1000 rows). I believe that SQL Developer does the same. What happens when you run that query in yet another, reliable tool called SQL*Plus? How long does the query take in SQL*Plus?

Therefore, I suspect that query really takes a lot of time; it is TOAD and SQL Developer that fooled you.

Now, query might be tuned so that it runs faster, but - in order to do that, you need to provide some more details. Have a look at our Performance Tuning sub-forum and read the sticky topic.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642645 is a reply to message #642501] Wed, 16 September 2015 04:26 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Thanks Little foot but it runs completly in TOAD and i am getting the all 30000 reocrds in 20 mins in TOAD, i am running the same select statement by putting it into a sql file and running in SQLPLUS

Apologies for late reply but we have to take some approvals to run the trace in Prod server, i am attaching the Trace, SQL Query and the explain plan

Please advise if you see any issues
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642646 is a reply to message #642645] Wed, 16 September 2015 04:26 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Explain Plan
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642647 is a reply to message #642646] Wed, 16 September 2015 04:27 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Query
  • Attachment: Query.txt
    (Size: 51.28KB, Downloaded 1408 times)
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642650 is a reply to message #642501] Wed, 16 September 2015 05:20 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Littlefoot wrote on Sun, 13 September 2015 01:18
I believe that SQL Developer does the same.


Yes, but 50 rows in query result tab in my SQL Developer version 3.2.

So, whether TOAD or SQL Developer, the time shown will be for the first few rows fetched, subsequently on scrolling down, next rows will be fetched and the time will increase.

[Updated on: Wed, 16 September 2015 05:30]

Report message to a moderator

Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642654 is a reply to message #642650] Wed, 16 September 2015 06:10 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Hi Lalit,

Its the entire time ( 20 mins) scrolling and getting the entire records ( 30K)

Thanks
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642659 is a reply to message #642645] Wed, 16 September 2015 09:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
saurabh.sha wrote on Wed, 16 September 2015 02:26
Thanks Little foot but it runs completly in TOAD and i am getting the all 30000 reocrds in 20 mins in TOAD, i am running the same select statement by putting it into a sql file and running in SQLPLUS

Apologies for late reply but we have to take some approvals to run the trace in Prod server, i am attaching the Trace, SQL Query and the explain plan

Please advise if you see any issues


This trace file showed elapsed time of under 8 minutes for the LARGE query.
please provide details that describe what actually got traced.
Is this supposedly the slow batch run?
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642660 is a reply to message #642659] Wed, 16 September 2015 09:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Wow.
That query has the most evil case/decode statement I've ever clapped eyes on. I'm amazed it runs fast in any circumstances and I guarantee it's not properly understood.
Here's one of them reformatted a bit to make it more readable:
CASE
  WHEN DECODE ((SELECT SUM (ol.ordered_qty)--query 1
                FROM yfs_order_release_status sts
                WHERE sts.order_header_key = oh.order_header_key
                AND sts.order_line_key = ol.order_line_key
                AND sts.status_quantity > 0
               ),
               '0', 
               'Cancelled',
               DECODE ((SELECT SUM (ol.ordered_qty)--query 1
                        FROM yfs_order_release_status sts
                        WHERE sts.order_header_key = oh.order_header_key
                        AND sts.order_line_key = ol.order_line_key
                        AND sts.status_quantity > 0
                       ),
                       (SELECT SUM (sts.status_quantity) --query 1a
                        FROM yfs_order_release_status sts
                        WHERE sts.order_header_key = oh.order_header_key
                        AND sts.order_line_key = ol.order_line_key
                        AND sts.status_quantity > 0
                       ), 
                       (SELECT st.description  --query 2
                        FROM yfs_status st,
                             yfs_pipeline p
                        WHERE st.status = (SELECT MAX (status) --query 3
                                           FROM yfs_order_release_status sts
                                           WHERE sts.order_header_key = oh.order_header_key
                                           AND sts.order_line_key = ol.order_line_key
                                           AND sts.status_quantity > 0)
                        AND p.process_type_key = st.process_type_key
                        AND p.pipeline_key = (SELECT DISTINCT ors.pipeline_key  --query 3a
                                              FROM yfs_order_release_status ors
                                              WHERE ors.order_header_key = oh.order_header_key
                                              AND ors.order_line_key = ol.order_line_key
                                              AND ors.status_quantity > 0
                                             )
                       ),
                       DECODE ((SELECT MIN (status) --query 3b
                                FROM yfs_order_release_status sts
                                WHERE sts.order_header_key = oh.order_header_key
                                AND sts.order_line_key = ol.order_line_key
                                AND sts.status_quantity > 0
                               ),
                               (SELECT MAX (status) --query3
                                FROM yfs_order_release_status sts
                                WHERE sts.order_header_key = oh.order_header_key
                                AND sts.order_line_key = ol.order_line_key
                                AND sts.status_quantity > 0
                               ), 
                               (SELECT st.description --query 2a
                                FROM yfs_status st,
                                     yfs_pipeline p
                                WHERE st.status = (SELECT MIN (status) --query 3
                                                   FROM yfs_order_release_status sts
                                                   WHERE sts.order_header_key = oh.order_header_key
                                                   AND sts.order_line_key = ol.order_line_key
                                                   AND sts.status_quantity > 0
                                                  )
                                AND p.process_type_key = st.process_type_key
                                AND p.pipeline_key = (SELECT DISTINCT ors.pipeline_key --query 3a
                                                      FROM yfs_order_release_status ors
                                                      WHERE ors.order_header_key = oh.order_header_key
                                                      AND ors.order_line_key = ol.order_line_key
                                                      AND ors.status_quantity > 0
                                                     )
                               ),
                               DECODE ((SELECT MIN (status) --query 3b
                                        FROM yfs_order_release_status sts
                                        WHERE sts.order_header_key = oh.order_header_key
                                        AND sts.order_line_key = ol.order_line_key
                                        AND sts.status_quantity > 0
                                       ),
                                       (SELECT MAX (status) --query 4
                                        FROM yfs_order_release_status sts
                                        WHERE sts.order_header_key = oh.order_header_key
                                        AND sts.order_line_key = ol.order_line_key
                                        AND sts.status_quantity > 0
                                        AND sts.status < '9000'
                                       ), 
                                       (SELECT st.description --query 2a
                                        FROM yfs_status st,
                                             yfs_pipeline p
                                        WHERE st.status = (SELECT MIN (status) --query 3
                                                           FROM yfs_order_release_status sts
                                                           WHERE sts.order_header_key = oh.order_header_key
                                                           AND sts.order_line_key = ol.order_line_key
                                                           AND sts.status_quantity > 0
                                                          )
                                        AND p.process_type_key = st.process_type_key
                                        AND p.pipeline_key = (SELECT DISTINCT ors.pipeline_key --query 3a
                                                              FROM yfs_order_release_status ors
                                                              WHERE ors.order_header_key = oh.order_header_key
                                                              AND ors.order_line_key = ol.order_line_key
                                                              AND ors.status_quantity > 0
                                                             )
                                       ),
                                       (SELECT 'Partially ' || st.description --query 2b (same as 2 apart from the ||)
                                        FROM yfs_status st, yfs_pipeline p
                                        WHERE st.status = (SELECT MAX (status) --query 4
                                                           FROM yfs_order_release_status sts
                                                           WHERE sts.order_header_key = oh.order_header_key
                                                           AND sts.order_line_key = ol.order_line_key
                                                           AND sts.status_quantity > 0
                                                           AND sts.status < '9000'
                                                          )
                                        AND p.process_type_key = st.process_type_key
                                        AND p.pipeline_key = (SELECT DISTINCT ors.pipeline_key --query 3a
                                                              FROM yfs_order_release_status ors
                                                              WHERE ors.order_header_key = oh.order_header_key
                                                              AND ors.order_line_key = ol.order_line_key
                                                              AND ors.status_quantity > 0
                                                             )
                                       )
                                      )
                              )
                      )
              ) = 'Cancelled'
  THEN cancelled_date.status_date --cancelled_date.status_date
  ELSE (SELECT MAX (rcpt_status.new_status_date)
        FROM yfs_receipt_status_audit rcpt_status
        WHERE rcpt_status.receipt_header_key = rh.receipt_header_key
        AND rcpt_status.new_status = '1500') --rcpt_status.new_status_date-- rcpt_status.new_status_date
END AS reporting_date_gmt,

I've added comments to inditify all the individual queries contained within. Queries with the same number have the same from and where but different selects.
There should be no need to repeat all those sub-queries. However since I know nothing about your system or what the query is supposed to, and because it's depressingly complicated I'll limit myself to a few obvious suggestions:
1) Query 2b is a complete waste of time. Whatever it returns can't possibly = 'Cancelled' so you should just rip it out and replace with null if necessary.
I say that. However, my last point below makes it a moot point. Leave it in and fix the other code duplication.
2) Bit's like this:
               DECODE ((SELECT SUM (ol.ordered_qty)--query 1
                        FROM yfs_order_release_status sts
                        WHERE sts.order_header_key = oh.order_header_key
                        AND sts.order_line_key = ol.order_line_key
                        AND sts.status_quantity > 0
                       ),
                       (SELECT SUM (sts.status_quantity) --query 1a
                        FROM yfs_order_release_status sts
                        WHERE sts.order_header_key = oh.order_header_key
                        AND sts.order_line_key = ol.order_line_key
                        AND sts.status_quantity > 0
                       )

could be rewritten like this:
               DECODE ((SELECT 1
                        FROM yfs_order_release_status sts
                        WHERE sts.order_header_key = oh.order_header_key
                        AND sts.order_line_key = ol.order_line_key
                        AND sts.status_quantity > 0
                        HAVING SUM (ol.ordered_qty) = SUM (sts.status_quantity)
                       ),
                       1

One pass on the table rather than 2.
There are two other pairs of queries in that that you can apply the same trick to.

There there's the other massive bit of code duplication.
You've got a decode for sib_whs_order_status and CASE statements for reporting_date_gmt and billing_date_gmt.
Both CASE statments are identical. Both CASE statements replicate the full DECODE.
Considering how many aub-queries the decode contains that's frankly madness.
Some nesting will sort that out:
SELECT
sib_whs_order_status,
reporting_date_gmt,
reporting_date_gmt as billing_date_gmt
FROM (SELECT sib_whs_order_status,
             CASE WHEN sib_whs_order_status = 'Cancelled'
                     THEN
                        cancelled_date.status_date --cancelled_date.status_date
                     ELSE
                        (SELECT MAX (rcpt_status.new_status_date)
                           FROM yfs_receipt_status_audit rcpt_status
                          WHERE rcpt_status.receipt_header_key =
                                   rh.receipt_header_key
                                AND rcpt_status.new_status = '1500') as reporting_date_gmt
     FROM (SELECT DECODE(......) as sib_whs_order_status
           FROM .....
          )
    )


Though really if reporting_date_gmt is always the same as billing_date_gmt then probably one of those shouldn't be in the output.

Sort all that out and see where you stand.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642661 is a reply to message #642660] Wed, 16 September 2015 09:34 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Also you appear to be using cursor_sharing=force. Try setting it to exact, you can do that at session level if you don't want to mess with the whole system.
I'm not convinced your WITH clauses are useful either, try removing them and replicating what the do in the JOIN clauses (you pretty much are already).
Finally - are you sure those outer-joins should all be outer-joins?
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642662 is a reply to message #642661] Wed, 16 September 2015 10:23 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's worth pointing out that due to the way decodes short-circuit that query can undergo massive fluctions in the time it takes due to the state of the data.
Say you run the query in toad at a point in time where the very first sub-query in the decode returns 0 for most records. In that case the decode will return a status of Cancelled without running the rest of the aub-queries.
Now suppose the batch job runs later on and the state of the data changes in the meantime.
So that when the batch job runs the decodes end up falling all the way through to query 2b to get the status for most records. The same number of records are returned but now the decode is running 19 sub-queries per record returned instead of 1. And of course you're replicated the decode twice so that's actually 3*19 = 57 sub-queries per record instead of 3.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642689 is a reply to message #642662] Thu, 17 September 2015 06:08 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
Thanks Cookiemaster for your inputs, this query was running fine up untill 3 weeks ago in the Production DB, it is only now showing performance degradation, one more point to update is that during this period we have done quite some data purging from the tables used by this query and ideally thought this would improve the performance, could this any anyway related to indexes going haywire
Do we need to rebuild the indexes or analysing them can also help, does update statatics or anything else help.

We will also analyse on the decode thing you suggested and advise back.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642690 is a reply to message #642689] Thu, 17 September 2015 06:18 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I would seriously fix the decode thing first. If nothing else it'll simply the query and the explain plan and so make it a lot simpler to see what's going on.
You should make sure the statistics are up to date.
Once that is done see what affect ditching the WITH clause has and if it's still slow after that come back to us with the new query and explain plan and we'll be in a better position to see if we need to do anything with indexes.
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642692 is a reply to message #642690] Thu, 17 September 2015 06:26 Go to previous messageGo to next message
saurabh.sha
Messages: 15
Registered: March 2015
Location: india
Junior Member
yes sure we will try that, the only interesting thing is that explain plan for attached query also shows on 18-20 min execution so can really see where the slowness is happening
Re: Query running fine in TOAD and SQL developer but very slow in Server ( oracle DB) [merged 2] [message #642693 is a reply to message #642692] Thu, 17 September 2015 07:04 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That's the problem with the decodes - you've got conditional selects but the explain plan has to include them all because it can't know in advance how many are going to be run. And generally they will all be run for some records in the result set and not others.
Look at a result set - any record with a status that starts with 'Partially ' is one where all the sub-queries were run, all 57. Any record with a status that doesn't start with that didn't run all the sub-queries.
That fact makes it really difficult to tell where the time is being spent.
Previous Topic: Table patition and aautomate sub partition.
Next Topic: Pivot
Goto Forum:
  


Current Time: Tue Apr 23 20:22:14 CDT 2024