Optimizing queries for the next and previous element

前端 未结 11 930
耶瑟儿~
耶瑟儿~ 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 12:07

    Basic assumptions:

    • Specials are weekly
    • We can expect the site to change infrequently... probably daily?
    • We can control updates to the database with ether an API or respond via triggers

    If the site changes on a daily basis, I suggest that all the pages are statically generated overnight. One query for each sort-order iterates through and makes all the related pages. Even if there are dynamic elements, odds are that you can address them by including the static page elements. This would provide optimal page service and no database load. In fact, you could possibly generate separate pages and prev / next elements that are included into the pages. This may be crazier with 200 ways to sort, but with 3 I'm a big fan of it.

    ?sort=price
    include(/sorts/$sort/tomatoes_class_1)
    /*tomatoes_class_1 is probably a numeric id; sanitize your sort key... use numerics?*/
    

    If for some reason this isn't feasible, I'd resort to memorization. Memcache is popular for this sort of thing (pun!). When something is pushed to the database, you can issue a trigger to update your cache with the correct values. Do this in the same way you would if as if your updated item existed in 3 linked lists -- relink as appropriate (this.next.prev = this.prev, etc). From that, as long as your cache doesn't overfill, you'll be pulling simple values from memory in a primary key fashion.

    This method will take some extra coding on the select and update / insert methods, but it should be fairly minimal. In the end, you'll be looking up [id of tomatoes class 1].price.next. If that key is in your cache, golden. If not, insert into cache and display.

    • Do you think this is a good practice to find out the neighboring records for varying query orders? Yes. It is wise to perform look-aheads on expected upcoming requests.
    • Do you know better practices in terms of performance and simplicity? Do you know something that makes this completely obsolete? Hopefully the above
    • In programming theory, is there a name for this problem? Optimization?
    • Is the name "Sorting cache" is appropriate and understandable for this technique? I'm not sure of a specific appropriate name. It is caching, it is a cache of sorts, but I'm not sure that telling me you have a "sorting cache" would convey instant understanding.
    • Are there any recognized, common patterns to solve this problem? What are they called? Caching?

    Sorry my tailing answers are kind of useless, but I think my narrative solutions should be quite useful.

提交回复
热议问题