File: queries-limit.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (61 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>7.6.LIMIT and OFFSET</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="queries.html" title="Chapter7.Queries">
<link rel="prev" href="queries-order.html" title="7.5.Sorting Rows">
<link rel="next" href="datatype.html" title="Chapter8.Data Types">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="queries-limit"></a>7.6.<code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></h2></div></div></div>
<a name="id583356"></a><a name="id583367"></a><p>   <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> allow you to retrieve just
   a portion of the rows that are generated by the rest of the query:
</p>
<pre class="synopsis">SELECT <em class="replaceable"><code>select_list</code></em>
    FROM <em class="replaceable"><code>table_expression</code></em>
    [<span class="optional">LIMIT { <em class="replaceable"><code>number</code></em> | ALL }</span>] [<span class="optional">OFFSET <em class="replaceable"><code>number</code></em></span>]</pre>
<p>
  </p>
<p>   If a limit count is given, no more than that many rows will be
   returned (but possibly less, if the query itself yields less rows).
   <code class="literal">LIMIT ALL</code> is the same as omitting the <code class="literal">LIMIT</code>
   clause.
  </p>
<p>   <code class="literal">OFFSET</code> says to skip that many rows before beginning to
   return rows.  <code class="literal">OFFSET 0</code> is the same as
   omitting the <code class="literal">OFFSET</code> clause.  If both <code class="literal">OFFSET</code>
   and <code class="literal">LIMIT</code> appear, then <code class="literal">OFFSET</code> rows are
   skipped before starting to count the <code class="literal">LIMIT</code> rows that
   are returned.
  </p>
<p>   When using <code class="literal">LIMIT</code>, it is important to use an
   <code class="literal">ORDER BY</code> clause that constrains the result rows into a
   unique order.  Otherwise you will get an unpredictable subset of
   the query's rows. You may be asking for the tenth through
   twentieth rows, but tenth through twentieth in what ordering?  The
   ordering is unknown, unless you specified <code class="literal">ORDER BY</code>.
  </p>
<p>   The query optimizer takes <code class="literal">LIMIT</code> into account when
   generating a query plan, so you are very likely to get different
   plans (yielding different row orders) depending on what you give
   for <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>.  Thus, using
   different <code class="literal">LIMIT</code>/<code class="literal">OFFSET</code> values to select
   different subsets of a query result <span class="emphasis"><em>will give
   inconsistent results</em></span> unless you enforce a predictable
   result ordering with <code class="literal">ORDER BY</code>.  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
   <code class="literal">ORDER BY</code> is used to constrain the order.
  </p>
<p>   The rows skipped by an <code class="literal">OFFSET</code> clause still have to be
   computed inside the server; therefore a large <code class="literal">OFFSET</code>
   can be inefficient.
  </p>
</div></body>
</html>