Applying iterative algorithm to a set of rows from database

Posted by Corvin on Stack Overflow See other posts from Stack Overflow or by Corvin
Published on 2010-05-17T18:00:35Z Indexed on 2010/05/17 18:10 UTC
Read the original article Hit count: 151

Filed under:
|

Hello,

this question may seem too basic to some, but please bear with be, it's been a while since I dealt with decent database programming.

I have an algorithm that I need to program in PHP/MySQL to work on a website. It performs some computations iteratively on an array of objects (it ranks the objects based on their properties). In each iteration the algorithm runs through all collection a couple of times, accessing various data from different places of the whole collection. The algorithm needs several hundred iterations to complete. The array comes from a database.

The straightforward solution that I see is to take the results of a database query and create an object for each row of the query, put the objects to an array and pass the array to my algorithm.

However, I'm concerned with efficacy of such solution when I have to work with an array of several thousand of items because what I do is essentially mirror the results of a query to memory.

On the other hand, making database query a couple of times on each iteration of the algorithm also seems wrong.

So, my question is - what is the correct architectural solution for a problem like this? Is it OK to mirror the query results to memory? If not, which is the best way to work with query results in such an algorithm?

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql