Solved

How Can I do Query Caching in MySQL?

Posted on 2008-10-23
7
199 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now