3

Some of my queries were taking way too long, including some alter table queries, so I pressed Ctrl-c to abort them and continue with other matters.

One that I aborted was alter table labs modify ID int(11) first.

Now quite simple queries like alter table labs drop column ID are taking forever. I've been waiting for over 25 minutes. There are millions of records, but I've run similar alter table queries on the same table that only take a few seconds.

Can aborting queries harm the column that the query was operating on? If so, how can I recover from the problem?


Update: I am nuking a primary-key column. Could that have something to do with the insanely long running time?

2 Answers 2

7

If the transaction wasn't completed, no. MySQL does ALTER TABLE by creating a new table and then updates the reference to the old table. You shouldn't have altered your original table if you interrupted the original ALTER TABLE query.

Source.

8
  • Okay, that's comforting. Would you have any idea why the alter table labs drop column ID is taking forever though? It's still running.
    – Dylan
    May 2, 2016 at 22:13
  • @Dylan a good number of reasons. Perhaps that column is indexed? Or is foreign-keyed, with triggers in other tables that react to being deleted? Maybe I/O is blocked by something else, compared to when you ran the other alter in the past?
    – Matt
    May 2, 2016 at 22:15
  • The column is primary-keyed, would that cause lots of delay? It isn't foreign-keyed.
    – Dylan
    May 2, 2016 at 22:17
  • 1
    I have to admit I've never nuked a primary-key column before! That seems like a bad idea, not sure of the performance implications there. I'll have to let someone else chime in though
    – Matt
    May 2, 2016 at 22:18
  • 3
    Oh, if the column is the PRIMARY KEY then I would suspect the ALTER TABLE will have some trouble as there is no longer an index to assist in the re-building of the table. It'd fall into a table scan or sorts which is rather painful.
    – mferly
    May 2, 2016 at 22:20
1

Short answer: Not necessarily.

If you're using InnoDB, which is backed by a transaction log for recovery and rollback purposes, then you can get away with a lot, especially in a non-production environment.

The easiest way to terminate a renegade query is to use the MySQL shell as the root user:

SHOW PROCESSLIST;

This will give you a list of the current connections and a process ID for each one. To terminate any given query, such as number 19, use:

KILL 19;

Usually this will undo and roll back the query. In some cases this is not sufficient and you may have to force-quit the MySQL server process with kill -9. Under most circumstances you should be able to restart the server right away, and the DB will be in the last fully committed state.

https://stackoverflow.com/a/3442027/4148092

1
  • Oops. Sorry I'll watch out for that next time :) May 2, 2016 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.