Leilin Peng Share: MySQL indexes

  Build MySQL MySQL index for efficient operation is very important, the index can greatly improve the retrieval speed of MySQL.

  For example, if a reasonable design and use MySQL index is a Lamborghini, then there is no MySQL design and use of the index is a tricycle.

  Sub-index separate index and combination index. Separate index, i.e. only contains a single column index, a table can have multiple separate index, but this is not a composite index. Combination index, i.e. an index containing a plurality of columns.

  When you create an index, you need to make sure that the index is a condition in SQL query applications (usually as a condition of the WHERE clause).

  In fact, the index is also a table that holds the primary key and index fields, and point to record the entity table.

  The above are talking about the benefits of using the index, but too much use of the index will lead to abuse. Therefore, the index also has its disadvantages: Although the index greatly increased query speed, while it will reduce the speed of updating the table, such as table INSERT, UPDATE, and DELETE. Because when you update table, MySQL is not only to save the data, but also save about index file.

  Indexing files index takes up disk space.

  General index

  Creating an index

  This is the most basic index, it does not have any restrictions. It has created the following ways:

  CREATE INDEX indexName ON mytable(username(length));

  If CHAR, VARCHAR type, length can be less than the actual length of the field; if BLOB and TEXT types, must be specified length.

  Modify table configuration (add an index)

  ALTER table tableName ADD INDEX indexName(columnName)

  Create table when specified directly

  CREATE TABLE mytable(

  ID INT NOT NULL,

  username VARCHAR(16) NOT NULL,

  INDEX [indexName] (username(length))

  );

  Delete index syntax

  DROP INDEX [indexName] ON mytable;

  The only index

  It is similar to the previous general index, it is different: the value of the index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique. It has created the following ways:

  Creating an index

  CREATE UNIQUE INDEX indexName ON mytable(username(length))

  Modify table structure

  ALTER table mytable ADD UNIQUE [indexName] (username(length))

  Create table when specified directly

  CREATE TABLE mytable(

  ID INT NOT NULL,

  username VARCHAR(16) NOT NULL,

  UNIQUE [indexName] (username(length))

  );

  Use ALTER command to add and delete indexes

  There are four ways to add index data table:

  ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): This statement adds a primary key, which means that the index value must be unique, and can not be NULL.

  ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): This statement creates an index value must be unique (in addition to NULL, NULL may appear several times).

  ALTER TABLE tbl_name ADD INDEX index_name (column_list): add a normal index, the index value can occur multiple times.

  ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list): This statement specifies the index is FULLTEXT, for full-text indexing.

  The following examples are added in the index table.

  mysql> ALTER TABLE testalter_tbl ADD INDEX (c);

  You can also use the DROP clause to delete an index ALTER command. Try the following examples delete indexes:

  mysql> ALTER TABLE testalter_tbl DROP INDEX c;

  Use ALTER command to add and delete primary key

  It can only act on a primary key column, when you add a primary key index, you need to make sure that the default primary key is not empty (NOT NULL). Examples are as follows:

  mysql> ALTER TABLE testalter_tbl MODIFY i INT NOT NULL;

  mysql> ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);

  You can also use the ALTER command to delete the primary key:

  mysql> ALTER TABLE testalter_tbl DROP PRIMARY KEY;

  Simply specify PRIMARY KEY When you delete a primary key, but when you delete the index, you must know the name of the index.

  Display index information

  You can use the SHOW INDEX command to list the relevant index information table. Can \ G to format the output by adding the information.

  Try the following examples:

  mysql> SHOW INDEX FROM table_name; \G

  ........

  Click to see all MySQL Tutorial Articles: https://www.codercto.com/courses/l/30.html (edit: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/11022843.html