Thu 15 Sep 2005
Real life example:
SELECT username FROM users WHERE (ADDDATE( last_in_chat, INTERVAL 2 MINUTE) > NOW()) ORDER BY username LIMIT 30
NEVER do that. The user in question runs this query on table with 3000+ rows, which means, that SQL server (MySQL in
this case) is doing 3000+ date/time additions and will never use any index. If you write the query this way:
SELECT username FROM users WHERE last_in_chat> NOW()-interval 2 minute ORDER BY username LIMIT 30
the server will do only one date/time substraction and will probably use index if there is such on last_in_chat. The second query runs much faster, even without index. In this case not having index on last_in_chat maybe is a better option as this field probably updates on user actions in chat room, which may happen more ofthen than the displaing of this list.
Mind the Query cache.
MySQL have a nice feature - query cache, which stores query results and if you rerun your query, you will have your result served from the cache, which is quite faster. The cache expires at the first update/insert/delete query on the table (or any of the tables in case of join), so you will not get outdated results. The downside is that when you use functions like NOW(), CURRENT_DATE(), UNIXTIME() and so on, the server will not put your query in the cache, as next time the query will be executed this functions may return different value.
You can rewrite the query from the example above to use the query cache:
SELECT username FROM users WHERE last_in_chat>'2005-09-15 16:44:00' ORDER BY username LIMIT 30
where “2005-09-15 16:44:00″ is computed in the programming language, from which you run query like this (PHP):
$time=date("Y-m-d H:i:00", time()-120);
As you see, the seconds are hardcoded to be 00. This sacrifices correctness of the query and now you will see users which was last in chat 2 to 3 minutes. You shoud decide if you care about this. In this case this may be not a good option, but if you want users, that was active in last 15 minutes, you will most probably will not care about 1 minute.
so when you run this query, it will be stored in the cache and the cached result will be used during this minute as all the queries will be the same and will not have one of “bad” functions in it. On the next minute, query will be:
SELECT username FROM users WHERE last_in_chat>'2005-09-15 16:45:00' ORDER BY username LIMIT 30
and this will generate new results.
Of course you shoud use this trick only if you think that this query will be run more then once per minute, but if not you maybe don’t need any performance tips.
This trick is better illustrated when you substitute CURRENT_DATE() with current date, generated from your program as this changes quite less often than NOW() (once in 24 hours actually) and you can have a query served from the cache for a whole day.
November 11th, 2010 at 9:15 am
… track backe bei http://www.friendsy.org/account/submit/add-blog/added_12876/ …
fine, votre site disposition style est vraiment merveilleux , Je suis cherchant pour la nouveau disposition style pour mon moncler doudoune personnel blog , j’aime vôtre, maintenant Je vais à aller chercher le exacte même design !
November 12th, 2010 at 7:24 pm
… track backe bei http://graigmarzullo.blogart.ro/ …
grand , votre blog thème est certainement bon , Je suis recherche tout nouveau thème pour mon moncler doudoune propre personnel site web , j’aime vôtre, maintenant Je vais aller chercher le similaires disposition style !
November 13th, 2010 at 1:11 am
… track backe bei http://www.freeblog.org.uk/troywlodyka/ …
excellent , votre site web modèle est véritablement grand, Je suis recherche pour obtenir un nouveau thème pour mon moncler doudoune propre blog site, j’aime vôtre, maintenant je vais aller chercher le même disposition style !
November 13th, 2010 at 1:39 pm
… track backe bei http://brentonmoring.bloging.ro/ …
très bon , votre site thème est réellement bon , Je suis recherche pour la nouveau thème pour mon moncler doudoune personnel site web , j’aime vôtre, maintenant Je vais aller recherche le similaires modèle !
January 16th, 2011 at 12:15 am
**YOUTUBE VIDEO REVIEWS ON THE HOTTEST ELECTRONICS OUT**
#1 SITE FOR THE LATEST REVIEWS ON THE HOTTEST TECHNOLOGY HITTING THE MAINSTREAM!
January 17th, 2011 at 4:06 am
MOST INFORMATIVE SITE FOR ELECTRONICS.
**YOUTUBE VIDEO REVIEWS ON THE HOTTEST ELECTRONICS OUT**
January 26th, 2011 at 8:11 pm
**YOUTUBE VIDEO REVIEWS ON THE HOTTEST ELECTRONICS OUT**
#1 SITE FOR THE LATEST REVIEWS ON THE HOTTEST TECHNOLOGY HITTING THE MAINSTREAM!
June 12th, 2011 at 4:38 pm
hello
really good article. Ready to hear more next week,my blog http://talonmpgraham.blogdrive.com/archive/20.html Many Thanks.
June 13th, 2011 at 9:54 pm
hello
really good article. Ready to hear more next week,my blog http://talonmpgraham.xanga.com/749901497/adressing-wedding-invitations-envelopes/ Many Thanks.
June 16th, 2011 at 9:11 am
really good article
I have spent a bit of time going through your posts, more than I should have but I must say, its worth it! http://night071.clipsy.org/2011/06/14/need-to-offer-full-specifics/ many Thanks.
June 20th, 2011 at 9:49 am
hello
Hello there just quality post! http://pamela11.dokyun.jp/ ,i had a great read.appreciate your article,My problem has been resolved.
June 20th, 2011 at 10:57 pm
very helpful
I preferred to thank you for this good article. http://bbzgm.fruitblog.net/ I by all odds liked every little bit of it
June 21st, 2011 at 8:17 pm
Great
You did a great job! http://denisha.lowprice.up4.net/2011/06/19/akila-ellery-a-amazing-match/
June 22nd, 2011 at 11:11 am
Great
You did a great job! http://denice.hostablog.net/2011/06/19/the-as-well-as-sized-designs-of-vogue-italia/
June 24th, 2011 at 12:55 pm
……. {christian louboutin egypte|escarpin christian louboutin pas cher|christian louboutin vente|magasins christian louboutin paris|christian louboutin miami|christian louboutin prix chaussures|christian louboutin livre|christian louboutin pompe a…
http://dustinbrunz.travelblog.fr/
June 26th, 2011 at 9:09 am
quality post
I have spent a bit of time going through your posts! http://demetria.blogcenter.de/2011/06/17/a-musing-near-to-the-achievement-of-muse/ ,i had a good read.
June 26th, 2011 at 12:08 pm
really
Fat women and girls always worried because of their ugly shaped bodies. They want to wear expensive and stylish outfits . http://bdczxnj.allmyblog.com/ but they can not wear these outwears
June 27th, 2011 at 12:34 pm
Greate
It’s such a great site! http://andrea1070smiths.onsugar.com/IIFA-awards-start-Toronto-18006763 Great post, I just bookmarked it on Digg.
June 29th, 2011 at 8:03 pm
really
Fat women and girls always worried because of their ugly shaped bodies. They want to wear expensive and stylish outfits . http://dhgjky.bloxode.com/ but they can not wear these outwears
July 18th, 2011 at 8:55 pm
Great One
I must say, its worth it! My link!http://joseph071.medykblog.pl/ ,thanks haha
August 9th, 2011 at 10:01 am
Great
love your blog, http://blogpro.eu/heidiad/ ,Thanks again.
August 13th, 2011 at 1:22 am
Great One
I must say, its worth it! My link, http://www.dreamblog.net/free/blanche/index.php,thanks haha
August 18th, 2011 at 2:09 am
really
Fat women and girls always worried because of their ugly shaped bodies. http://www.freeblog.com.br/kliuyhj/ , but they can not wear these outwears
September 1st, 2011 at 11:17 pm
very helpful
I preferred to thank you for this good article. http://www.pcztew.ewebsite.com/admin.php?a=a&a2=articles&nocache=0.4619965313232557 I by all odds liked every little bit of it
September 9th, 2011 at 3:37 am
Hello
My life,vist it http://juhuacha.blogonsisters.com/buzz/letter-wedding-cake-topper/ ,Thanks.
September 14th, 2011 at 4:51 am
Hello
My life,vist it http://xiangcai.myfotojournal.com/2011/sep/02/movie-theme-wedding-cakes/ ,Thanks.
September 19th, 2011 at 3:08 pm
Hello
My life,vist it http://sylviaj.webs.com/apps/blog/ ,Thanks.
October 21st, 2011 at 9:26 pm
Great
What sport requires the most brain calculations overall to play?, http://jtxyl.scoom.com/
December 2nd, 2011 at 5:11 am
quality post
ugg boots lilac love, http://www.ballaterholidaycottage.com ,thanks ugg