Peter Craig
Peter Craig

Reputation: 7289

Updating display order of multiple MySQL rows in one or very few queries

I have a table with say 20 rows each with a number for display order (1-20).

SELECT * FROM `mytable` ORDER BY `display_order` DESC;

From an admin area you can drag the rows around or type a new number manually for each row.

Surely it's not good to loop over an UPDATE query for every row, what's an alternative in one or very few queries suitable for updating one cell in 20 rows or even more, 50-200+?


Edit: A lot of good responses and ideas. I might expand on the ideas I've considered so far:

One array string: I could have the order in a string listing the unique row IDs in the order I want - eg rows 1,9,2,6,23. When the order is updated, a hidden field updates with JavaScript and adds that to the database or a text file when complete:

UPDATE `my_dispaly_order_table` SET `display_order`='1,9,2,6,23';

Update each row individually: This is what I was trying to avoid but it would only be changed very infrequently so 20-30 calls in one hit once a week or month might not be a problem so simply calling UPDATE on each row is what I usually do:

UPDATE `mytable` SET `display_order`='1' WHERE `rowId` = 1;
UPDATE `mytable` SET `display_order`='2' WHERE `rowId` = 9;
UPDATE `mytable` SET `display_order`='3' WHERE `rowId` = 2;
UPDATE `mytable` SET `display_order`='4' WHERE `rowId` = 6;
UPDATE `mytable` SET `display_order`='5' WHERE `rowId` = 23;

Upvotes: 19

Views: 14638

Answers (11)

Jamon Holmgren
Jamon Holmgren

Reputation: 24392

soulmerge's answer made me think and I think this is a better solution. What you need to do is select the rows with the id using IN() and then use CASE to set the value.

UPDATE mytable SET display_order =
  CASE id
    WHEN 10 THEN 1
    WHEN 23 THEN 2
    WHEN 4 THEN 3
  END CASE
WHERE id IN (10, 23, 4)

I have a need for this in my current app. In PHP, I'm getting a serialized (and ordered) set of id's from jQuery UI's built-in Sortable feature. So the array looks like this:

$new_order = array(4, 2, 99, 15, 32); // etc

To generate the single MySQL update query, I do this:

$query = "UPDATE mytable SET display_order = (CASE id ";
foreach($new_order as $sort => $id) {
  $query .= " WHEN {$id} THEN {$sort}";
}
$query .= " END CASE) WHERE id IN (" . implode(",", $new_order) . ")";

The "implode" at the end just gives me my ID list for IN(). This works beautifully for me.

Upvotes: 16

fela
fela

Reputation: 1042

A little late, but it may be useful to someone else:

UPDATE mytable SET display_order = FIND_IN_SET(rowId, '1,9,2,6,23') WHERE rowId in (1,9,2,6,23)

Upvotes: 9

Tiago Sintra
Tiago Sintra

Reputation: 31

This is a great way of sorting items on a database. Exactly what i was looking for ;)

Here's an improved version of Jamon Holmgren's version. This function is totally dynamic:

function reOrderRows($tablename, $ordercol, $idsarray){

    $query = "UPDATE $tablename SET $ordercol = (CASE $ordercol ";
    foreach($idsarray as $prev => $new) {
      $query .= " WHEN $prev THEN $new\n";
    }
    $query .= " END) WHERE $ordercol IN (" . implode(",", array_keys($idsarray)) . ")";

    mysql_query($query);
}

This assumes that you have a column $ordercol on your table $tablename just for ordering purposes.

The $idsarray is an array in the format array("current_order_num" => "updated_order_num").

So if you just want to swap two lines in your table (imagine that you have for example a "move up" and "move down" icons on your webpage), you can use this function on top of the previous one:

function swapRows($tablename, $ordercol, $firstid, $secondid){
    $swaparray = array("$firstid" => "$secondid", "$secondid" => "$firstid");
    reOrderRows($tablename, $ordercol, $swaparray); 
}

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425491

If you need to drag you rows, this is a good implementation for a linked list.

