问题描述
我在 PHP 中为 REGEXP 关键字附加了一个函数,我发现 SQLite3Result::fetchArray 重新执行查询!多么低效!
I attached a function to the REGEXP keyword in PHP and I discovered that SQLite3Result::fetchArray re-executes the query! What inefficiency!
function _sqliteRegexp($pattern, $string)
{
echo $pattern . ' - ' . $string . '<br/>';
return preg_match('/' . $pattern . '/', $string);
}
$handle = new SQLite3($filename);
$handle->createFunction('regexp', '_sqliteRegexp', 2);
// (1)
$result = $handle->query("SELECT * FROM pages WHERE '" . $request_uri . "' REGEXP request LIMIT 1;");
// (2)
$result->fetchArray();
给予,例如:
(1)
\/ - /admin/settings/1
\/sqlite - /admin/settings/1
\/admin - /admin/settings/1
\/admin\/logout - /admin/settings/1
\/admin\/settings(\/[0-9]+)? - /admin/settings/1
(2)
\/ - /admin/settings/1
\/sqlite - /admin/settings/1
\/admin - /admin/settings/1
\/admin\/logout - /admin/settings/1
\/admin\/settings(\/[0-9]+)? - /admin/settings/1
(1) 和 (2) 都对模式和字符串进行了响应.我高度怀疑该查询被重新执行,这将是非常多余的.这是一个错误还是只是我仓促下结论?
Both (1) and (2) echo the pattern and string the very same. I highly suspect the query is re-executed, which would be quite redundant. Is this a bug or just me being hastily concluding?
推荐答案
源代码显示query
获取第一行只是为了确定结果是否为空.第一次调用 fetchArray
将再次执行查询.(效率太高了.或者副作用.)
A look into the source code shows that query
fetches the first row just to determine whether the result is empty, or not. The first call to fetchArray
will execute the query again.(So much for efficiency. Or side effects.)
如果您只对一行感兴趣,请使用 querySingle代码>
代替.
If you are interested in no more than one row, use querySingle
instead.
回答您的问题:这是按设计工作的.那个设计是一个错误.
To answer your question: this works as designed. And that design is a bug.
这篇关于PHP SQLite3Result::fetchArray 重新执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!