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

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.

Related

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.

Different resource usage when using a LIMIT and SQL_CALC_FOUND_ROWS and without?

Is there a difference in resource usage if I write an SQL with a LIMIT and SQL_CALC_FOUND_ROWS and without it?
Like
SELECT id FROM table;
or
SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE id > 100 LIMIT 10;
So in the first one I get all the IDs, so I have to go through the whole database, to get the information but on the second one I only get 10 IDs but I need to go through the whole database to get my row count.

Get total number of rows while using limit clause

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.

Maximum number of rows a select statement can fetch

Is there any practical limit of number of rows a select statement can fetch from a database- any database?
Assume, I am running a query SELECT * FROM TableName and that table has more than 12,00,000 rows.
How many rows it can fetch? Is there a limit for this?
12000000 is not at all a big number I have worked with way bigger result sets. As long as your memory can fit the result you should have no problems.

When is LIMIT applied? Will it select all results before limiting?

I'm concerned about the performance of a query such as SELECT * FROM user LIMIT 5 on a very large user table. Will it select all records then limit to 5?
More specifically will the following query select all assetids before limiting...
SELECT * FROM assets WHERE asset_id IN(1,2,3,4,5,6,7,8,9,10) LIMIT 5
I realize it doesn't make sense to include all ids in the IN() clause if I'm limiting but I'd like to know how mysql behaves in this situation.
Thanks.
This depends on your query. See this page for more explanations of how LIMIT is applied:
http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html
For that specific query, the following would apply:
"As soon as MySQL has sent the required number of rows to the client, it aborts the query unless you are using SQL_CALC_FOUND_ROWS."
Hope that helps.
Your query will have to scan all rows by asset_id column, so you better have an index on it. In my experience, you would always want to set an order by clause also, since the result set will be internally (i.e. order unknown), and you would not know why the returned 5 results were the ones you actually wanted.