tags:

views:

39

answers:

2

I've just started to investigating how I should optimize my database. Indexing seems to be a good idea, so I want to index a VARCHAR column, the engine is MyISAM.

From what I've read, I understand that an index is limited to a size of 1000 bytes. A VARCHAR character is 3 bytes in size. Does this mean that if I want to index a VARCHAR column with 50 rows, I need an index prefix of 6 characters? I came to that number by dividing 1000 with the row number 50, then the bytesize per character that is 3. 1000/50/3=6,66.

It seems a little complicated, so I'm just wondering if I'm thinking right? It seems weird to me that you'd only be able to index 333 rows in a VARCHAR column, using a prefix of 1 character.

A: 

I think its meant the varchar width. If the index max size is 1000 bytes and a varchar is 3 bytes long then the max indexable size would be 1000/3.

oli
A: 

From what I've read, I understand that an index is limited to a size of 1000 bytes.

Index key length is limited to 1000 bytes in MyISAM, 767 bytes in InnoDB (per column).

This means that you cannot index a single UTF8 column more than 333 characters long in a MyISAM table (when calculating the max index size, MySQL assumes 3 bytes per character, though actual length may be much smaller)

You can create as many indexed records as you need.

Quassnoi
Oh, so the 1000 bytes restriction only applies to the column name?
Tommy
@Tommy: it applies to the maximum amount of characters you can store in an indexed column of a single record.
Quassnoi
Thanks, that clarifies it a little. So each record in the column I want to index can have 333 characters. What's the reason of using index prefix with less than 333 characters then? The example in mysql reference uses a prefix of 10 characters.
Tommy
@Tommy: it's faster. The index is shrunk in size and more records can fit into a single page. Sometimes `10` characters is enough to narrow down the search to a record or two which can be further filtered with a pure comparison.
Quassnoi