LoginSignup
0
0

More than 1 year has passed since last update.

MariaDB の LIMIT 句や OFFSET 句には指定できる限界値がある

Last updated at Posted at 2021-07-29

MariaDB に限らず、MySQL や PostgreSQL の場合にも限界値はあるようだが、今回は MariaDB でのみ検証を実施。

環境

MariaDB 10.4.11

発端

ページネーション用の OFFSET を GET パラメータより受け取るよくある検索画面にて、膨大な数を指定された際にデータベースエラーが発生した。
これまで意識したことが無かったが、そりゃあ限界値あるよね。

検証

LIMIT 句の場合

MariaDB [testdb]> SELECT * FROM users LIMIT 18446744073709551615;
+----+-------+-------------------+-------------------+----------+------------+----------------+---------------------+---------------------+
| id | name  | email             | email_verified_at | password | logined_at | remember_token | created_at          | updated_at          |
+----+-------+-------------------+-------------------+----------+------------+----------------+---------------------+---------------------+
|  1 | user1 | user1@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  2 | user2 | user2@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  3 | user3 | user3@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  4 | user4 | user4@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  5 | user5 | user5@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  6 | user6 | user6@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  7 | user7 | user7@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  8 | user8 | user8@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
|  9 | user9 | user9@example.com | NULL              |          | NULL       | NULL           | 2021-07-29 00:00:00 | 2021-07-29 00:00:00 |
+----+-------+-------------------+-------------------+----------+------------+----------------+---------------------+---------------------+
9 rows in set (0.00 sec)
MariaDB [testdb]> SELECT * FROM users LIMIT 18446744073709551616;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '18446744073709551616' at line 1

OFFSET 句の場合

MariaDB [testdb]> SELECT * FROM users LIMIT 1 OFFSET 18446744073709551615;
Empty set (0.00 sec)
MariaDB [testdb]> SELECT * FROM users LIMIT 1 OFFSET 18446744073709551616;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '18446744073709551616' at line 1

結論

符号なし BIGINT の最大値「18446744073709551615」まで指定可能。

直接的な公式ドキュメントの記載は発見できなかったが、以下の情報を集約する限り間違っていないかと思われる。

特定のオフセットから結果セットの最後までのすべての行を取得するために、2 番目のパラメータにある程度大きい数字を使用できます。 次のステートメントは、96 行目から最後の行までのすべての行を取得します。
SELECT * FROM tbl LIMIT 95,18446744073709551615;

SELECT に LIMIT 句がある場合、LIMIT が sql_select_limit の値に優先されます。

MariaDB [testdb]> SHOW variables LIKE 'sql_select_limit';
+------------------+----------------------+
| Variable_name    | Value                |
+------------------+----------------------+
| sql_select_limit | 18446744073709551615 |
+------------------+----------------------+
1 row in set (0.00 sec)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0