Avatar billede fredand Forsker
28. januar 2008 - 14:27 Der er 8 kommentarer og
1 løsning

How is this qury executed

Hello!
I just got an job/issue on my desk.
"Why does this query take so long to execute?"

To give you the complete info, we got at database Oracle 9i with a table a_tbl with over 10 000 000 rows.
Columns in a_tbl looks for eg: (id, name, mark)

We got a cronjob that updates some of these rows each night and insert a int in the column "mark".

The sql to fetch these rows looks like

select name from a_tbl
where name not in
  (
    "very complex select sql-query"
  )

If I just execute the "very complex select sql-query" it returns after 2 minutes.

But if executed within the the total query it doesn't return until after 5 hours, acctually it has not returned yet.

So my question is if Oracle, or the total question is executed like

select name from a_tbl
  for eache row, execute "very complex select sql-query"

or does it:
execute "very complex select sql-query" only once
and with this list above only select those "name" that is not in that list.

My guess is that it executes the "very complex select sql-query"
for each name.

Any comment is very welcome
Best regards
Fredrik
Avatar billede nva Praktikant
28. januar 2008 - 14:53 #1
Fandt dette:

Avoid NOT


Predicates formed using NOT are Stage 1
But they are not indexable
For Subquery - when using negation logic:
Use NOT Exists
DB2 tests non-existence
Instead of NOT IN
DB2 must materialize the complete result set

her www.oti.fsu.edu/dba/2003_Database_Training/DB2_SQL/Module6Tuning.ppt+sql+select+subselect+optimize+improve+performance&hl=da&ct=clnk&cd=6&gl=dk" target="_blank">http://66.102.9.104/search?q=cache:NMtOrLLQza0J:www.oti.fsu.edu/dba/2003_Database_Training/DB2_SQL/Module6Tuning.ppt+sql+select+subselect+optimize+improve+performance&hl=da&ct=clnk&cd=6&gl=dk
Avatar billede coderdk Praktikant
28. januar 2008 - 14:58 #2
Try to execute:

explain plan for
select name from a_tbl
where name not in
  (
    "very complex select sql-query"
  );


followed by:


@$ORACLE_HOME/rdbms/admin/utlxpls.sql


That should give you some idea of Oracle's behaviour...
Avatar billede nva Praktikant
28. januar 2008 - 14:58 #3
Avoid NOT IN with SubSelects
Using sub-selects and NOT IN is poorly optimized. Converting to nested queries or OUTER JOINs are more efficient. The following example finds customers without orders:

Less efficient:
      SELECT Customers.*
      FROM Customers
      WHERE Customers.[Customer ID]
            NOT IN (SELECT [Customer ID] FROM Orders);
               

More efficient:
      SELECT Customers.*
      FROM Customers LEFT JOIN Orders
          ON Customers.[Customer ID] = Orders.[Customer ID]
      WHERE ((Orders.[Customer ID] Is Null));
Avatar billede arne_v Ekspert
28. januar 2008 - 16:46 #4
Whether it is possible to only execute the huge subquery once depends on whether
it is constant (does not depend on values in the outer data).

If it is not constant it has do execute it N times.

If it is constant then potentially the query optimizer can only execute it once.

Whether Oracle actually do it or not is another question.

If it is constant but Oracle does not use it, then I can see two possible
workarounds:

1)  a rewrite to not use subquery like nva's example above
2)  stuff the huge subquery into a temporary table and have a simple subquery using that
Avatar billede nva Praktikant
01. februar 2008 - 08:56 #5
Any luck?
Avatar billede fredand Forsker
03. februar 2008 - 13:44 #6
Hello Mates!

I will try to get a chance to test your suggetstions in the begining of next week.
Right now I'm participating in 3 different projects unfortenately. But If everything goes my way I will have 4 hours tommorow afternoon.
I get back to you as quick as I can.


BTW I spoke to a DBA in this project and he suggested to create a couple of extra index. That would speed up the "very complex select sql-query" according to him. To me it sounds better to fetch all rows first for the prestanda. But I guess he is right if I need to be sure that the "very complex select sql-query" get evaluated  for each row.

I will get back as soon as I can!

Best regards
Fredrik
Avatar billede fredand Forsker
20. februar 2008 - 12:55 #7
Hello guys!

It looks like it works fine with a couple of temp tables.

Please all of you give a svar so I can reward you mates!

Best regards
Fredrik
Avatar billede arne_v Ekspert
20. februar 2008 - 14:08 #8
.
Avatar billede fredand Forsker
28. februar 2008 - 21:20 #9
Thanks alot!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester