简体   繁体   中英

PHP MySQL: Select from same table multiple times without database load for each query?

I'll try to describe my situation and problem first, and ask the question then.

Description of situation and problem

So I am using a phpBB forum and programming for it. I want to code something on my own where I need to run multiple select queries on the database to check several things. Some of them are a bit more compicated and there will be more than a hundred statements, so I don't want to slow down my site. I want to check these selects asynchronous via Ajax in the background and store the results in a cache table ready for retrieval. That's not the big problem.

What I think about is the database load cause of all this queries. If 100 users are connected and regularly have over 100 queries running, that's very huge. I don't know the impact on the performance, but I don't want to chance it. Many of these queries are just SELECTs from the same table with different WHERE clauses. So I thought about if it is possible to just select the whole table once without any parameters and every select query on this table will just access a cached version of this table. This would also lead to a more consinstent result, cause all the selects use the same values. I know it is possible to store the query in an two-dimensional php-array, but I will lose all the cool MySQL functions and clauses I need, it will be much more work.

So, much text, hope you read it ._. Now my question:

Question

Is is possible to cache a MySQL table once and use this cache for each following query in the same php document call? Would it save performance?



EDIT://
I think i should explain it a bit more, so there are no misunderstandings what I want to do. For simplify matters lets say I want to make a similar thing like the badges here on stackoverflow. So if the users load the site, I need to check if he got some new badges, if the criterias are fulfilled, etc. And I often use the same table for this checks. This is the question why I wanted to ask if caching the table is possible.

Why I check at the siteload, and not if the user performs the action he will get the badge for? There are two reasons: First many badges are earned after a period of time or if other users perform actions, not yourself. Also it is very confusing and complicated and also hard to manage to add >100 queries in all the files of phpBB in places where they are needed. I think THIS would be bad design. So I wanted to group my code and capsulate it.

I expect you are using MySQL as Database system.

You can use in your Select with SQL_CACHE and the rest will work automatically. ( http://dev.mysql.com/doc/refman/5.1/en/query-cache-in-select.html )

The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.

The query cache can be useful in an environment where you have tables that do not change very often and for which the server receives many identical queries. This is a typical situation for many Web servers that generate many dynamic pages based on database content.

If your table data is not changing very frequently then you can cache your query results and for it you can use various technologies. For example you can use solr, which will cached your 100 queries results for a specific period of time like 10 minute or 1 hour as per your requirement and after a specific time you need to again cache your data.

By this approach suppose you have cached your data for 1 hour then 100 queries will execute 1 time in every hour and rest time query results will be fetched from cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM