Question

Is it any faster to add or drop multiple columns in one query, rather than executing a query for each column? For example, is this:

ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;

any faster than this?

ALTER TABLE t2 DROP COLUMN c;
ALTER TABLE t2 DROP COLUMN d;
Était-ce utile?

La solution 2

Yes, it's faster. You only have to make one call to the database API, and it only has to parse the query once.

However, for ALTER TABLE queries, performance usually isn't a concern. You shouldn't be doing this frequently, only when you redesign your schema.

But if your question were about UPDATE queries, for instance, it would probably be significant. E.g. you should do:

UPDATE table
SET col1 = foo, col2 = bar
WHERE <condition>;

rather than

UPDATE table
SET col1 = foo
WHERE <condition>;

UPDATE table
SET col2 = bar
WHERE <condition>;

Autres conseils

Yes, it should be faster to run a single ALTER TABLE statement than two.

In my experience (with InnoDB on 5.1 and 5.5), MySQL doesn't seem to modify the table "in place". MySQL actually creates a new table, as copy of the old table with the specified modifications.

Two separate statements would require MySQL to do that copy operation twice.

With a single statement, you give MySQL the opportunity to make all the changes with just one copy operation. (I don't know the details of the MySQL internals, but it's possible that MySQL actually does the copy two times.)

Other database engines (MyISAM et al.) may get processed differently.


I believe the InnoDB plugin and/or newer versions of InnoDB in the MySQL (>5.5) have some algorithms other than the "copy" method, at least for some changes, which allow for the table to still be available while the ALTER TABLE is running (for read queries). But I don't know all the details.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top