Optimizing queries for the next and previous element

前端 未结 11 932
耶瑟儿~
耶瑟儿~ 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:41

    The problem / datastructur is named bi-directional graph or you could say you've got several linked lists.

    If you think of it as a linked list, you could just add fields to the items table for every sorting and prev / next key. But the DB Person will kill you for that, it's like GOTO.

    If you think of it as a (bi-)directional graph, you go with Jessica's answer. The main problem there is that order updates are expensive operations.

     Item Next Prev
       A   B     -
       B   C     A
       C   D     B
       ...
    

    If you change one items position to the new order A, C, B, D, you will have to update 4 rows.

提交回复
热议问题