在PDO中,如果int被引用为字符串,那么性能是否存在差异?

und*_*ned 4 php mysql pdo

如果在准备好的PDO查询中将整数绑定为字符串,那么性能是否存在差异?在Mysql中,如果值绑定为int或字符串,则查询可以正常工作,但是在执行此操作时性能是否有任何差异?

$pdo = new PDO(
    "mysql:host={$host};port={$port};dbname={$dbname};charset=utf8", 
    $username, 
    $password
);
$statement = $pdo->prepare("SELECT * FROM `table` WHERE `id` = :param");

// Is there any performance difference between the two rows below
$statement->bindValue(":param", 5);
$statement->bindValue(":param", 5, PDO::PARAM_INT);

$statement->execute();
Run Code Online (Sandbox Code Playgroud)

绑定参数和指定其类型之间是否有任何区别,或者只是将其引用为字符串?

Riz*_*123 6

如果你想完全采用你指定类型的方法比你没有指定类型的方法稍快,默认类型是PDO::PARAM_STR.

如果你运行它100万次,平均值如下:

  • 指定的int类型:0.53秒($stmt->bindValue(":param", 5, PDO::PARAM_INT);)
  • 没有指定类型:0.66秒($stmt->bindValue(":param", 5);)
  • str type sepcified:0.70秒($stmt->bindValue(":param", 5, PDO::PARAM_STR);)

使用PHP 5.6.3版进行测试; 32位Windows;