我将任意多语句 SQL 传递给 Mysqli(使用 mysqlnd)并检索结果集。SELECT 语句可以包含LIMIT
(或不包含)子句。我需要的是将获取的行限制为 1000(硬编码限制)。这就是我所拥有的:
define('SAFE_ROWS', 1000); // strict limit
$sql = "SELECT * FROM table1 LIMIT 99999999";
$mysqli->multi_query($sql);
// unbuffered: results are stored at MySQL-side
// and transfer when i call fetch*
$result = $mysqli->use_result();
$rows = [];
while ($row = $result->fetch_array()) {
if (count($rows) >= SAFE_ROWS) {
#############################################
# QUESTION: what i have to do here #
# to skip remaining rows of current result? #
#############################################
break;
}
$rows[] = $row;
}
// As slow as much rows remained unfetched:
$result->free();
如果当前结果中有很多未提取的行,则释放结果需要太多时间。有没有办法告诉MySQL“我不再需要这个结果的剩余行,只是把它埋起来”?
PS。我知道该SQL_SELECT_LIMIT
选项,但是当显然设置了选择限制 ( SELECT * FROM t1 LIMIT 99999999
) 时,此限制将被忽略。