gpt4 book ai didi

php - 使用pdo_sqlite在SQL查询中的'LIKE'运算符非常慢

转载 作者:行者123 更新时间:2023-12-03 19:45:20 25 4
gpt4 key购买 nike

我发现使用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跟踪
SQLite二进制文件带有“ .timer打开”

NomChaine like 'DCLC257__'
●pdo_sqlite:1.4521s✘
●sqlite3二进制:0.084秒✔

NomChaine like 'DCLC257%'
●pdo_sqlite:1.4881s✘
●sqlite3二进制:0.086秒✔

NomChaine = 'DCLC25736'
●pdo_sqlite:0.002秒✔(我认为它长一点,但是非常快)
●sqlite3二进制:0.054秒✔

我该如何改善这种情况?


编辑:也许我太在意'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

最佳答案

您是否有机会在同一脚本中(一个接一个地)运行PDO与二进制文件?如果这样做的话,使用二进制文件可获得更好的结果是正常的,因为PDO在高速缓存为空(因此会命中光盘)时运行,而二进制文件则从RAM中获取数据。

对于您的第二个脚本,确实是这样:第一个查询获得1.3+秒,因为它还读取数据,而其余查询从RAM中获取数据。

有关详细信息,请参见http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#pragma-cache_size

关于php - 使用pdo_sqlite在SQL查询中的'LIKE'运算符非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22026084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com