开发者

Is it possible to get count(*) and * more efficiently?

开发者 https://www.devze.com 2022-12-12 01:56 出处:网络
I\'m now running two queries,which is not so efficient I think: select count(*) from table where i开发者_高级运维d>0;

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

select count(*) from table where i开发者_高级运维d>0;
select * from table where id>0 limit 10;


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消