N
N
Nikita Markelov2014-04-12 12:51:11
MySQL
Nikita Markelov, 2014-04-12 12:51:11

MySQL Data Limit

Task: get data from MySQL with a certain limit and offset, find out the number of all elements in the table (that is, without applying limit and offset to it).
The following solution comes to mind: we get all the data without taking into account limit and offset, then, without SQL, we count them and cut them off.
You can, of course, specify limit and offset in the SQL select query, but then you will have to call a separate SQL query to count all elements.
What will be the fastest?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2014-04-12
@Entii

Two requests should be made: for data fetching, a request with limit and offset, for quantity - a request without limit and offset, but with the count function (no need to pull data, only count).

D
Dmitry T., 2014-04-12
@tyzhnenko

Use SQL_CALC_FOUND_ROWS when selecting with a limit, MySQL will count all records matching the parameters.

SELECT SQL_CALC_FOUND_ROWS * FROM TABLE1 WHERE .... LIMIT 40;
SELECT FOUND_ROWS()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question