如果在准备好的 PDO 查询中将整数绑定(bind)为字符串,性能会有所不同吗?在 Mysql 中,如果值被绑定(bind)为一个 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();
绑定(bind)参数并指定其类型,或者只是将其作为字符串引用之间有什么区别吗?
最佳答案
如果您想完全采用它,指定类型的方法比未指定类型的方法稍快,默认类型为 PDO::PARAM_STR
。
如果你运行它 100 万次,avarage 如下:
- 指定的 int 类型:0.53 秒 (
$stmt->bindValue(":param", 5, PDO::PARAM_INT);
) - 未指定类型:0.66 秒 (
$stmt->bindValue(":param", 5);
) - str 类型指定:0.70 秒 (
$stmt->bindValue(":param", 5, PDO::PARAM_STR);
)
使用 PHP 版本 5.6.3 进行测试; 32 位 Windows;
关于php - 在 PDO 中,如果将 int 引用为字符串,性能会有所不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184215/