So I have a table of user favorites. There's a few million rows of them.
Currently, they have only three columns: id
(pk),userId
and someFkRef
. There's an index on userId
to allow me to select a user's favorites quickly.
Currently these are ordered by id
which is effectively just the insert order. We'd like to offer the user a chance to re-order their favorites, most likely via some sort of drag and drop interaction.
My first (and I suspect naive) approach to this would be to simply add an order
column and a composite index over userId
,order
. However, upon reflection, when the user moves their item some distance over the list, all intermediate rows between the item's start position and end position will need their order
column recalculated and therefore, the index too.
This is (most likely) bad.
Before I spend ages trying to quantify exactly how bad, I'm wondering if there's a better table-based representation that is cheaper to manipulate with the kinds of operations I describe above.
order by
ops require an index, no? – Phonetician