Having your rows ordered with a linked list means that you will update at most 3 rows at a time -- even if you move the whole block (as long as it's contiguous).

Create a table of your rows like this:

CREATE TABLE t_list (
        id INT NOT NULL PRIMARY KEY,
        parent INT NOT NULL,
        value VARCHAR(50) NOT NULL,
        /* Don't forget to create an index on PARENT */
        KEY ix_list_parent ON (parent)
)

id   parent  value

1    0       Value1
2    3       Value2
3    4       Value3
4    1       Value4

and use this MySQL query to select the rows in order:

SELECT  @r := (
        SELECT  id
        FROM    t_list
        WHERE   parent = @r
        ) AS id
FROM    (
        SELECT  @r := 0
        ) vars,
        t_list

This will traverse your linked list and return the ordered items:

id   parent  value

1    0       Value1
4    1       Value4
3    4       Value3
2    3       Value2

To move a row, you'll need to update the parent of the row, the parent of its current child, and the parent of the row you're inserting before.

See this series of articles in my blog on how to do it efficiently in MySQL:

There are lots of articles because there are some issues with row locking which should be handled slightly differently for each case.

Upvotes: 2

Eduardo Molteni
Eduardo Molteni

Reputation: 39423

I'm thinking about this problem, and the solution I came up is having a decimal number as order and change the number of the item change for a number between the next and the previous item

Order    Item
-----    ----
1        Original Item 1
2        Original Item 2
3        Original Item 3
4        Original Item 4
5        Original Item 5

If you change the item 4 to the 2nd position, you get:

Order    Item
-----    ----
1        Original Item 1
1.5      Original Item 4
2        Original Item 2
3        Original Item 3
5        Original Item 5

If you change the item 3 to the 3rd position, you get:

Order    Item
-----    ----
1        Original Item 1
1.5      Original Item 4
1.75     Original Item 3
2        Original Item 2
5        Original Item 5

Theoretically there is always a decimal between two decimals, but you could face some storage limits.

This way you only have to update the row being re-ordered.

Upvotes: 2

Csaba Kétszeri
Csaba Kétszeri

Reputation: 694

Collect the new order in a temporary variable and put a "save this order" button to the admin area. Then save the order for the rows with one round.

You'll get better response times, undoable changes, fewer modified rows (because in low level in the dbms, practically no updates used to be possible, but save a new instance of the whole row and delete the old one).

After all, it would be a lower cost solution for a whole reordering and would save some coding on the update optimization.

Upvotes: 0

soulmerge
soulmerge

Reputation: 75724

You should first ensure that the column has no UNIQUE index, otherwise mysql will tell you that the constraint is broken during the query. After that you can do things like:

-- Move #10 down (i.e. swap #10 and #11)
UPDATE mytable SET display_order =
  CASE display_order
    WHEN 10 THEN 11
    WHEN 11 THEN 10
  END CASE
WHERE display_order BETWEEN 10 AND 11;

-- Move #4 to #10
UPDATE mytable SET display_order
  CASE display_order
    WHEN 4 THEN 10
    ELSE display_order - 1
  END CASE
WHERE display_order BETWEEN 4 AND 10;

But you should actually ensure that you do things in single steps. swapping in two steps will result in broken numbering if not using ids. i.e.:

-- Swap in two steps will not work as demostrated here:

UPDATE mytable SET display_order = 10 WHERE display_order = 11;
-- Now you have two entries with display_order = 10

UPDATE mytable SET display_order = 11 WHERE display_order = 10;
-- Now you have two entries with display_order = 11 (both have been changed)

And here is a reference to the CASE statement of mysql.

Upvotes: 11

Greg
Greg

Reputation: 321698

You could delete an re-insert all the rows - that would do the whole operation in just two queries (or three if you need to select all the data). I wouldn't count on it being faster, and you'd have to do it inside a transaction or you'll be heading for your backups before too long. It could also lead to table fragmentation.

A better option might be to record each change as a history then do something like this:

Example, position 10 is moved down two to 12th

UPDATE table SET display_order = display_order -1 WHERE display_order BETWEEN 10 AND 12
UPDATE table SET display_order = 12 WHERE row_id = [id of what was row 10]

Upvotes: 1

Till
Till

Reputation: 22408

You could try to wrap it into a few statements, I don't think it's possible in a single one. So for example, let's say you are going to update the 10th row. You want every record after 10 to be bumped up.

UPDATE table SET col=col+1 WHERE col > 10
UPDATE table SET col=10 WHERE id = X
... 

But it's really tough to roll in all logic required. Because some records maybe need a decrement, etc.. You want to avoid duplicates, etc..

Think about this in terms of developer time vs. gain.

Because even if someone sorts this once per day, the overhead is minimal, compared to fixing it in a stored procedure, or pseudo-optimizing this feature so you don't run 20 queries. If this doesn't run 100 times a day, 20 queries are perfectly fine.

Upvotes: 2

tpdi
tpdi

Reputation: 35151

Add an id (or other key) to the table, and update where id (or key) = id (or key) of changed row.

Of course, you'll have to make sure that either there are no duplicate display_order values, or that you're OK with ties in display_order displaying in any order, or you'll introduce a second, tie-breaker to the order by list.

Upvotes: 0

Matt K
Matt K

Reputation: 13852

You could create a temporary table and populate with the primary key of the rows you want to change, and the new values of display_order for those rows, then use the multi-table version of UPDATE to update the table in one go. I wouldn't assume that this is faster, however, not without testing both approaches.

Upvotes: 0

Related Questions