views:

68

answers:

2

I'm now running two queries,which is not so efficient I think:

select count(*) from table where id>0;
select * from table where id>0 limit 10;
+2  A: 

You can do it using SQL_CALC_FOUND_ROWS.

SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();

But check out this blog entry about the performance of SQL_CALC_FOUND_ROWS.

Asaph
That articles says two queries is more efficient than one?I can't even believe..
Mask
@Mask: Indeed it does. A little surprising...
Asaph
I don't trust his testing.
Mask
+1  A: 

If you are using PHP just run mysql_num_rows on the query.

$query = mysql_query("SELECT * FROM ... WHERE ...");
$rows = mysql_num_rows($query);
Frankie