July 17, 2010

which is better single query or multiple query?

Question by mathew

I do have 8 tables. when a query fires from search page data from all these 8 tables is pulled out and displayed on the result page. What I want to know is which is the best optimized query method for this process??

what I do now is :

$result = mysql_query("SELECT * FROM TG_dat,TG_dat1,TG_dat2,TG_dat3 WHERE 
TG_dat.web = TG_dat1.web AND TG_dat.web = TG_dat2.web AND TG_dat.web = 
TG_dat3.web AND TG_dat.web='".$uri."'")or die(mysql_error());

or do i need to use this??

$result = mysql_query("SELECT * FROM TG_dat WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat1 WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat2 WHERE web='$uri'")or die(mysql_error());
$result = mysql_query("SELECT * FROM TG_dat3 WHERE web='$uri'")or die(mysql_error());

Answer by OMG Ponies

Your existing query is perfect – always try to use as few queries as possible.

Answer by Starx

your existing query is good. The point is to limit querying db server for better effieciency

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!