简体   繁体   English

SQL和PHP-哪个更快mysql_num_rows()或'select count()'?

[英]SQL & PHP - Which is faster mysql_num_rows() or 'select count()'?

I'm just wondering which method is the most effective if I'm literally just wanting to get the number of rows in a table. 我只是想知道哪种方法最有效,如果我只是想获取表中的行数。

$res = mysql_query("SELECT count(*) as `number` FROM `table1`");
$count = mysql_fetch_result($res,0,'number');

or 要么

$res = mysql_query("SELECT `ID` FROM `table1`");
$count = mysql_num_rows($res);

Anyone done any decent testing on this? 有人对此做过任何体面的测试吗?

mysql_query() transfers all result records from the MySQL into the php pcrocess before it returns (unlike mysql_unbufferd_query() ). mysql_query()在返回之前将所有结果记录从MySQL传输到php pcrocess中(与mysql_unbufferd_query()不同)。 That alone would make the mysql_num_rows() version slower. 仅此一项将使mysql_num_rows()版本变慢。

Furthermore for some engines (like MyISAM) MySQL can serve a Count(*) request from the index of the table without hitting the actual data . 此外,对于某些引擎(如MyISAM),MySQL可以从表的索引中满足Count(*)请求, 而无需访问实际数据 A SELECT * FROM foo on the other hand results in a full table scan and MySQL has to read every single dataset. 另一方面, SELECT * FROM foo导致全表扫描,而MySQL必须读取每个数据集。

Test in database with more then 2300000 rows , type: InnoDB , size near 1 GiB, using xhprof 测试数据库中有更多然后230万行 ,类型:InnoDB的 ,近1吉布大小,使用XHProf的

test1: 测试1:

    ....SELECT COUNT(id) as cnt FROM $table_name....;
       row= mysqli_fetch_assoc($res2);
   echo $row['cnt'];
        //result1:
        1,144,106
        1,230,576
        1,173,449
        1,163,163
        1,218,992

test2: 测试2:

....SELECT COUNT(*) as cnt FROM $table_name....;
       row= mysqli_fetch_assoc($res2);
   echo $row['cnt'];
//result2:
1,120,253
1,118,243   
1,118,852
1,092,419
1,081,316

test3: 测试3:

 ....SELECT * FROM $table_name....;
    echo mysqli_num_rows($res2);
    //result3:
7,212,476
6,530,615
7,014,546
7,169,629
7,295,878

test4: 测试4:

     ....SELECT * FROM $table_name....;
        echo mysqli_num_rows($res2);
        //result4:
1,441,228
1,671,616
1,483,050
1,446,315
1,647,019

conclusion: The fastest method is in the test2 : 结论:最快的方法是在test2中:

....SELECT COUNT(*) as cnt FROM $table_name....;
       row= mysqli_fetch_assoc($res2);
   echo $row['cnt'];

Definitely the first. 绝对是第一个。 MySQL can usually do this by looking at an index rather than the whole table, and if you use MyISAM (the default), the row count for the table is stored in the table metadata and will be returned instantly . MySQL的通常可以通过看一个指标,而不是整个表中做到这一点,如果你使用MyISAM(默认值),表的行计数被存储在表中的元数据,将被立即返回。

Your second method will not only read the entire table into memory but also send it to the client through the network before the client counts the rows. 第二种方法不仅将整个表读入内存,而且还要在客户端计算行数之前通过网络将其发送给客户端。 Extremely wasteful! 极度浪费!

I don't really think any testing is needed. 我真的不认为需要任何测试。

Doing the COUNT in the SQL query 在SQL查询中执行COUNT

1) Sends only one row of data back the to client (instead of every row) 1)仅将一行数据发送回客户端(而不是每一行)

2) Lets SQL do the count for you which is likely always going to be faster than PHP. 2)让SQL为您做计数,这很可能总是比PHP快。

I guess count(1) will be even faster: 我想count(1)会更快:

$res = mysql_query("SELECT count(1) as `number` FROM `table1`");
$count = mysql_fetch_result($res,0,'number');

Although haven't tried the proposed methods, the first makes database fetch all the records and count them in the database, the second makes database fetch a separate field for all the records and count the number of results on the server. 尽管尚未尝试所提出的方法,但第一种方法使数据库获取所有记录并在数据库中对它们进行计数,第二种方法使数据库获取所有记录的单独字段并计算服务器上的结果数。
As a rule of thumb the less data you fetch for a particular record the less time it will take therefore I'd vote for updated first method (fetching constant for every record and counting the number of constants fetched). 根据经验,为特定记录获取的数据越少,花费的时间也就越少,因此我投票支持更新的第一种方法(为每条记录获取常量并计算获取的常量数量)。

Using Count with index and inodb makes it too much slow, but when use it with mysqli_num_rows it returns without any delay. 将Count与index和inodb一起使用会使速度太慢,但与mysqli_num_rows一起使用时,它会立即返回。 you can check mysqli_num_rows result at http://ssajalandhar.org/generalinstruction-0-1-0.html it wouldn't take fraction of second to load. 您可以在http://ssajalandhar.org/generalinstruction-0-1-0.html上检查mysqli_num_rows的结果,这不需要花费几分之一秒的时间。 For me mysqli works awesome. 对我来说,mysqli很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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