SQLite Forum

How to rerank one row in a big set of rows?
Login
## The situation

Suppose there is a big dataset (say 200,000 items). 

Each item requires a specified unique rank (from 1 to 200,000)

Need to update some item's rank frequently. 

## Temporary solution and problem

Store all items in a table: 

```sqlite
CREATE TABLE dataset (
    id     INTEGER PRIMARY KEY,
    item   TEXT
);
```

Each row stores one item, and make the `id` as it's rank. 

But when updating one item's rank, say from `m` to `n`, I need to alter all the `id`s between 'm' and `n`. The time complexity is O(m-n). 

Say the m is 100000 and the n is 100, then updating one item's rand requires updating 99900 `id`s, making that kind of rank update 1000 times requires updating 99900000 `id`s. That is insane. 

Is there a more efficient way to organize this kind of dataset? 

- Each item requires a specified unique and continuous rank, which can be used for ordering. 
- The item's rank often need to update.