バージョンごとのドキュメント一覧

7.6. LIMITOFFSET #

<title><literal>LIMIT</literal> and <literal>OFFSET</literal></title>

<literal>LIMIT</literal> and <literal>OFFSET</literal> allow you to retrieve just a portion of the rows that are generated by the rest of the query: LIMITおよびOFFSETを使うことで、問い合わせの実行で生成された行の一部だけを取り出すことができます。

SELECT select_list
    FROM table_expression
    [ ORDER BY ... ]
    [ LIMIT { number | ALL } ] [ OFFSET number ]

If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the query itself yields fewer rows). <literal>LIMIT ALL</literal> is the same as omitting the <literal>LIMIT</literal> clause, as is <literal>LIMIT</literal> with a NULL argument. 限度(limit)数を指定すると、指定した行数より多くの行が返されることはありません(しかし、問い合わせの結果が指定した行数より少なければ、それより少なくなります)。 LIMIT ALLは、LIMIT句を省略した場合と同じです。LIMITの引数がNULLの場合も同様です。

<literal>OFFSET</literal> says to skip that many rows before beginning to return rows. <literal>OFFSET 0</literal> is the same as omitting the <literal>OFFSET</literal> clause, as is <literal>OFFSET</literal> with a NULL argument. OFFSETは、行を返し始める前に飛ばす行数を指定します。 OFFSET 0は、OFFSET句を省略した場合と同じです。OFFSETの引数がNULLの場合も同様です。

If both <literal>OFFSET</literal> and <literal>LIMIT</literal> appear, then <literal>OFFSET</literal> rows are skipped before starting to count the <literal>LIMIT</literal> rows that are returned. OFFSETおよびLIMITの両者が指定された場合、OFFSET分の行を飛ばしてから、返されるLIMIT行を数え始めます。

When using <literal>LIMIT</literal>, it is important to use an <literal>ORDER BY</literal> clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is unknown, unless you specified <literal>ORDER BY</literal>. LIMITを使用する時は、結果の行を一意な順序に制御するORDER BY句を使用することが重要です。 ORDER BYを使わなければ、問い合わせの行について予測不能な部分集合を得ることになるでしょう。 10番目から20番目の行を問い合わせることもあるでしょうが、どういう並び順での10番目から20番目の行でしょうか? ORDER BYを指定しなければ、並び順はわかりません。

The query optimizer takes <literal>LIMIT</literal> into account when generating query plans, so you are very likely to get different plans (yielding different row orders) depending on what you give for <literal>LIMIT</literal> and <literal>OFFSET</literal>. Thus, using different <literal>LIMIT</literal>/<literal>OFFSET</literal> values to select different subsets of a query result <emphasis>will give inconsistent results</emphasis> unless you enforce a predictable result ordering with <literal>ORDER BY</literal>. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless <literal>ORDER BY</literal> is used to constrain the order. 問い合わせオプティマイザは、問い合わせ計画を生成する時にLIMITを考慮します。 そのため、LIMITOFFSETに指定した値によって、異なった計画(得られる行の順序も異なります)が得られる可能性が高いです。 従って、1つの問い合わせ結果から異なる部分集合を選び出すために、異なるLIMIT/OFFSETの値を使用すると、ORDER BYで結果の順序を制御しなければ、一貫しない結果が生じるでしょう。 これは不具合ではありません。 ORDER BYを使って順序を制御しない限り、SQLは必ずしも特定の順序で問い合わせの結果を渡さないという特性の必然的な結果です。

The rows skipped by an <literal>OFFSET</literal> clause still have to be computed inside the server; therefore a large <literal>OFFSET</literal> might be inefficient. OFFSET句で飛ばされる行を、実際にはサーバ内で計算しなければなりません。 そのため、大きな値のOFFSETは非効率的になることがあります。