免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5440 | 回复: 10
打印 上一主题 下一主题

mysql_fetch_row的速度是不是比mysql_fetch_array快? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-08 21:31 |只看该作者 |倒序浏览




mysql_fetch_row

Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

mysql_fetch_array

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.






mysql_fetch_row和mysql_fetch_array之间有什么不同?看了官方手册,还是不明白有什么不同。


我更想知道,mysql_fetch_row的速度是不是比mysql_fetch_array快?


论坛徽章:
0
2 [报告]
发表于 2009-09-08 22:26 |只看该作者
一定是

论坛徽章:
0
3 [报告]
发表于 2009-09-09 09:46 |只看该作者
但是第一个不清晰, 谁知道$row[1],$row[2]是什么呀,还是第二个比较好.用字段做索引

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
4 [报告]
发表于 2009-09-09 09:53 |只看该作者
你可以尝试往数据库随机插入10万条数据,然后各自做实际的读取测试。

论坛徽章:
0
5 [报告]
发表于 2009-09-09 21:18 |只看该作者
原帖由 HonestQiao 于 2009-9-9 09:53 发表
你可以尝试往数据库随机插入10万条数据,然后各自做实际的读取测试。


你先把这里所有的MySQL的代码写出来吧。我真不会写测试代码。

论坛徽章:
0
6 [报告]
发表于 2009-09-09 23:21 |只看该作者
这类的效率损耗可以忽略不计

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
7 [报告]
发表于 2009-09-10 09:01 |只看该作者
原帖由 akyahoo 于 2009-9-9 21:18 发表


你先把这里所有的MySQL的代码写出来吧。我真不会写测试代码。


说句实话,如果这个不能如常写出来的话,那目前,基本上,这个性能问题,真可以不考虑。

但是我想下面的做法,就算大家不说,你也肯定做得到,相信你。

最简单测试:
1. 往数据库插入10万条随机数据,这个你应该会吧?
简单一点,两个字段:id md5,id就用1~100000,md5就用id的md5值即可,把他们写入到数据库,我想你应该可以做到,相信你。

2. 从mysql把前面的数据读取出来,这个我想你会把,while+fetch语句,只要读,别的啥也不做。

3. 完全相同的数据,你就让他们读,看看谁的速度更快。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
8 [报告]
发表于 2009-09-10 09:42 |只看该作者
直接贴一个吧:

<?php

// --------------------------------------------------------------------------

// File name   : test_speed.php

// Description : mysql结果集获取函数测试

// Requirement : PHP5 (http://www.php.net)

// --------------------------------------------------------------------------


define('DATA_COUNT',100000);&nbsp;&nbsp;&nbsp;&nbsp;//数据表的记录总数

define('TEST_COUNT',100);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//每轮测试循环总数


mysql_connect('localhost','root','123456') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());

mysql_query('CREATE TABLE IF NOT EXISTS `test_speed` (`id` int(10) NOT NULL auto_increment,  `md5` char(32) NOT NULL,  PRIMARY KEY  (`id`)) ENGINE=MEMORY;') or die(mysql_error());
mysql_query('TRUNCATE TABLE `test_speed`') or die(mysql_error());

$t = microtime(true);
{
&nbsp;&nbsp;&nbsp;&nbsp;$query = 'INSERT INTO test_speed (`md5`) VALUES (MD5('.implode(')),(MD5(',range(1,DATA_COUNT,1)).'));';
&nbsp;&nbsp;&nbsp;&nbsp;mysql_query($query) or die(mysql_error());
}
printf("insert:%f\n",microtime(true)-$t);

$query = "SELECT SQL_CACHE * FROM test_speed";
$res = mysql_query($query) or die(mysql_error());
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0) or die(mysql_error());
&nbsp;&nbsp;&nbsp;&nbsp;mysql_fetch_assoc($res) or die(mysql_error());
&nbsp;&nbsp;&nbsp;&nbsp;mysql_fetch_row($res) or die(mysql_error());
&nbsp;&nbsp;&nbsp;&nbsp;mysql_fetch_array($res) or die(mysql_error());
&nbsp;&nbsp;&nbsp;&nbsp;mysql_fetch_object($res) or die(mysql_error());
}

for($i=0,$t=microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;$res = mysql_query($query);
}
printf("mysql_query:\t%f\n",microtime(true)-$t);

for($i=0,$t=microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0);
}
printf("mysql_data_seek:\t%f\n\n\n",microtime(true)-$t);

for($i=0,$t = microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0);
&nbsp;&nbsp;&nbsp;&nbsp;while($row = mysql_fetch_assoc($res)){
&nbsp;&nbsp;&nbsp;&nbsp;}
}
printf("mysql_fetch_assoc:\t%f\n",microtime(true)-$t);

for($i=0,$t = microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0);
&nbsp;&nbsp;&nbsp;&nbsp;while($row = mysql_fetch_row($res)){
&nbsp;&nbsp;&nbsp;&nbsp;}
}
printf("mysql_fetch_row:\t%f\n",microtime(true)-$t);

for($i=0,$t = microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0);
&nbsp;&nbsp;&nbsp;&nbsp;while($row = mysql_fetch_array($res)){
&nbsp;&nbsp;&nbsp;&nbsp;}
}
printf("mysql_fetch_array:\t%f\n",microtime(true)-$t);

for($i=0,$t = microtime(true);$i<TEST_COUNT;$i++)
{
&nbsp;&nbsp;&nbsp;&nbsp;mysql_data_seek($res,0);
&nbsp;&nbsp;&nbsp;&nbsp;while($row = mysql_fetch_object($res)){
&nbsp;&nbsp;&nbsp;&nbsp;}
}
printf("mysql_fetch_object:\t%f\n",microtime(true)-$t);

mysql_close();
?>

论坛徽章:
0
9 [报告]
发表于 2009-09-11 06:04 |只看该作者
我回去后试了一下。mysql_fetch_array改成mysql_fetch_row后。程序就不能运行了。


真不知道你们亲手试过没有。

论坛徽章:
0
10 [报告]
发表于 2009-09-11 13:12 |只看该作者
我插一句,我不知道大家说的慢会慢都什么程度,以至于如此重视,在手册上很明显的地方,不知道是译者写的还是编者写的“有一点很重要必须指出,用 mysql_fetch_array() 并不明显 比用 mysql_fetch_row() 慢,而且还提供了明显更多的值。”
这就差不多了吧,敲代码本来就很繁琐,是件苦差事(个人观点),再在里面加上$row[0]、$row[1]、$row[2]之类的,您能分辨的清谁是谁吗?
用mysql_fetch_array(),直接就能很直观的看出来$row,$row[h],$row, $row[t]
呵呵,何乐而不为呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP