I’ve got an object, say a person. I’ve also have a class called personList. Which is an array of persons.

When I initiate the list it calls a database query and builds an array of objects (person).

Elsewhere in other objects I need to reference a particular person.

What I currently do is declare my personList and call a method called getID which returns the person with the ID passed in by looping through and matching it with the property of the person object in the full array.

It’s works well. But my question is.

My person database is now over 5,000 rows long.

Is initiating this list and building an array of over 5,000 bad in terms of performance? Or is this something easily managed by MySQL

should my getID be doing a database lookup and building the person as a one off person?

The couple things that pop to my mind:

  • Storing 5000+ person objects in an array is going to eat up a significant amount of memory (for an undefined value of "significant").
  • "Looping through and matching it with the property of the person object in the full array" may actually be less efficient than doing a DB query, assuming the applicable column(s) is indexed.

    If you're only working with individual persons then it makes sense to only retrieve those persons you're interested in.

    If you've got a need to deal with the entire table's worth of persons, and you can't do whatever it is in the database, then fetch them all into an array but use the persons' IDs as array keys if you should need to identify them individually.

      Is there a way of working out how much memory is being used up?

        NZ_Kiwis Is 2.351264MB much to worry about?

        You could check memory_limit to see what your configured max is, as a starter:

        echo ini_get('memory_limit');
        

        (The default on my work MacBook Pro is 128M, FWIW.)

          nzkiwi80 My person database is now over 5,000 rows long.

          Imagine it was 3M rows long. When asking if your process is efficient for N users, ask yourself if it would work for very large values of N. If the answer is that it would break down for large N, then do it a different way.

          If you are performing a single-user operation, you should be fetching a single record. As a rule of thumb, you should avoid fetching data you don't need and you should avoid looping through large amounts of data if you just want to make one small change. It's often easy to just grab everything from the DB and do what you want in PHP, and it generally takes more effort to cook up the extra logic and SQL queries to make your code more efficient, but I think one should always take the time to do so.

            Write a Reply...