简体   繁体   中英

Fastest way to count pages in the database

I'm creating a small website with PHP and MySql, and i need a fast way to count all the pages stored in the database. I know 3 ways to do this, but since I'm not much of an expert in PHP, MySql i have no idea which is faster or better.

The first way will be to use the PHP's mysql_num_rows function.

$query = mysqli_query($dbcon, "SELECT * FROM `pages`");
$count = mysqli_num_rows($query);
echo $count;

The second way will be to use the MySql command SELECT COUNT(*)

$query = mysqli_fetch_assoc(mysqli_query($dbcon, "SELECT COUNT(*) FROM `pages`"));
echo $query["COUNT(*)"];

And the third way will be to create a database table row to hold the total number of pages, which will increase/decrease every time i create or delete a page.

I'm not sure which one to use, I've tested all, and they work just about the same with about 200 pages.

Select count(*)

That sounds like the better way for a MyIsam storage engine at least, which stores that value and update it when there is any INSERT/DELETE/TRUNCATE. So there is no calculation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM