Database index difference

1、普通索引  mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` )

  The only task of an ordinary index (an index defined by the keywords KEY or INDEX) is to speed up access to data. Therefore, indexes should be created only for those columns that appear most frequently in the query condition (WHEREcolumn=) or the sort condition (ORDERBYcolumn). Whenever possible, you should choose a data column with the most tidy and compact data (such as an integer type data column) to create an index.

  2. Unique index mysql>ALTER TABLE `table_name` ADD UNIQUE ( `column` )

  Ordinary indexes allow the indexed data column to contain duplicate values. For example, because people may have the same name, the same name may appear twice or more in the same Employee Profile data table.

  If it is certain that a data column will only contain values ​​that are different from each other, the keyword UNIQUE should be used to define it as a unique index when creating an index for this data column. The advantages of doing this: First, it simplifies MySQL's management of this index, which makes the index more efficient; second, when a new record is inserted into the data table, MySQL will automatically check the value of this field in the new record Whether this field has already appeared in a record; if so, MySQL will refuse to insert that new record. That is, a unique index can guarantee the uniqueness of data records. In fact, in many cases, the purpose of creating a unique index is not to improve access speed, but to avoid data duplication.

  3、主索引 mysql>ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )

  It has been emphasized many times before: an index must be created for the primary key field, and this index is the so-called "primary index". The only difference between a primary index and a unique index is that the former uses the keyword PRIMARY instead of UNIQUE when it is defined.

  4. Foreign key index mysql>ALTER TABLE `table_name` ADD FULLTEXT ( `column`)

  If a foreign key constraint is defined for a foreign key field, MySQL will define an internal index to help itself manage and use the foreign key constraint in the most efficient way.

  5、复合索引 mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

  An index can cover multiple data columns, such as an index like INDEX(columnA, columnB). The characteristic of this index is that MySQL can selectively use one such index. If the query operation only needs to use one index on the columnA data column, you can use the composite index INDEX (columnA, columnB). However, this usage only applies to the combination of data columns that are ranked first in the composite index. For example, INDEX(A,B,C) can be used as an index on A or (A,B), but not as an index on B, C or (B,C).

  6. The length of the index

  When defining indexes for data columns of type CHAR and VARCHAR, you can limit the length of the index to a given number of characters (this number must be less than the maximum number of characters allowed for this field). The advantage of this is that it can generate an index file with a smaller size and a faster retrieval speed. In most applications, the string data in the database is dominated by various names. Setting the index length to 10~15 characters is enough to narrow the search range to a few data records. . When creating indexes for data columns of BLOB and TEXT types, the length of the index must be limited; the maximum index allowed by MySQL The ordinary index on the full-text index text field can only speed up the string that appears at the front of the field content ( That is, the characters at the beginning of the field content) for retrieval operations. If the field contains a larger piece of text consisting of several or even multiple words, a normal index is useless. This retrieval often comes in the form of , which is complicated for MySQL, and if the amount of data to be processed is large, the response time will be long.

  This is where full-text indexing comes into play. When generating this type of index, MySQL will create a list of all words that appear in the text, and query operations will use this list to retrieve the relevant data records. The full-text index can be created with the data table, or added later when necessary using the following command:

  ALTERTABLEtablenameADDFULLTEXT (column1, column2) With the full-text index, you can use the SELECT query command to retrieve data records that contain one or more of the given words. The following is the basic syntax of this type of query command:

  SELECT*FROMtablename

  WHEREMATCH(column1,column2)AGAINST(‘word1','word2','word3’)

  The above command will query all data records with word1, word2 and word3 in the column1 and column2 fields.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325918323&siteId=291194637