Solved

How Can I do Query Caching in MySQL?

Posted on 2008-10-23
7
202 Views
Last Modified: 2013-12-13
I have some select queries that i use frequently. But these queries are important any change in database cache has to rebuild. Anyone have idea.
<?php
$query="select username from users where status='1'";
if(/* is there any changes made after last cache i saved */){
  $result=mysql_query($query);
}else{
  $result=/*last_cache*/;
}
 
//what can i replace /**/ wrote i. i dont know how to be sure is there any changes in tables and i dont know to cache query.
//Help Me:(
 
?>

Open in new window

0
Comment
Question by:phparmy
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:ushastry
ID: 22794332
Two query cache-related options may be specified in SELECT statements:

SQL_CACHE

The query result is cached if it is cacheable and the value of the query_cache_type system variable is ON or DEMAND.

SQL_NO_CACHE

The query result is not cached.

Examples:

select SQL_CACHE username from users where status='1';

select SQL_NO_CACHE username from users where status='1';


Take a look at the manual pages for more details (pls note that you need to change MySQL system parameters in order to work)

http://dev.mysql.com/doc/refman/5.0/en/query-cache.html
0
 

Author Comment

by:phparmy
ID: 22794422
iam dont going to cache my all queries. i want to cache some specific queries. could anyone show me little basic code that caches query.
0
 
LVL 26

Expert Comment

by:ushastry
ID: 22794476
.To catch specific queries you have to pass the hint "SQL_CACHE" before your query.. in order to work properly you MySQL server has to be enabled..

SHOW VARIABLES LIKE 'have_query_cache'; =>>>>> should be " Yes"
SHOW VARIABLES LIKE 'query_cache_type'; ===> should be "2" which is on demand

Let me know if you are not clear about my comments...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:phparmy
ID: 22794520
i understant your commnets. i read about it in mysql site. but how can i user below code i write. could you say how can i write it properly.
<?php
 
$query="select username from users where status='1'";
if(/* is there any changes made after last cache i saved */){
  $result=mysql_query(cache this query = $query);
}else{
  $result=/*last_cache*/;
}
 
while($list=mysql_fetch_assoc($result)){
  echo $list['username'];
} 
 
?>

Open in new window

0
 
LVL 26

Expert Comment

by:ushastry
ID: 22794574
I assume that you have checked MySQL server parameters which I mentioned earlier(query cache type is on demand i.e 2 & have_query_cache=yes)

To monitor query cache performance, use SHOW STATUS to view the cache status variables:

mysql> SHOW STATUS LIKE 'Qcache%';
+-------------------------+--------+
| Variable_name           | Value  |
+-------------------------+--------+
| Qcache_free_blocks      | 36     |
| Qcache_free_memory      | 138488 |
| Qcache_hits             | 79570  |
| Qcache_inserts          | 27087  |
| Qcache_lowmem_prunes    | 3114   |
| Qcache_not_cached       | 22989  |
| Qcache_queries_in_cache | 415    |
| Qcache_total_blocks     | 912    |
+-------------------------+--------+

SQL-CACHE.TXT
0
 
LVL 26

Accepted Solution

by:
ushastry earned 500 total points
ID: 22794577
You code should look like this..

<?php
 
# Pass the SQL_CACHE hint to MySQL server

$query="select SQL_CACHE username from users where status='1'";
$result=mysql_query($query);
 
while($list=mysql_fetch_assoc($result)){
  echo $list['username'];
}
 
?>
0
 

Author Comment

by:phparmy
ID: 22795013
Thanks i understand. Do you know how set interval that mysql caches query after 2 hours previous one it cahces. Like below.
<?php
 
$query="select username from users where status='1'";
if(/* if two hours passed, query previous one i cached cache again */){
  $result=mysql_query(cache this query = $query);
}else{
  $result=/*last_cache*/;
}
 
while($list=mysql_fetch_assoc($result)){
  echo $list['username'];
} 
 
?>

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question