A. MySQLsQueryCache_PHP tutorial on MYSQL. E

Source: Internet
Author: User
MYSQL. MySQLsQueryCache. Www. discuz. netviewthread. php? TVariable_name | Value | + ------------------- + --------- + | have_query_cache | YES | query_cache_limit | http://www.discuz.net/viewthread.php?tid=43137&sid=G4jizDNovember 18,200 3 MySQLs Query CacheBy Ian GilfillanA typical scenarioBoss: Our new website is crawler! How can it be, we have four state-of-the-art web servers-whats the problem? You: Well, the web servers are fine-its the database server thats struggling. Boss: What? You told me this MySQL thing was fast, that we didnt need Oracle, and now you say it cant success! How can this be? You: Well, the web servers are behaving so well that theyre pushing through lots of queries, and the database cant manage to process all of them at the same time. its only one database, and lots of web servers... boss: Its too late to buy Oracle now-what are we going to do !? Big Boss to Boss (in the bosss mind): This project has been a disaster from the beginning-now you want me to delay it while we install a new database, and spend a whole lot more! Do you think were made of money !? Im calling in someone who knows what theyre doing-youre history buddy. Colleague (about to take your job): Wait, I think I can solve the problem! So, what does your colleague know that you dont? How can he save the day and let the boss get all the credit? Our scenario is too imprecise to generalize, and there are both possible solutions. you can read about optimizing queries and indexes, optimizing by improving the hardware, and tweaking the MySQL variables, using the slow query log, and of course, there are other methods such as replication. however, MySQL 4 provides one feature that can prove very handy-a query cache. in a situation where the d Atabase has to repeatedly run the same queries on the same data set, returning the same results each time, MySQL can cache the result set, avoiding the overhead of running through the data over and over. usually, you wowould want to implement some sort of caching on the web server, but there are times when this is not possible, and then it is the query cache you will look to for help. setting up Query cacheTo make sure MySQL uses the query cache, there are a few variables you need to set in the configuration file (usually my. cnf or my. ini ). first, is the query_cache_type. there are three possible settings: 0 (for off, do not use), 1 (for on, cache queries) and 2 (on demand, discussed more below ). to ensure it is always on, place: query-cache-type = 1in the configuration file. if you start Ed the server having only made this change, you wocould see the following cache variables set: mysql> show variables like % query_cache %; + dimensions + --------- + | Variable_name | Value | + dimensions + --------- + | have_query_cache | YES | query_cache_limit | 1048576 | query_cache_size | 0 | query_cache_type | ON | + dimensions + ------- + 4 rows in set (0.06 sec) note that these ar E results from MySQL 4.0.x-youll see more in versions 4.1.x and beyond. the query_cache_type will be set to ON or OFF as appropriate. however, there is one more to set, and that is the query_cache_size. if set to 0 (the default), the cache will be disabled. this variable determines the memory, in bytes, used for the query cache. for our purposes, we will set it to 20 MB: query-cache-size = 20MTh E amount is shown in bytes: mysql> show variables like % query_cache %; + Metrics + ---------- + | Variable_name | Value | + Metrics + ---------- + | have_query_cache | YES | query_cache_limit | 1048576 | query_cache_size | 20971520 | query_cache_type | ON | + Metrics + ---------- + 4 rows in set (0.06 sec) the Query cache in action (almost) For this tutorial, I used a dump from W Ikipedia, the open content encyclopedia (you can find the dumps here. I am using a fairly slow machine, with nothing else happening on it, to minimize interference in the results. lets run the same query twice, and see how much improvement we see the second time: SELECT * FROM cur ;... 14144 rows in set (2.96 sec) Now we run the same query again: SELECT * FROM cur; 14144 rows in set (3.02 sec) Now we Run the same query again: SELECT * FROM cur; 14144 rows in set (3.02 sec) What is happening? We wowould perform CT the second query to take noticeably less time. lets examine some of the status variables to get a better picture. mysql> show status like % qcache %; + Metrics + ---------- + | Variable_name | Value | + Metrics + ---------- + | metrics | 0 | Qcache_inserts | 2 | Qcache_hits | 0 | metrics | 0 | Qcache_not_cached | 2 | | Qcache_fr Ee_memory | 20962720 | Qcache_free_blocks | 1 | percent | 1 | + percent + ---------- + 8 rows in set (0.00 sec) The two queries we ran are both recorded (by Qcache_inserts ), but neither of them have been cached. (You may get different results if other queries have been running .) the problem is that the result set is too big. I used the Wikipedia Esperanto dump (4 MB compres Sed-the English dump is 135 MB, and even though my English is better than my Esperanto, bandwidth is expensive in South Africa !), But it is immaterial, as even that is more than the query cache can handle by default. there are two limits in play here-the limit for each inpidual query is determined by the value of query_cache_limit, which is 1 MB by default. moreover, the limit of the cache in total is determined by query_cache_size, which we have seen already. the former limit applies here. if a result set is greater than 1 M, it is not cached. the Query cache in action (really) Lets try a smaller query: SELECT cur_is_new FROM cur WHERE cur_user_text> Y... 2336 rows in set (0.38 sec) Lets see if this one was cached: mysql> show status like % qcache %; + ------------------------- + ---------- + | Variable_name | Value | + ----------------------- + ---------- + | bytes | 1 | Qcache_inserts | 3 | Qcache_hits | 0 | California | 0 | Qcache_not_cached | 2 | Qcache_free_memory | 20947592 | Qcache_free_blocks | 1 | Qcache_total_blocks | 4 | + california + ---------- + 8 rows in set (0.00 sec) there is now a query in the cache. if it took 0.38 seconds to run the first time, lets see if we notice an improvement the second time: SELECT cur_is_new FROM cur WHERE cur_user_text> Y... 2336 rows I N set (0.11 sec) Much better! And, looking at the status again: mysql> show status like % qcache %; + Metrics + ---------- + | Variable_name | Value | + Metrics + ---------- + | metrics | 1 | Qcache_inserts | 3 | Qcache_hits | 1 | metrics | 0 | Qcache_not_cached | 2 | | Qcache_free_memory | 20947592 | Qcache_free_blocks | 1 | Qcache_total_blocks | 4 | + -------------------- ----- + ---------- + 8 rows in set (0.06 sec) The cache has been hit once. the status variables above shoshould be fairly self-explanatory. available memory for the cache has gone from 20962720 to 20947592 bytes. the most useful variable for future tuning is Qcache_lowmem_prunes. each time a cached query is removed from the query cache, (because MySQL needs to make space for another), this value will be I Ncremented. if it increases quickly, and you still have memory to spare, you can up the query_cache_size, while if it never increases, you can reduce the cache size. lets run the query again, with a slight difference, as follows: SELECT cur_is_new from cur where cur_user_text> Y... 2336 rows in set (0.33 sec) That took longer than we wowould have expected. lets look at the status variables to see wha Ts up: mysql> show status like % qcache %; + Metrics + ---------- + | Variable_name | Value | + Metrics + ---------- + | metrics | 2 | Qcache_inserts | 4 | Qcache_hits | 1 | metrics | 0 | Qcache_not_cached | 2 | | Qcache_free_memory | 20932976 | Qcache_free_blocks | 1 | Qcache_total_blocks | 6 | + ----------------------- + ---------- + The query Has not made use of the cache-in fact, MySQL has inserted another query in the cache! The proble

Http://www.bkjia.com/PHPjc/531868.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/531868.htmlTechArticlehttp://www.discuz.net/viewthread.php? Tid = 43137 + ------------------- + --------- + | Variable_name | Value | + ----------------- + --------- + | have_query_cache | YES | query_cache_limit |...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.