简体   繁体   中英

Oracle - SQL query takes too much time

I have this tables:

  • automa
  • accoun
  • client
  • profil
  • market

The biggest one is accoun, with almost 30 million rows. There are like 20 thousand rows in client, and 30 thousand rows in automa.

I need to get every row in automa, where:

  • mktcode matches mktcode from market
  • acccode matches acccode from accoun
  • ccccode from accoun matches ccccode from client
  • clicode from client matches clicode from profil
  • procode from profil is 'ADMIN'

This is my query:

SELECT au.acccode, au.oprcode, au.ctrcode, au.ctrdesc, au.mkcode
FROM automa au
JOIN profil pr ON pr.procode = 'ADMIN'
JOIN client cl ON cl.clicode = pr.clicode
JOIN market mk ON mk.mktcode = au.mkcode
JOIN accoun ac ON ac.ccccode = cl.ccccode AND ac.acccode = au.acccode
GROUP BY au.acccode, au.oprcode, au.ctrcode, au.ctrdesc, au.mkcode

It takes like 60 seconds.

I have 2 extra problems:

  • I cannot see the execution plan
  • I cannot create indexes. If it's the only way, I could ask for them to be created, but that would take some time to be done.

Any ideas? I cannot figure out how to solve it without indexes.

You have no join condition between automa and profil . If you try

SELECT au.acccode, au.oprcode, au.ctrcode, au.ctrdesc, au.mkcode
  FROM automa au
  JOIN profil pr ON pr.procode = 'ADMIN'

this query returns ALL rows from automa, without any filtering. Firstly, add join condition.

You can also try to rewrite this query. Because you need data only from automa , try to use EXISTS. May be after that you will not need in grouping/distinct. Something like that:

SELECT au.acccode, au.oprcode, au.ctrcode, au.ctrdesc, au.mkcode
  FROM automa au
 where exists (select null 
                from profil pr 
                JOIN client cl ON cl.clicode = pr.clicode and pr.procode = 'ADMIN'
                JOIN accoun ac ON ac.ccccode = cl.ccccode 
                              AND ac.acccode = au.acccode
                JOIN market mk ON mk.mktcode = au.mkcode
               )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM