views:

75

answers:

4

I want to add some static information associated with string keys to all of my pages. The individual PHP pages use some of that information filtered by a query string. Which is the better approach to add this information? Generate a 100K (or larger if more info is needed later) PHP file with an associated array or add an other DB table with this info and query that?

The first solution involves loading the 100K file every time even if I use only some of the information on the current page. The second on the other hand adds an extra database call to the rendering of every page.

Which is the less costly if there are a large number of pages? Loading a PHP file or making an extra db call?

+2  A: 

Unless it is shown to really be a bottleneck (be it including the php file or querying the database), you should choose the option that is best maintainable.

My guess is that it is the second option. Store it in a database.

Dennis Haarbrink
A: 

Storing it in a database is a much better plan. With the database you can provide better data constraints, more easily cross reference with other data and create strong relationships. You may or may not need that at this time, but it's a much more flexible solution in the end.

What is the data used for? I'm wondering if the data you need could be stored in a session variable/cookie once it is pulled from the database which would allow you to not query the db on the rendering of every page.

RC
A: 

If you were to leverage a PHP file then utilizing APC or some other opcode cache will mitigate performance concerns as your PHP files will only be loaded each time the file changes.

However, as others have noted, a database is the best place to store this stuff as it is much easier to maintain (this should be your priority to begin with).

Having ensured ease of maintenance and a working application, should you require a performance boost then generally accepted practice would be to cache this static data in an in-memory key/value store such as memcached. This will give you rapid access to your static values (for most requests).

rojoca
A: 

I wouldn't call this information "static".
To me, it's just a routine call to get dome information from the database, among other calls being made to assemble whole page. What I am missing?

And I do agree with Dennis, all optimizations should be based on real needs and profiling. Otherwise it's effect could be opposite.

If you want to utilize some caching, consider to implement Conditional GET for the whole page.

Col. Shrapnel