php - sorting in mysql VS already sorted array -


i have mysql table 10000 entries. want represent these entries sorted name. page should show 20 entries @ time.

my question is, more efficient

  • let database sort it, means load corresponding 20 entries using sorting, limit, select query.

or should 1 rather

  • sort list of entries once, save them in array in file, load file , @ 20 indices of interest.

both seems terrible me. not want sort database 10000 entries each time user loads page show 20 entries, nor want load array more 10000 entries have access corresponding 20 entries.

remark: not asking is php sort better mysql "order by"? or database sort vs. programmatic java sort - want know if better presort database, save in array , load complete sorted array including entries.

it depends mean "better".

what makes things better? speed, simplicity, versatility?

what happens if save file , table updated? missing rows in file. can't guarantee storing rows in file going faster. mysql can quite @ caching if table isn't being updated much.

that being said, if speed important @ memcached or redis. both of these storage solutions key-pair data in data stored in memory. implement memcached:

function gettablerows() {     $memcached = & get_memcached_instance();     $result = $memcached->get("mytablerows");     if (! is_array($result)) {         $result = $this->model->fetchsortedrowsfromdb();         $memcached->put("mytablerows", $result);     }     return $result; } 

you can use required indexes pagination. keep in mind have delete cache every time table updated.

is of speed improvement required though? table gets more , more rows put more strain on php , may lead memory issues. can quite use limit , offset deal kind of thing , assuming tables indexed shouldn't give of performance hit.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -