views:

197

answers:

9

I am connecting oracle db thru java program. The problem is i am getting Outofmemeory exception because the sql is returning 3 million records. I cannot increase the JVM heapsize for some reason.

What is the best solution to solve this?

Is the only option is to run the sql with LIMIT?

+2  A: 

In my opinion is pointless to have queries that return 3 million records. What would you do with them? There is no meaning to present them to the user and if you want to do some calculations it is better to run more than one queries that return considerably fewer records.

Using LIMIT is one solution, but a better solution would be to restructure your database and application so that you can have "smarter" queries that do not return everything in one go. For example you could return records based on a date column. This way you could have the most recent ones.

kgiannakakis
It may not be utterly pointless to return 3 million rec. App may pull such records for some reporting and analysis purpose.
Ravi Gupta
If you do need 3 million records, process them in smaller chunks.
kgiannakakis
yes, thats one way. I think if user really needs 3 million records, increasing heap size is something he cannot avoid for long.
Ravi Gupta
+3  A: 

If your program needs to return 3 mil records at once, you're doing something wrong. What do you need to do that requires processing 3 mil records at once?

You can either split the query into smaller ones using LIMIT, or rethink what you need to do to reduce the amount of data you need to process.

futureelite7
A: 

Try this:

http://w3schools.com/sql/sql_where.asp

ck
A: 

There could be three possible solutions 1. If retreiving 3million records at once is not necessary.. Use LIMIT

  1. Consider using meaningful where clause

  2. Export database entries into txt or csv or excel format with the tool that oracle provides and use that file for your use..

Cheers :-)

Richie
A: 

reconsider your where clause. see if you can make it more restrictive. and/or use limit

Aadith
+1  A: 

Application scaling is always an issue. The solution here will to do whatever you are trying to do in Java as a stored procedure in Oracle PL/SQL. Let oracle process the data and use internal query planners to limit amount of data flowing in an out and possibly causing major latencies.

You can even write the stored procedure in Java.

Second solution will be to indeed make a limited query and process from several java nodes and collate results. Look up map-reduce.

whatnick
A: 

Just for reference, In Oracle queries, LIMIT is ROWNUM

Eg., ... WHERE ROWNUM<=1000

S.Mark
+1  A: 

If each record is around 1 kilobyte that means 3gb of data, do you have that amount of memory available for your application?

Should be better if you explain the "real" problem, since OutOfMemory is not your actual problem.

medopal
A: 

If you get that large a response then take care to process the result set row by row so the full result does not need to be in memory. If you do that properly you can process enormous data sets without problems.

Thorbjørn Ravn Andersen