简体   繁体   中英

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.

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

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

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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM