Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Optimization for dropping multiple columns in MySQL

#3275

Dropping multiple columns on a large table in MySQL (at least version 5.0) can be slow proportional to the number of columns being dropped. That can be a drag on a large production database:

...MySQL, for almost all ALTER TABLE commands, copies the entire table into a temporary table, then renames the new table and drops the old one. ... The only type of ALTER TABLE call that doesn’t copy the entire table is a table rename.

(from http://labs.animoto.com/2008/08/06/activerecord-column-transformati...)

MySQL offers an extension to SQL that allows multiple columns to be dropped at once, so at least you only have to go through that once:

You can issue multiple ADD, ALTER, DROP, and CHANGE clauses in a single ALTER TABLE statement, separated by commas. This is a MySQL extension to standard SQL, which allows only one of each clause per ALTER TABLE statement. For example, to drop multiple columns in a single statement, do this:

ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

(from http://dev.mysql.com/doc/refman/5.0/en/alter-table.html)

Here's a patch that overrides MysqlAdapter#remove_column to issue multiple drops in a single statement.

I'm assuming this doesn't need additional tests since it's an implementation detail optimization of a feature that's already covered by tests but I'm happy to write some if someone has an idea about what to write.

The patch was applied to Rails 3.0 but the same diff should apply to Rails 2.3 as well.

Reported by Jordan Brough · September 27th, 2009 @ 01:46 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from activerecord, optimization to activerecord, bugmash, optimization

    September 27th, 2009 @ 11:25 AM

  2. Elomar França
    Elomar França

    +1, verified.

    I think that optimizations like this (tiny but significative) are always welcome. The patch applies on 2-3-stable and master.

    September 27th, 2009 @ 05:53 PM

  3. sr.iniv.t
    sr.iniv.t

    +1 verified.

    The patch applies cleanly on 2-3-stable and master. But the patch lacks tests.

    September 27th, 2009 @ 06:35 PM

  4. Jordan Brough
    Jordan Brough

    What would new tests look like? Verify that execute was only called once for multiple drops? Wouldn't that be getting a little bit too concerned with the 'how' instead of the 'what'? Functionality-wise it seems to be covered pretty well already but I'm happy to add some tests if there's some ideas on what tests to add.

    September 28th, 2009 @ 02:48 PM

  5. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, bugmash, optimization to activerecord, optimization

    February 12th, 2010 @ 12:46 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 9th, 2011 @ 12:31 AM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 9th, 2011 @ 12:31 AM

  8. bingbing