在函数或方法中使用时受益于 PDO 准备语句
Benefiting from PDO Prepared statement when used in a function or method
每 http://php.net/manual/en/pdo.prepared-statements.php
The query only needs to be parsed (or prepared) once, but can be
executed multiple times with the same or different parameters. When
the query is prepared, the database will analyze, compile and optimize
its plan for executing the query. For complex queries this process can
take up enough time that it will noticeably slow down an application
if there is a need to repeat the same query many times with different
parameters. By using a prepared statement the application avoids
repeating the analyze/compile/optimize cycle. This means that prepared
statements use fewer resources and thus run faster.
因此,以下内容将在使用准备好的语句时提高性能:
<?php
...
$stmt=$conn->prepare('SELECT a FROM mytable WHERE x=?'); //I often use globals or similar for $conn
foreach($array as $id){
$stmt->execute(array($id));
$data=$stmt->fetchAll(PDO::FETCH_COLUMN);
...
}
...
?>
为了消除重复代码,我希望在函数中执行查询。
在这种情况下,我如何从准备好的语句的改进性能中获益?
请注意,以下代码没有提供任何效率优势,实际上比不首先使用准备好的语句要慢。
<?php
function getStuff($x)
{
...
$stmt=$conn->prepare('SELECT a FROM mytable WHERE x=?');
$stmt->execute(array($x));
$data=$stmt->fetchAll(PDO::FETCH_COLUMN);
...
return $data;
};
...
foreach($array as $x){
$data=getStuff($x);
...
}
...
?>
我以前用过这个:
function getStuff($x)
{
global $conn;
static $stmt;
if (null === $stmt) {
$stmt = $conn->prepare('SELECT a FROM mytable WHERE x=?');
}
如果函数是无状态的,则此方法有效。
例如,将 $conn
变量作为参数是没有意义的:
function getStuff($conn, $x)
{
static $stmt;
if (null === $stmt) {
$stmt = $conn->prepare('SELECT a FROM mytable WHERE x=?');
}
因为静态语句不一定属于提供的连接。
无论你怎么做,你都需要让语句在函数中持久化,或者以其他方式将函数用作工厂并在别处缓存语句。
编辑,测试非 oop 场景:
echo '<pre>';
function do_something()
{
static $i = 0;
echo $i . PHP_EOL;
$i++;
}
do_something();
do_something();
do_something();
do_something();
do_something();
输出:
0
1
2
3
4
每 http://php.net/manual/en/pdo.prepared-statements.php
The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters. When the query is prepared, the database will analyze, compile and optimize its plan for executing the query. For complex queries this process can take up enough time that it will noticeably slow down an application if there is a need to repeat the same query many times with different parameters. By using a prepared statement the application avoids repeating the analyze/compile/optimize cycle. This means that prepared statements use fewer resources and thus run faster.
因此,以下内容将在使用准备好的语句时提高性能:
<?php
...
$stmt=$conn->prepare('SELECT a FROM mytable WHERE x=?'); //I often use globals or similar for $conn
foreach($array as $id){
$stmt->execute(array($id));
$data=$stmt->fetchAll(PDO::FETCH_COLUMN);
...
}
...
?>
为了消除重复代码,我希望在函数中执行查询。
在这种情况下,我如何从准备好的语句的改进性能中获益?
请注意,以下代码没有提供任何效率优势,实际上比不首先使用准备好的语句要慢。
<?php
function getStuff($x)
{
...
$stmt=$conn->prepare('SELECT a FROM mytable WHERE x=?');
$stmt->execute(array($x));
$data=$stmt->fetchAll(PDO::FETCH_COLUMN);
...
return $data;
};
...
foreach($array as $x){
$data=getStuff($x);
...
}
...
?>
我以前用过这个:
function getStuff($x)
{
global $conn;
static $stmt;
if (null === $stmt) {
$stmt = $conn->prepare('SELECT a FROM mytable WHERE x=?');
}
如果函数是无状态的,则此方法有效。
例如,将 $conn
变量作为参数是没有意义的:
function getStuff($conn, $x)
{
static $stmt;
if (null === $stmt) {
$stmt = $conn->prepare('SELECT a FROM mytable WHERE x=?');
}
因为静态语句不一定属于提供的连接。
无论你怎么做,你都需要让语句在函数中持久化,或者以其他方式将函数用作工厂并在别处缓存语句。
编辑,测试非 oop 场景:
echo '<pre>';
function do_something()
{
static $i = 0;
echo $i . PHP_EOL;
$i++;
}
do_something();
do_something();
do_something();
do_something();
do_something();
输出:
0
1
2
3
4