views:

84

answers:

3

Hello Experts,

what is the performance characteristic for Unique Indexes in Mysql and Indexes in general in MySQl (like the Primary Key Index):

Given i will insert or update a record in my databse: Will the speed of updating the record (=building/updating the indexes) be different if the table has 10 Thousand records compared to 100 Million records. Or said differently, does the Index buildingtime after changing one row depend on the total indexsize?

Does this also apply for any other indexes in Mysql like the Primary Key index?

Thank you very much Tom

+1  A: 

Most indexes in MySQL are really the same internally -- they're B-tree data structures. As such, updating a B-tree index is an O(log n) operation. So it does cost more as the number of entries in the index increases, but not badly.

In general, the benefit you get from an index far outweighs the cost of updating it.

Bill Karwin
Indeed it does, and @Tom: keep in mind: when using InnoDB tables there is a primary index wether you have one in your table definition or not, might as well be a visibile primary key.
Wrikken
With InnoDB, if you don't define a primary key, MySQL will actually use your first NOT NULL unique index as the primary key (and clustered index) before creating its own clustered index. http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html
Marcus Adams
+1  A: 

A typical MySQL implementation of an index is as a set of sorted values (not sure if any storage engine uses different strategies, but I believe this holds for the popular ones) -- therefore, updating the index inevitably takes longer as it grows. However, the slow-down need not be all that bad -- locating a key in a sorted index of N keys is O(log N), and it's possible (though not trivial) to make the update O(1) (at least in the amortized sense) after the finding. So, if you square the number of records as in your example, and you pick a storage engine with highly optimized implementation, you could reasonably hope for the index update to take only twice as long on the big table as it did on the small table.

Alex Martelli
A: 

Note that if new primary key values are always larger than the previous (i.e. autoincrement integer field), your index will not need to be rebuilt.

Marcus Adams