Performance: storing a large mysql result in an array of VS vars class results

People hej,

I was wondering what is faster. Suggest that we have a large database table with approximately 50 columns and a class that processes these columns.

The class constructor loads all the fields - and here my question begins.

Is it useful to store each column in its own class variable or is it juts unperformant? In this case, I already have an array, for example. $ result, which are the columns of the table.

Or is it inappropriate?

I tried to write a benchmark, but I only have vServer for testing, so the results are not very clear.

Is a one-time function that stores values ​​in vars classes faster than searching the entire array for each get method?

Thank you in advance:)

PS PHP 5

+4
source share
1 answer

I think the answer to your question is "it doesn't really matter."

In terms of application performance, the code you are talking about - iterating through a loop in memory 50 times and manipulating data in memory, retrieving class data, or retrieving data from an associative array - is incredibly fast. So fast that it is almost impossible to measure differences under normal circumstances. To notice the difference between employee::$id, employee::$name vs employee::$result['id'], employee::$result['name'] , you have to deal with extreme circumstances - an array of $ result from million columns, for example. See this criterion.

On the other hand, from a design point of view, there are some tradeoffs - by matching the result with class variables, you can hide the business logic and create a consistent interface. For example, if your employee class should calculate year_salary based on the month_salary database field, you can create another class variable called "year_salary" and the client code is always consistent - it is employee::annual_salary and employee::monthly_salary , not employee::result['monthly_salary'] and employee::annual_salary .

+3
source

All Articles