Avatar billede fredand Forsker
19. august 2008 - 09:35 Der er 6 kommentarer og
2 løsninger

Need nice strategy for this search.

Hello!

We got a task where we are supposed to optimize a webapplication written in Java.

The application got a huge database, oracle, for eg 10 000 000 rows.
The table looks like (simplified)
id  long
name varchar50
desc varchar250

One usecase that now got bad performance is the search and searchdisplay. My guess is that the strategy looks bad.

Now it works like this:

1) Enter search criteria.
For eg name="screwdriver", desc~"*color of handel is *"

2) Execute SQL (through hibernate)

3) Get the search result, for eg 10 000 rows.

4) Display the search result, The result is devided into 50 rows on each page. The complete list of rows is looped through by java-code and the proper intervall 50 rows is fetched.

5) If user choose to browse the search result and enter page, for eg, 10 (rows 500-550) then the complete SQL is executed again. And the proper intervall of 50 rows is fetched this time.

Hope you get it.

To me it looks like this strategy is bad since the complete search result is fetched over and over again. One way of solving this might be store the search result in the session. But since the session also seems to be using a lot of RAM or disk through swapping, if there is a lot of users, perhaps this idea is bad as well.

My hope is that there is a way to execute the sql in a better way so it only fetch 50 rows each time in a efficient way.

Any comments are welcome!

Best regards
Fredrik
Avatar billede arne_v Ekspert
19. august 2008 - 19:02 #1
The problem is well known.

You can go two directions for solutions:

1) database specific SQL that grabs only the rows (the page of rows) you want

  in Oracle that means using ROWNUM or ROW_NUMBER (the correct way !)

2) cache data

  two options for where

2a) cache in HTTP session

2b) cache in stateful session bean and just store ref to that in HTTP session

Check out http://www.corej2eepatterns.com/Patterns2ndEd/ValueListHandler.htm
for some inspiration (the book has actual code).
Avatar billede simonvalter Praktikant
19. august 2008 - 19:10 #2
Avatar billede arne_v Ekspert
19. august 2008 - 19:16 #3
It should be noted that any approach that uses multiple SQL statements may return
inconsistent results if the database is updated in between.
Avatar billede arne_v Ekspert
19. august 2008 - 19:24 #4
Based on info in one of the Hibernate links, then it should be obvious that there
is a third options:

2c) cache in the database server (by keeping connection and cursor open)

That is not a good solution. It will scale horrible because the app will run
out of database connections and it will perform horrible because unless run with
a very low transaction isolation level it will lock the database up.

I assume that the Hibernate:

q.setMaxResults
q.setFirstResult

solution is a smart #1 where Hibernate take care of the database specific SQL and
not a #2c.
Avatar billede arne_v Ekspert
28. oktober 2008 - 02:57 #5
fredand ?
Avatar billede fredand Forsker
06. december 2008 - 17:15 #6
Hello!

We had had a long dissusion around this issue.

The solution we went for was to store the id's in the session and let the web-framwork show only the correct intervall.

Sorry to be late on this answer but I encourage you guys to give a svar so I can reward you both.
Your opinions was most welcome.

Best regards
Fredrik
Avatar billede arne_v Ekspert
06. december 2008 - 17:30 #7
.
Avatar billede simonvalter Praktikant
06. december 2008 - 18:33 #8
:)
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