I have a very big list of element in my non-sql database.
Every element has a sort order from 1 to N. This sort order specifies how the results appear on the forms.
When in the UI triggers a change of order (put element i in position j) I need to update all the entities between. If the element 1 becomes the lastest, I need to make N updates.
Is there an efficient way to make this operation less costly? Is there an smart way to index the sort value?
Some considerations:
- I’m re-designing my application so I can afford to reindex the entities with a smarter solution.
- The cost of a write (update) is +-4 times bigger than a fetch (read 1 entity).
- The list is big, does not fit in memory.
Every time a user moves an entity to a new position, assign it a new order property between the other two entities:
entityA.setOrder((entityB.getOrder() + entityC.getOrder())/2);
Save entity A (property “order” should be indexed).
When a user requests entities from 10000 to 10200, build a Query on your order property with a sort order. Retrieve results from 10000 to 10200:
datastore.prepare(q).asList(FetchOptions.Builder.withOffset(10000).limit(200));
Never reindex your entities again. Datastore does it for you every time you save an entity.