Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long should it take to build an index using ALTER TABLE in MySQL?

Tags:

indexing

mysql

This might be a bit like asking how long a length of string is, but the stats are:

  • Intel dual core 4GB RAM
  • Table with 8million rows, ~ 20 columns, mostly varchars with an auto_increment primary id
  • Query is: ALTER TABLE my_table ADD INDEX my_index (my_column);
  • my_column is varchar(200)
  • Storage is MyISAM

Order of magnitude, should it be 1 minute, 10 minutes, 100 minutes?

Thanks

Edit: OK it took 2 hours 37 minutes, compared to 0 hours 33 mins on a lesser spec machine, with essentially identical set ups. I've no idea why it took so much longer. The only possibility is that the prod machine HD is 85% full, with 100GB free. Should be enough, but i guess it depends on how that free space is distributed.

like image 509
Richard H Avatar asked Feb 15 '10 17:02

Richard H


People also ask

How long does it take to create an index in SQL?

If index is relatively wide - it can take from minutes to hours. if you have your DB on Enterprise Edition of Sql Server, than you can choose to create an index WITH ONLINE=ON - it will take a little bit more time and space to complete, but will not stop other processing with the table involved

What is the use of ALTER TABLE in MySQL?

MySQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column

Why does it take so long to create a table?

Things like total size of the table, data types, what the index was that you created. Those things can also impact the creation time. Run the script from the following link and post the results for the table in question. Also post things like the table definition and index definition please.

How long does an index rebuild take?

Currently, the index rebuild for this big table is taking 4 hours (half of the total time for full database index rebuild) I am trying to balance the cost and benefit here.


1 Answers

If you are just adding the single index, it should take about 10 minutes. However, it will take 100 minutes or more if you don't have that index file in memory.

Your 200 varchar with 8 million rows will take a maximum of 1.6GB, but with all of the indexing overhead it will take about 2-3 GB. But it will take less if most of the rows are less than 200 characters. (You might want to do a select sum(length(my_column)) to see how much space is required.)

You want to edit your /etc/mysql/my.cnf file. Play with these settings;

myisam_sort_buffer_size = 100M
sort_buffer_size = 100M

Good luck.

like image 165
vy32 Avatar answered Oct 13 '22 12:10

vy32