Optimizing queries for the next and previous element

前端 未结 11 915
耶瑟儿~
耶瑟儿~ 2021-01-30 11:33

I am looking for the best way to retrieve the next and previous records of a record without running a full query. I have a fully implemented solution in place, and would like to

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 11:53

    There are as many ways to do this as to skin the proverbial cat. So here are a couple of mine.

    If your original query is expensive, which you say it is, then create another table possibly a memory table populating it with the results of your expensive and seldom run main query.

    This second table could then be queried on every view and the sorting is as simple as setting the appropriate sort order.

    As is required repopulate the second table with results from the first table, thus keeping the data fresh, but minimising the use of the expensive query.

    Alternately, If you want to avoid even connecting to the db then you could store all the data in a php array and store it using memcached. this would be very fast and provided your lists weren't too huge would be resource efficient. and can be easily sorted.

    DC

提交回复
热议问题