Avatar billede fredand Forsker
28. februar 2008 - 21:43 Der 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.

Best regards
Fredrik
Avatar billede arne_v Ekspert
28. februar 2008 - 22:27 #1
Does counter column have an index ?
Avatar billede fredand Forsker
29. februar 2008 - 08:20 #2
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
Avatar billede arne_v Ekspert
29. februar 2008 - 14:47 #3
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.
Avatar billede arne_v Ekspert
29. februar 2008 - 14:48 #4
It will obviously only help if counter>0 only returns a small part of all records.
Avatar billede arne_v Ekspert
29. februar 2008 - 14:49 #5
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
Avatar billede arne_v Ekspert
28. oktober 2008 - 02:58 #6
Any progress ?
Avatar billede fredand Forsker
02. marts 2009 - 20:41 #7
Hello!

(Sorry for late answer!)

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!

Mange tak!
Fredrik
Avatar billede arne_v Ekspert
02. marts 2009 - 20:48 #8
OK
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
Kurser inden for grundlæggende programmering

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

IT-JOB

Netcompany A/S

IT Manager

Metroselskabet og Hovedstadens Letbane

Senior OT Cybersecurity Advisor til Københavns Metro

Capgemini Danmark A/S

Open Application (Denmark)

Forsvarsministeriets Materiel- og Indkøbsstyrelse

Strategiske planlæggere til program Digital og Operativ Transformation