Is MySQL query Caching really good for performance optimization ?

MySQL query cache is one of the option on MySQL server optimization.  But question here is, is it really good to do MySQL query Caching?

To understand this we have to know what really happens during MySQL query caching.

Example:

Select * from table ;

Untitled-1

Above is the result of running a simple query on 2M rows, with query cache enabled. Here we see each time query is executed, Query cache is checked for existing result. If result not found, new cache is written to the query cache.  Which seems good but imaging the different scenario.

1. Each time there is modification in the table like update, edit on column e.t.c  query that is cached is invalid, so whole process of reading the query and writing to query has to perform again.

2. When query cache is written, its locks the cache of that query, so if one query is changing anything to the cache, no other query can not access it unless the lock is released, which results more response time. If there is higher frequency of updates on the query there will be more response time.

3. If you have millions of combination of query that can be generated in your database, caching query is not an option, as caching is only for few sec and holding millions of cached query to the memory, is not a possible options.

So, this summarized that query cache is not always good so when we use it ?

1. if we have same query is written in same piece of code very frequently,  then there is a less chance table is updated with in that milli- second, in that case we can use query caching.  Many framework related development of highly oops based development we are using same query many time in the code for example:

$result = $dataModel->find->id(‘5’);

// php code

if($dataModel->RelationalModel->status == 2)  {

// php code

}

In above example  dataModel is called twice, so same query will be executed in same code twice ( if object is not cached) , in that case query caching can be effective. The code is just a basic idea, its not a real code or working code of any kind.

So what we do?

1. If we have more read and less writes to the tables then query cache  can be efficient. Sites like forums, blogs, directory, news sites e.t.c has very less update frequency so query cache will be effective.

2. Limiting query cache size can be good option like few mega byte or few hundred mega byte relative to huge data size will limit the frequency of query caching.

3. if possible, disable query cache and test over all effect on performance can be good idea.

MySQL optimization depends on the database usage frequency and database data size, so testing different scenario with benchmark will be the best option. There is no hard written rule on optimization. But every time we optimize  MySQL server just enabling the query cache is not always the best option.

Fixing 500 internal server error

“500 inernal server error’ can be due to various server and code related issue. To fix ‘500 internal server error’ we have to look into various aspect in the server and code.

1. File permissions / File size issue: 

If server cant read / write / or execute different files or even directories, it throws 500 internal server errors. This can be writing to the file generated by php like log files, image files, or creating directories from php. Same error problem also be created when server services like httpd ( apache), mysql et.c cant save / read / or write logs files associated with it.  To solve the problem, first check the server side ,  so need to see ‘messages’ log file which is generally , inside /var/ log/messages every error / status / alert created by linux system will be copied to this file so just reading it and finding the error and fixing the problem will solve this issue.

2. Tmp folder might be full:

As we know every linux server has tmp folder, which can be just a folder within a drive or new mount to the server.  If this folder is full that will also cause 500 internal server error.

its always better to run tempwatch script in the server to manage the size of the tmp folder, it can be run as a cron job in a server which automatically clears the tmp folder on periodic basics.

3.  .htaccess syntax issue or misconfigured.

if there is any mistake on .htaccess syntax error or if some module related condition or module mention in that htaccess that is not loaded in the server then we will get this error. Best way to solve this problem is to  comment out the htaccess line that you have recently added and reloading the page to see if that is the problem.

4. Mod security or similar security proxies in the server.

Some of the rules in the mod security is set to give out 500 internal server error  in case of match with criteria. So checking the mod security log file can also give the idea if there is any problem there.  If server has other proxy security installed, we need to check it there too.

5. Mis-configuration of apache / nginx/ php

Any web server like apache / nginx  e.tc misconfiguration can also cause the 500 internal server error.  Commonly, many times we are loading the module on web server that is not installed, or any module needs certain access to the files or folder can also cause this problem. Similarly misconfiguration of php.ini also cause this problem. To solve this problem log files of webserver and error log of php must be checked to get to the root of the problem.

6. php su_exec folder permission:

Many of us now, running php with su_exec for better security option. But this only allows folder to have 755 permission and if you have 777 permission to the folders then we will get 500 internal server error.

7. Bad email gateway:

if your code is trying to send an email and you get 500 internal server error , that means your email protocol is not set properly, so a quick look on this is worth trying.

I suggest to clear the browser cache, and if the server have internal cache like varnish, file cache e.t.c please clear those too and refresh after the problem is fixed, else even the problem is fixed we will get the same response as its being served by cache.

8. PHP and Apache settings:

Make sure to check few php config parameters:
a) upload_max_filesize : If you trying to upload and get 500 error make sure your file size is less than upload_max_filesize value in php.ini, if not change it to required value.

b) max_execution_time: If you script requires longer time to execute like importing xml data, computing large data set e.t.c you can try by increasing these value. If you have set your php as mod_fcgi then you have change two parameters in httpd.conf file or php.fcgi file.

FcgidMaxRequestLen 10737418240
FcgidIOTimeout 3600