【问题标题】:'LIKE' operator in SQL query is very slow with pdo_sqliteSQL 查询中的“LIKE”运算符使用 pdo_sqlite 非常慢
【发布时间】: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();
?>

这是我使用的快速“基准”:

  1. pdo_sqlite 测量的 XDebug 跟踪
  2. 带有“.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?
  • 我编辑了我的第一篇文章,问题与“LIKE operator”无关。我在这张表上没有任何索引,但这似乎不是问题。

标签: php sql sqlite pdo sql-like


【解决方案1】:

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

对于您的第二个脚本,情况肯定是这样:第一个查询需要 1.3 多秒,因为它还读取数据,而其余的则从 RAM 中获取数据。

详情请见http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#pragma-cache_size。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多