Rey Mark Abao
Rey Mark Abao

Reputation: 61

limiting the Index size of specific DB column in mysql

Good Day,

I have concern about indexing in mysql. I am trying to limit the Index size of specific DB table column which column names like ###ID.

this seems that the ID looks unique in first 8 bytes instead of entire length.

Thanks in advance.

Upvotes: 4

Views: 3413

Answers (2)

Shadow
Shadow

Reputation: 34231

As mysql documentation on creating indexes describes:

For string columns, indexes can be created that use only the leading part of column values, using col_name(length) syntax to specify an index prefix length.

  • Prefixes can be specified for CHAR, VARCHAR, BINARY, and VARBINARY column indexes.

  • Prefixes must be specified for BLOB and TEXT column indexes.

  • Prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements is interpreted as number of characters for nonbinary string types (CHAR, VARCHAR, TEXT) and number of bytes for binary string types (BINARY, VARBINARY, BLOB). Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.

  • For spatial columns, prefix values cannot be given, as described later in this section.

The statement shown here creates an index using the first 10 characters of the name column (assuming that name has a nonbinary string type):

CREATE INDEX part_of_name ON customer (name(10));

Upvotes: 2

Bernd Buffen
Bernd Buffen

Reputation: 15057

You can set the size after the field name. You must remove the old key first

ALTER TABLE mytab
ADD  KEY `parent` (`parent`(8)) ;

Upvotes: 0

Related Questions