سؤال

Let's say I have a table with a composite primary key consisting of 3 varchar(256) columns: A, B, and C. There's also an index on B, and the composite key acts as an index on A. This table has millions of rows and has lots of INSERTS and SELECTS, but no UPDATES (except for using ON DUPLICATE KEY UPDATE with INSERTS).

If I changed this table to have a single column char(32) primary key of md5(A,B,C) (as well as an index on A and an index on B) would this setup be more efficient for INSERTS? The possibility for collisions doesn't bother me.

MySQL 5.5, using InnoDB

هل كانت مفيدة؟

المحلول

In InnoDB the primary key is a clustered key - the data on the disk is physically organized based on the order of the PK. This means that if you are not using auto-increment or something other that increases the value of PK over time, you will got a lot of random writes and this will kill your performance as soon as the size of the table is larger than the buffer_pool size.

Having in mind this, md5() won't be much better than the compound index for the inserts. Having smaller PK is good for 2 reasons: the size of the PK affects the size of all secondary indexes (because all secondary indexes contain the PK) and using smaller indexes is better ('cause you can load more values into memory at once)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top