Home >Database >Mysql Tutorial >How Can I Efficiently Determine the Total Row Count Before Applying LIMIT in PostgreSQL Queries?
Database data paging usually requires determining the total number of pages to render the paging control. Typically, this requires running two separate queries: one using COUNT() to get the total, and another using LIMIT to retrieve the current page's data.
This method is inefficient. Fortunately, there is a better way in PostgreSQL to get the total before applying LIMIT: use window functions.
Window functions introduced in PostgreSQL 8.4 allow us to perform calculations on a data set defined by a "window". By specifying a suitable window, we can retrieve the total without affecting the LIMIT operation.
Consider the following query:
<code class="language-sql">SELECT foo, COUNT(*) OVER() AS full_count FROM bar WHERE <some condition=""> ORDER BY <some col=""> LIMIT <pagesize> OFFSET <offset>;</code>
Here, full_count
provides the total number of rows before LIMIT is applied.
Note: Using window functions in this way can be computationally more expensive than the traditional two-query approach. This is because all rows must be counted regardless of paging parameters.
In some cases there is no need to get the total before LIMIT. An alternative is:
The above is the detailed content of How Can I Efficiently Determine the Total Row Count Before Applying LIMIT in PostgreSQL Queries?. For more information, please follow other related articles on the PHP Chinese website!