dongxuxian1123 2015-08-28 15:16
浏览 76
已采纳

bindValue / bindParam与数组性能

I have always passed an array of my values into execute as it seemed easier to read imo. I was recently working on a script and noticed it was using bindParam and later came to find out how this passes the variable reference (I never knew).

With that said, in a current project I can think of quite a few uses for bindParam instead of my current array usage.

With that said, is there any performance difference between bindValue/bindParam and using an array on execute... especially in the case of repeated loops? I find myself doing a lot of $stmt->execute( array_merge($binding_clause, $binding) ); where I have a few binds that won't change and some that will, in loops of course, where as far as I can tell using bindParam would be perfect for this.

Does adding the type (PDO::PARAM_STR, PDO::PARAM_INT) with the first have any performance over not doing so when using an array (I believe it is string by default with arrays)?

Example difference between the two (these are prepared statements) :

$binding = array(
    'cw_account_id' => $_SESSION['user']['account_id'],
    'cw_date_start' => $date_start,
    'cw_date_end' => $date_end,
    'cw_start' => $b_counter * 500
);  

$stmt->execute($binding);

compared to

$stmt->bindValue(':cw_account_id', $_SESSION['user']['account_id'], PDO::PARAM_INT);
$stmt->bindValue(':cw_date_start', $date_start, PDO::PARAM_INT);
$stmt->bindValue(':cw_date_end', $date_end, PDO::PARAM_INT);
$stmt->bindValue(':cw_start', $b_counter * 500, PDO::PARAM_INT);

$stmt->execute();

Aside from the questions above, would each $stmt->bindValue() here be another trip to the db as compared to one when using the array method?

Aside from readability of code, and how bindParam references values, are there are performance positives/negatives between the two (small and large scale usage... repeated loops included)?

  • 写回答

1条回答 默认 最新

  • doudizhi947129 2015-08-28 15:20
    关注

    Aside from readability of code, and how bindParam references values, are there are performance positives/negatives between the two

    No.

    would each $stmt->bindValue() here be another trip to the db

    No.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大