$s = explode (" ", microtime());
$s = $s[0]+$s[1];
$con = mysqli_connect ('localhost', 'test', 'pass', 'db') or die('Err');

for ($i=0; $i<1000; $i++) {

  $stmt = $con -> prepare( " SELECT MAX(id) AS max_id , MIN(id) AS min_id FROM tb ");
  $stmt -> execute();
  $stmt->bind_result($M,$m);
  $stmt->free_result();
  $rand = mt_rand( $m , $M ).'<br/>';

  $res = $con -> prepare( " SELECT * FROM tb WHERE id >= ? LIMIT 0,1 ");
  $res -> bind_param("s", $rand);
  $res -> execute();
  $res->free_result();
}

$e = explode (" ", microtime());
$e = $e[0]+$e[1];
echo  number_format($e-$s, 4, '.', '');

// and:

$link = mysql_connect ("localhost", "test", "pass") or die ();
mysql_select_db ("db") or die ("Unable to select database".mysql_error());

for ($i=0; $i<1000; $i++) {
  $range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM tb ");
  $range_row = mysql_fetch_object( $range_result ); 
  $random = mt_rand( $range_row->min_id , $range_row->max_id );
  $result = mysql_query( " SELECT * FROM tb WHERE id >= $random LIMIT 0,1 ");
}

defenitly prepared statements are much more safer but also every where it says that they are much faster BUT in my test on the above code I have: - 2.45 sec for prepared statements - 5.05 sec for the secon example

defenitly准备的声明更安全,但也在每个说它更快更快但在我对上述代码的测试中: - 2.45秒准备语句 - 5.05秒secon示例

What do you think I'm doing wrong? Should I use the second solution or I should try to optimize the prep stmt?

你觉得我做错了什么?我应该使用第二种解决方案,还是应该尝试优化准备工作?

4 个解决方案

#1


23

What you are doing wrong is that you are preparing the statement a thousand times and only run each prepared statement once. You should prepare it once, and run it a thousand times.

你做错了的是你准备了一千次这个陈述并且只运行一次准备好的陈述。你应该准备一次,并运行一千次。

更多相关文章

  1. 无法连接远程MySQL数据库的解决方案
  2. MySQL 示例数据库sakila
  3. [征集] MySQL交叉表解决方案及散分
  4. MySQL官网示例数据库emploees分析使用
  5. PHP MYSQL 出现中文乱码的解决方案
  6. Amoeba for MySQL---分布式数据库Proxy解决方案
  7. MySQL数据库导入或者同步大量数据时数据丢失解决方案
  8. 关于mysql 和Oracle的一大堆麻烦问题的解决方案
  9. 关于mysql无法添加中文数据的问题以及解决方案

随机推荐

  1. 序列化是什么意思
  2. 推荐10款ASP.NET过滤特效(收藏)
  3. XML的解析与创建范例
  4. xml编辑器哪个好?最好用的五款xml编辑工具
  5. 内容和属性用法的相关文章推荐
  6. Java中解析XML的方式有哪些
  7. 传智播客2017xml视频教程的课件源码分享
  8. XML是什么,有什么作用
  9. List集合的9篇内容推荐
  10. XML四大特性基础入门教程推荐