Your explaination/lack of code example is rather vague. It sounds as if you are probably pulling a very large query into an array and/or looping over it in a
loop. If the data set is really large you are going to run out of memory. Therefore you may need to refine your query to a stricter criteria and then run that multiple times so you can digest it in smaller chunks.