28. februar 2008 - 21:43Der er
7 kommentarer og 1 løsning
Tricky hql-question
Hello!
This is a similar problem that you guys helped me out with a couple of weeks ago.
I have a task to increase performance of a HQL-question. I guess we could solve it with SQL if it is easier, but the previous developer has used Hibernate. The purpose for this HQL is to fetch rows from a table.
The background of the problem is that we have 2 tables like:
Table A contains id:s, names and a flag for deletion. tbl_a id, name, flag 1 a 0 2 b 1 3 c 0 4 d 1 5 e 0 6 f 1 7 g 0 8 h 1
Table B contains references to Table A and a counter. tbl_b id, ref_id, counter 1 1 10 2 2 0 3 3 10 4 4 5
We are now supposed to fetch those id's from table A that should be deleted. The condition for fetching for deletion is: flag = 1 and ( (id = ref_id and counter = 0) or (id not exist as ref_id in tbl_b) )
Then the HQL looks like.
from tbl_a where tbl_a.flag =:Bool and tbl_a.id not in (select distinct ref_id from tbl_b where ( counter > 0 ) )
The HQL above seems to fulfill the condition. To me these rows would be fetched from tbl_a:
2 b 1 6 f 1 8 h 1
But the problem seems to be "not in", which propably performs a full table scan. And with tables with like 10 000 000 rows, this takes a lot of time.
So in some how I need to figure out a better way to express the HQL, or if needed in SQL.
I would really appreciate some comments of how you would solve this.
I dette særtema ser vi på, hvordan cloud og AI bliver fundamentet for virksomhedernes digitale forretning, og hvordan de nye muligheder for automatisering og forretningsværdi kan udnyttes uden at miste overblik, sikkerhed og menneskelig kontrol.
Hello! No the counter column does not have an index. I propably do not know enough about index but since you ask about it I bet it would help. But in what way? Best regards Fredrik
And index on a database column works just like an index in a book.
If you need to find info about ArrayList in a Java book without an index you have to read the entire book.
If you need to find info about ArrayList in a Java book with an index , then you look in the index and see that ArrayList is described at page 85-88 and 123.
You could also attack it a bit more scientificly: * configure Hibernate to output the SQL it is actually using * examine that and possible run the databases explain/query analyzer on it
A DBA took a look at the problem. He went for the analyzing-way. He examined the question and some more, and he used som verbose in the oracle grid to look at the sql-executed. His solution was to add a couple of index.
Voila!
Problems seems to be gone. I think you should give a svar Arne so I can give you the points!
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.