I’m using jQuery UI’s sortable list to allow the user to drag and change the sorting of a list.
I normally get back the ids of each item and the array key then update each row’s sort number in the database.
This is all fine and looking at examples it seems to be the normal way of doing it for most.
What I’d like to know is if running UPDATE queries for every single item is the most efficient way of storing the sorting order? The above works well for a few items but what if I have a list of say 200 items? It seems excessive to run an UPDATE query 200 times just to move item 2 up to the first position.
Does anyone have a more efficient way of doing this?
Thanks.
If you’re keeping track of the positions of each item, you should only have to update the positions of those items that have changed. IE, if you move item #19 to position 1, you’ve shifted items 1-18 up a position. you wouldn’t have to change the position of items 20-200.
Also, if you’re using .NET/SQL Server 2008, you could use a User defined table type (UDTT) and pass a data table as one of the parameters to your stored procedure that has a list of all items and their current list position, then do an update on the set of data rather than on each item individually. no RBAR 🙂