Jump to content

Count


jaymc

Recommended Posts

I have a query that queries a table and pulls out the last 5 comments posted

 

I need it to also say how many comments there are in total

 

At current the first query has a LIMIT 0,5 si I cant use mysql_num_rows()

 

And, I cant take the limit out because the query needs to be optimised

 

Will I need a seperate query just to tell me how many comments all together, or is there a way to intergreat a function into the query such as COUNT()

 

Cheers

Link to comment
Share on other sites

I was going to so that.. while($i < 5 mysql_fetch_array())

 

However, its going to be putting all of the rows in that fetch, and when there are comments reading 1000 chars its going to cause bother..

Link to comment
Share on other sites

id | user | poster | timestamp | comment | valid

 

From the query I need the last 5 comments (id | user | poster | timestamp | comment )

 

I also need the total number of comments in the table

 

So I may only be displaying 5 but I want to to show how many comments in total...

Link to comment
Share on other sites

In case you need an example:

 

<?php
$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM comments ORDER BY timestamp DESC LIMIT 5';
$result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$total_rows = mysql_result(mysql_query('SELECT FOUND_ROWS()'),0);
while ($row = mysql_fetch_row($result)) {
	// print comments, etc.
}
} else {
// print error or something
}

?>

Link to comment
Share on other sites

My benchmarks have both methods about the same on a table of 91 records (the largest table I happen to have on my test db...not so big).

 

In this case, the query is trivial.  SQL_CALC_FOUND_ROWS is more efficient in situations involving more complex queries and perhaps larger results sets.  Essentially the server is not required to run the query twice to satisfy a count and a query, so that's where you gain efficiency.  When I added a WHERE clause, the SCFR method gained an advantage.

 

I'd encourage jaymc to benchmark both versions of the actual query on the actual data to see which is better for that particular dataset.

Link to comment
Share on other sites

I will look into this

 

Thats another thing, the queries run so fast its hard to see which is faster

 

Is there a way to display query execution time

 

However, that will fluctuate depending on current server load

 

1 second there could be 50 queries hitting the server

 

The next only 5

 

Its going to be really hard to get an accurate result of which is faster?

Link to comment
Share on other sites

To benchmark these, you need to take multiple execution times to get a reliable time.... something like this:

 

<?php

$loops = 100;

$start = microtime(1);

for ($i = 0; $i < $loops; $i++) {
    // put test code #1 here
}

echo "First way took <b>", microtime(1) - $start, "</b> seconds.<br><br>\n";

$start = microtime(1);

for ($i = 0; $i < $loops; $i++) {
    // put test code #2 here
}

echo "Second way took <b>", microtime(1) - $start, "</b> seconds.<br><br>\n";

?>

 

With 100 loops on those queries we have been discussing, the execution times were around 0.08-0.09 seconds... which is no big deal.  But if your site gets a lot of traffic, maybe you'll need to make your code as efficient as possible.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.