Is it useful to use PHP `mysql_data_seek` and code, rather than SQL` LIMIT`, to limit the results for pagination?

After talking at work about pagination methods and getting only the data needed for a particular page. Is it better to use PHP mysql_data_seek()in the returned dataset and use the code to restrict or use SQL LIMITto limit the results to pagination?

For example, we have a built-in function for pagination, but we must use two queries. First we query the DB to find out how many results are available, then we query db using the keyword LIMITto actually get the data for the corresponding page.

It would be better to get the entire data set and iterate over it with mysql_data_seek(), to go to the corresponding page data before displaying it. Thus, we can make one request for both needs, to see how much data is available, and then only get the page we need.

I assume the latter will use more memory and maybe be slower with large databases?

+3
source share
4 answers

An additional query is COUNTor SQL_CALC_FOUND_ROWSmore efficient than loading the entire table. Imagine you have billions of lines !

  • He will use the tape.
  • Extremely slow on large sets.
  • It is also not efficient PHP.
+4
source

. - :

  • : ( ). .
  • : , , .
  • : .

1 3 , MySQL . ( , 100 ), COUNT , . . , , , , .

2 . COUNT , , . , , , , , .. , , .

, , , , . , . (, APC), PHP .

+2

LIMIT - , . @Wesley, .

0

If you want to have ten ice creams, you ask for 100 and leave 90 sedentary melting on the counter, taking only ten? I do not think you will do it. This question is almost the same: it is more efficient by asking the database how many rows there are and selects only those that are really interesting to you, because the database is more efficient in restricting rows, and also no data should be sent to the client, and further processing will not be completed.

As always, with the question "what is faster": do a test and find out. My money is in the database.

0
source

All Articles