/    Sign up×
Community /Pin to ProfileBookmark

Time elapsing for query or while loop

Hello,

For the query below:

for($i=0; $i<=3; $i++){

$query = mysql_query(“SELECT * FROM events_usa WHERE `sdate` = ‘{$daydisplay}’ {$category_query} {$state_query} ORDER BY id DESC LIMIT $i,1″);

while ($row = mysql_fetch_array($query)) {
$aa[$i]=$row[‘title’];}}

echo “<td valign=”top” width=130> $day_num $daydisplay $aa[0] $aa[1] $aa[2] $aa[3] </td>”;

for($f=0; $f<=3; $f++)
{$aa[$f]=null;}

1-How can you make php print the time elapsed for a particular mysql query such as this one?(or is it the while loop) I will try different versions so that I minimize the time elapsing for the query, but how can I print the time elapsing? Or is it not the query but the while loop taking too long?
2-Also, how can I shorten the time for the query, do I need to separate tables for say, category_query thus avoiding one extra “WHERE” clause? Is that an effective strategy?
3-Or if I decreased the $i<=3 value to 2 instead of 3 in the “for” statement, would it mean it would effectively eliminate a quarter of a time passing because it is may be doing the query 4 times because of the for loop?

Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SyCoDec 13.2008 — You can use microtime() and key start/finish arrays with the iteration value.

You have a query in a loop and a loop in a loop. Both of those should be avoided unless nothing else will work.

If you send one big query it uses more memory but hits the server less often. If you start to get high traffic and things slow down, with the big query you just up your hardware, with the multiple hits method you have to rewrite your code, then up your hardware.

I presume these {$category_query} {$state_query} are additional dynamically generated queries? It's all going to slow things down.

BTW you don't need back ticks on the columns unless the column name has a space or reserved word.
×

Success!

Help @ketanco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...