【发布时间】:2014-02-25 21:01:10
【问题描述】:
我发现使用 pdo_sqlite(PHP 5.3 或 PHP 5.4)时,“SELECT”SQL 查询中的“LIKE”运算符非常慢。
在 sqlite3 二进制文件中输入相同的查询要快得多。
示例代码:
<?php
$bdd = new PDO('sqlite:./chaines_centre.db');
$reponse = $bdd->prepare("select DateMonteeAuPlan, Debut, Fin, Statut from ReportJobs where NomJob = ? and NomChaine like 'DCLC257__' order by DateMonteeAuPlan DESC limit 20;");
$reponse->execute($_GET['job']);
while ($donnees = $reponse->fetch())
{
// whatever...
}
$reponse->closeCursor();
?>
这是我使用的快速“基准”:
- pdo_sqlite 测量的 XDebug 跟踪
- 带有“.timer on”的 SQLite 二进制文件
NomChaine like 'DCLC257__' :
● pdo_sqlite : 1.4521s ✘
● sqlite3 二进制:0.084s ✔NomChaine like 'DCLC257%' :
● pdo_sqlite : 1.4881s ✘
● sqlite3 二进制:0.086s ✔NomChaine = 'DCLC25736' :
● pdo_sqlite : 0.002s ✔(我认为它有点长,但非常快)
● sqlite3 二进制:0.054s ✔
我该如何改善这种情况?
编辑:也许我过于关注“LIKE”运算符。
<?php
$bdd = new PDO('sqlite:./chaines_centre.db');
$time_start = microtime(true);
$reponse = $bdd->query("select DateMonteeAuPlan, Debut, Fin, Statut from ReportJobs where NomJob = 'NSAVBASE' and NomChaine like 'DCLC257%' order by DateMonteeAuPlan DESC limit 20;");
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Situation 1 : $time second(s)<br><br>";
// Output : 1.3900790214539 second(s)
$time_start = microtime(true);
$reponse = $bdd->query("select DateMonteeAuPlan, Debut, Fin, Statut from ReportJobs where NomJob = 'NSAVBASE' and NomChaine like 'DCLC257%' limit 20;");
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Situation 2 : $time second(s)<br><br>";
// Output : 0.0030009746551514 seconde(s)
$time_start = microtime(true);
$reponse = $bdd->query("select DateMonteeAuPlan, Debut, Fin, Statut from ReportJobs where NomJob = 'NSAVBASE' and NomChaine = 'DCLC25736' order by DateMonteeAuPlan DESC limit 20;");
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Situation 3 : $time second(s)<br><br>";
// Output : 0 seconde(s)
?>
通过删除LIKE 运算符或order by DateMonteeAuPlan,查询将在预期时间执行...
太奇怪了。 o_O
【问题讨论】:
-
NomChaine 是否已编入索引?
-
如果不是,那为什么只会影响 PDO?
-
我编辑了我的第一篇文章,问题与“
LIKEoperator”无关。我在这张表上没有任何索引,但这似乎不是问题。
标签: php sql sqlite pdo sql-like