Mysql – Tuning (and understanding) table_cache in theSQL

MySQLperformance-tuning

I ran the excellent MySQL performance tuning script and started to work through the suggestions. One I ran into was

TABLE CACHE
Current table_cache value = 4096 tables You have a total
of 1073 tables. You have 3900 open tables. Current table_cache hit
rate is 2%, while 95% of your table cache is in use. You should
probably increase your table_cache

I started to read up on the table_cache but found the MySQL documentation quite lacking. They do say to increase the table_cache, "if you have the memory". Unfortunately the table_cache variable is defined as "The number of open tables for all threads."

How will the memory used by MySQL change, if I increase this variable? What is a good value, to set it to?

Best Answer

From the MySQL documentation

For example, for 200 concurrent running connections, you should have a table cache size of at least 200 × N, where N is the maximum number of tables per join in any of the queries which you execute. You must also reserve some extra file descriptors for temporary tables and files.

So if in your application you have a query that joins 4 tables and you want to be able to handle 200 concurrent connections, based on that statement you should have table_cache of at least 800.

As far as memory usage, I don't have those numbers, I would suspect it will depend upon the size of your tables that it is caching.