> [!NOTE] Limit > <table> > <tr> > <td width="25%"><img src="assets/ex_limit.png"></td> > <td>A simple but effective optimization. The engine stops processing and returns the result set as soon as the requested number of rows has been emitted. This is most effective when combined with an <code>ORDER BY</code> and a supporting index. (Analogized by a proud raccoon wearing a tollkeeper cap, standing next to a bamboo slide, blowing a whistle and beckoning with his hand to stop the flow of packages once he has exactly five stacked up).</td> > </tr> > </table> > > ```sql > -- Restricting the result set to 10 rows > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT * FROM animals LIMIT 10; > ``` > > ```text > Limit (cost=1.00..1.17 rows=10 width=27) (actual time=1.002..1.003 rows=10 loops=1) > Output: id, name, species_id, created_at > Buffers: shared hit=1 > -> Seq Scan on public.animals (...) > ``` > > <table> > <tr> > <td rowspan="5" width="25%"><img src="assets/ex_limit.svg"></td> > <td><b>Performance</b></td><td>High performance; short-circuits the child node once the limit is reached.</td> > </tr> > <tr><td><b>Factors</b></td><td>The position of the LIMIT in the plan (earlier is usually better).</td></tr> > <tr><td><b>Cost</b></td><td>A fractional cost based on the percentage of the input plan scanned.</td></tr> > <tr><td><b>Operates on</b></td><td><a href="Structures/Result Set">Result Set</a></td></tr> > <tr><td><b>Workloads</b></td><td><a href="Workloads/LWLock/Buffers/BufferContent">LWLock: BufferContent</a></td></tr> > <tr><td colspan="3"><b>Description</b>: Restricts the number of rows returned. (Analogized by a proud raccoon wearing a tollkeeper cap, standing next to a bamboo slide, blowing a whistle and beckoning with his hand to stop the flow of packages once he has exactly five stacked up).</td></tr> > </table>