Get total number of rows while using limit clause - sql

I am querying my table to achieve pagination but I do not know the total number of rows in the table.
select name from table where id = 1 limit 0, 10
Is there a way to find out the total number of rows that would have returned if I had not used limit clause without querying for total count.

SQLite computes results on the fly when they are actually needed.
The only way to get the total count is to run the actual query (or better, SELECT COUNT(*)) without the LIMIT.

Depends on which back end technology you are using. In PHP, mysql_num_rows() returns number of rows without actually fetching the data.

Related

What is the maximum number of records that can be retrieved by postgreSQL?

When retrieving records using SELECT in postgres, what is maximum number of records that can be retrieved? Can it return a maximum of 10 million records at a single time if I wanted it too? Or can it only return a specific amount (ex. 5,000) in order to encourage using LIMIT and OFFSET?
There is no limit to the number of result rows in a query.
If your client collects the whole result in memory, that poses a limit, but most clients can be configured not to do that.

using SQL COUNT function or executing search query directly which is more efficient

Let's say i have a very big database , if i execute a search query directly then count the returned rows would it be more faster ? Or using COUNT(searchquery) then start executing query like ->
SELECT *
FROM TABLE
WHERE bla='blabla'
OFFSET 0 FETCH NEXT 20 ROWS ONLY
I searched for it but i couldn't find any solution.
Do the count in the database! It will be much faster.
First, a count(*) only returns one row and one value. That is much, much less data -- and much faster -- than returning all the rows.
Second, a count(*) does not reference any columns in the select, so the query can be better optimized. It might be possible to get the count without ever looking at the data pages.
It looks like you are doing paging. You need the total count to do display the total count and calculate the total number of pages to the user, yes?
Than Gordon's answer is the one to use.

row num gives zero records

I need to get the total no of records in a table for pagination purpose..it has some around 1 million records...the count is calculated as part of page load..i can do count query but it takes little more time thus increasing the time to page load.So to avoid that i have used
select ROW_NUMS,OWNER from ALL_TABLES where table_name='table1'
But the problem is that this query for some tables gives result as zero and also sometimes the count is not correct(different from count query)
any idea how to update the all_tables data for a table and how all_table
If You want the exact number of records in a table, You need to select count(*). This result will be correct. NUM_ROWS has never been to provide the exact number of rows.
You can gather the table, but this will give you the estimate count.
exec dbms_stats.gather_table_stats('<OWNER>', '<TABLE_NAME>');
To get an accurate row count you can get the database to do the heavy lifting before it sends the data to the client, e.g.:
SELECT t.*
,COUNT(*) OVER () AS row_count
FROM my_table t;
You need to run ANALYZE statement to properly update that column

How do I avoid multiple query for Pagination count and data retreival?

I have a scenario where I get a count and then pass the count as a variable to a similar query to get the paginated records. So basically I am doing a full query to get all the count by internally creating the full table and then using that count to display the same table with 10 per page. What solutions do I have to avoid this sort of multiple query?
Something like this is a Pseudo language .
select count {big table}
select big table where records are between count and count+10
Is there a sensible way to get the COUNT variable in the same query?
I am wondering how would Google handle a search, would it first find all the records or just fetch the records without tracking the no: of pages? Page numbers can't be computed prior as it is dependent on the variable sent by the user.
Edit: I have a similar question here https://dba.stackexchange.com/questions/161586/including-count-of-a-result-in-the-main-query
Regarding Google, they are likely to generate only the requested amount of results (like 10) and to estimate the count. The estimated count is very imprecise.
You can't have SQL Server count all results and get only a subset of them. There 3 strategies to deal with this:
execute a counting and a data query
execute an unlimited data query and discard all but ten results on the client
execute an unlimited data query into a temp-table whose primary key is the row number. You can then count instantly (get the last row) and select any subset by rownumber with a single seek
Counting the data can be significantly cheaper because SQL Server can use different indexes or discard joins.

SQL: Counting number of matching results when using a LIMIT query

Say the users table in my MySQL DB contains a very large number of entries.
I need to go through all the users, but I want to do it only chunks at a time (i.e. using LIMIT and OFFSET):
SELECT * FROM users LIMIT 100 OFFSET 200
Is it possible to know the total number of users matched in the query, but only return a LIMIT number of them ?
In other words, is it possible for me to know in advance the total number of users there are, without making a separate query?
You can do it in (almost) one query, using SQL_CALC_FOUND_ROWS and FOUND_ROWS():
SELECT SQL_CALC_FOUND_ROWS * FROM users LIMIT 100 OFFSET 200;
SELECT FOUND_ROWS();
While you still end up with two result sets, the actual query is only executed once, which saves you from repetitive coding and possible some wasted CPU cycles.
Unfortunately no. You need to do two queries : one to fetch the total number of users, the other to fetch a single page of users.
select count(*) from users;
select * from users limit 0,10;
It is not possible in SQL standard. I do not know mysql much, but I would assume it is not possible even in any SQL extension.