~ubuntu-branches/debian/squeeze/pgadmin3/squeeze

« back to all changes in this revision

Viewing changes to docs/en_US/pg/queries-limit.html

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Porcheron
  • Date: 2008-02-07 00:56:22 UTC
  • mto: (2.1.6 hardy) (6.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080207005622-c2ail8p4d0sk3dnw
Tags: upstream-1.8.2
Import upstream version 1.8.2

Show diffs side-by-side

added added

removed removed

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