PHP 准备好的语句会导致多少次往返或数据库命中?
How many round trips or DB hits do PHP prepared statements cause?
这可能是 Whosebug 提出这个问题的错误部分,但我很好奇...我已经找了大约一个小时问 google 各种问题,但从未找到答案。我有 googled...
"PHP prepared statements number of round trips"
和大约 20 个其他 "tries" 找到此信息。几周前我确实看到了一张关于往返次数的小图表,说要做一个简单的 select 但有时会发生这种情况,我再也找不到那个网站了。
基本上,我的PHP非常简单。一个简单的...
- 数据库单例(读或写)
- a select,一次更新、替换或插入一行
- 关闭stmt
- 在析构函数中,单例关闭数据库连接
我想我记得读过它是 2 次点击,但我想澄清一下。另外,我是 OOP 编程的新手,所以如果我的功能做得非常糟糕并且不正确,请告诉我。我知道它有效,但有时仅仅因为某些东西有效并不意味着它是设计它的正确方法。
谢谢!下面是我的 PHP class.
中典型的 1 个函数示例
# returns false on error, $numOfRows on success
# $NUD just needs to be of type array - passed by REFERENCE
public function FetchUserFromDB($username, &$NUD)
{
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--$username--<b>' . $username . '</b>');
$query = 'SELECT ' . User::GetUserDBFields() . ' FROM UserDB WHERE username = ?'; // builds the SELECT and pulls in all fields from the User::GetUserDBFields function
try {
$stmt = $this->db->stmt_init(); // get the statement handle for prepared statements
if ($stmt->prepare($query) === false) throw new Exception('DB.php--FetchUserFromDB--prepare--FAILED--' . $stmt->error, 1);
if ($stmt->bind_param("s", $username) === false) throw new Exception('DB.php--FetchUserFromDB--bind_param--FAILED--' . $stmt->error, 1);
if ($stmt->execute() === false) throw new Exception('DB.php--FetchUserFromDB--execute--FAILED--' . $stmt->error, 1);
if ($stmt->store_result() === false) throw new Exception('DB.php--FetchUserFromDB--store_result--FAILED--' . $stmt->error, 1);
if ($stmt->bind_result($NUD["user_id"], $NUD["username"], $NUD["company_name"], $NUD["f_name"], $NUD["l_name"], $NUD["street_num"], etc) === false) throw new Exception('DB.php--FetchUserFromDB--bind_result--FAILED--' . $stmt->error, 1);
$numOfRows = $stmt->num_rows(); // store the number of rows
if ($numOfRows > 0) $stmt->fetch(); // only bother to fetch if there are more than 0 rows
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--fetch--$numOfRows=' . $numOfRows);
$stmt->close();
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--fetch--$NUD["username"]=<b>' . $NUD["username"] . '</b>');
return $numOfRows;
} catch (Exception $e) {
$stmt->close();
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--**********' . $e->getMessage());
return false; // error
}
}
一次发送语句,一次发送bindvars并执行它......但是你可以将不同的bindvars发送到同一个语句(无需重新发送语句)再次执行它使用不同的参数,它会使数据库行程的计数稍微不那么有意义.....并且您只在执行时将绑定变量发送到数据库,而不是针对每个单独的绑定变量(在抽象层中缓冲) – 马克贝克 2 天前
我无法评价马克,因为他以评论的形式回答了这个问题。我让他来这里回答这个问题,这样他就可以获得荣誉,但 2 天内没有回复,所以我想我会回答。如果管理员可以给他荣誉,那就太好了!
顺便说一句 - 谢谢马克!
这可能是 Whosebug 提出这个问题的错误部分,但我很好奇...我已经找了大约一个小时问 google 各种问题,但从未找到答案。我有 googled...
"PHP prepared statements number of round trips"
和大约 20 个其他 "tries" 找到此信息。几周前我确实看到了一张关于往返次数的小图表,说要做一个简单的 select 但有时会发生这种情况,我再也找不到那个网站了。
基本上,我的PHP非常简单。一个简单的...
- 数据库单例(读或写)
- a select,一次更新、替换或插入一行
- 关闭stmt
- 在析构函数中,单例关闭数据库连接
我想我记得读过它是 2 次点击,但我想澄清一下。另外,我是 OOP 编程的新手,所以如果我的功能做得非常糟糕并且不正确,请告诉我。我知道它有效,但有时仅仅因为某些东西有效并不意味着它是设计它的正确方法。
谢谢!下面是我的 PHP class.
中典型的 1 个函数示例 # returns false on error, $numOfRows on success
# $NUD just needs to be of type array - passed by REFERENCE
public function FetchUserFromDB($username, &$NUD)
{
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--$username--<b>' . $username . '</b>');
$query = 'SELECT ' . User::GetUserDBFields() . ' FROM UserDB WHERE username = ?'; // builds the SELECT and pulls in all fields from the User::GetUserDBFields function
try {
$stmt = $this->db->stmt_init(); // get the statement handle for prepared statements
if ($stmt->prepare($query) === false) throw new Exception('DB.php--FetchUserFromDB--prepare--FAILED--' . $stmt->error, 1);
if ($stmt->bind_param("s", $username) === false) throw new Exception('DB.php--FetchUserFromDB--bind_param--FAILED--' . $stmt->error, 1);
if ($stmt->execute() === false) throw new Exception('DB.php--FetchUserFromDB--execute--FAILED--' . $stmt->error, 1);
if ($stmt->store_result() === false) throw new Exception('DB.php--FetchUserFromDB--store_result--FAILED--' . $stmt->error, 1);
if ($stmt->bind_result($NUD["user_id"], $NUD["username"], $NUD["company_name"], $NUD["f_name"], $NUD["l_name"], $NUD["street_num"], etc) === false) throw new Exception('DB.php--FetchUserFromDB--bind_result--FAILED--' . $stmt->error, 1);
$numOfRows = $stmt->num_rows(); // store the number of rows
if ($numOfRows > 0) $stmt->fetch(); // only bother to fetch if there are more than 0 rows
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--fetch--$numOfRows=' . $numOfRows);
$stmt->close();
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--fetch--$NUD["username"]=<b>' . $NUD["username"] . '</b>');
return $numOfRows;
} catch (Exception $e) {
$stmt->close();
if (DEBUGMODE) MyDebug::DebugMsgs('DB.php--FetchUserFromDB--**********' . $e->getMessage());
return false; // error
}
}
一次发送语句,一次发送bindvars并执行它......但是你可以将不同的bindvars发送到同一个语句(无需重新发送语句)再次执行它使用不同的参数,它会使数据库行程的计数稍微不那么有意义.....并且您只在执行时将绑定变量发送到数据库,而不是针对每个单独的绑定变量(在抽象层中缓冲) – 马克贝克 2 天前
我无法评价马克,因为他以评论的形式回答了这个问题。我让他来这里回答这个问题,这样他就可以获得荣誉,但 2 天内没有回复,所以我想我会回答。如果管理员可以给他荣誉,那就太好了!
顺便说一句 - 谢谢马克!