Search code examples
mysqlvariablesmemory-managementglobal-variablesapc

Keeping mysql query results and variables set from the queries for use across pages and sessions


I have never used apc_store() before, and I'm also not sure about whether to free query results or not. So I have these questions...

In a MySQL Query Cache article here, it says "The MySQL query cache is a global one shared among the sessions. It caches the select query along with the result set, which enables the identical selects to execute faster as the data fetches from the in memory."

Does using free_result() after a select query negate the caching spoken of above?

Also, if I want to set variables and arrays obtained from the select query for use across pages, should I save the variables in memory via apc_store() for example? (I know that can save arrays too.) And if I do that, does it matter if I free the result of the query? Right now, I am setting these variables and arrays in an included file on most pages, since they are used often. This doesn't seem very efficient, which is why I'm looking for an alternative.

Thanks for any help/advice on the most efficient way to do the above.


Solution

  • MySQL's "Query cache" is internal to MySQL. You still have to perform the SELECT; the result may come back faster if the QC is enabled and usable in the situation.

    I don't think the QC is what you are looking for.

    The QC is going away in newer versions. Do not plan to use it.

    In PHP, consider $_SESSION. I don't know whether it is better than apc_store for your use.

    Note also, anything that is directly available in PHP constrains you to a single webserver. (This is fine for small to medium apps, but is not viable for very active apps.)

    For scaling, consider storing a small key in a cookie, then looking up that key in a table in the database. This provides for storing arbitrary amounts of data in the database with only a few milliseconds of overhead. The "key" might be something as simple as a "user id" or "session number" or "cart number", etc.