I have this tables:
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:
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:
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.