Mysql index PRIMARY, NORMAL, UNIQUE, FULLTEXT difference and use case Mysql index PRIMARY, NORMAL, UNIQUE, FULLTEXT difference and use case

Mysql index PRIMARY, NORMAL, UNIQUE, FULLTEXT difference and usage

 

index  

  The index of the database is like the catalog of a book, which can speed up the query speed of the database.

  MYSQL index has four kinds of PRIMARY, INDEX, UNIQUE, FULLTEXT, among them PRIMARY, INDEX, UNIQUE is one kind, FULLTEXT is one kind.

  These four are single-column indexes, that is, they all act on a single column, so they are also called single-column indexes; but so an index can also act on multiple columns, which is called a composite index or a composite index.

single column index

  Create a new test table

CREATE TABLE T_USER( ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL);

  (1) PRIMARY: primary key index. The index column is unique and cannot be empty ; a table can only have one primary key index (the primary key index is usually specified when the table is created)

CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,PRIMARY KEY(ID))

  (2) NORMAL : Normal index. There are no restrictions on indexed columns;

  • specified when creating the table
    CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,INDEX USERNAME_INDEX(USERNAME(16))) //给列USERNAME建普通索引USERNAME_INDEX
  • ALTER statement specifies
    ALTER TABLE T_USER ADD INDEX U_INDEX (USERNAME) //Create a common index U_INDEX for the column USERNAME
  • drop index
    DROP INDEX U_INDEX ON t_user // delete the index U_INDEX in table t_user

    (3) UNIQUE : Unique index. The value of the index column must be unique, but nulls are allowed;

  • specified when creating the table
    CREATE TABLE t_user(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,UNIQUE U_INDEX(USERNAME)) //Add unique index T_USER to column USERNAME
  • ALTER statement specifies
    ALTER TABLE t_user ADD UNIQUE u_index(USERNAME) //Add a unique index u_index to the column T_USER
  • drop index
    DROP INDEX U_INDEX ON t_user

  (4) FULLTEXT : Index for full-text search. FULLTEXT works best when searching for very long articles. It is used for relatively short text, if it is only one or two lines of words, ordinary INDEX can also be used. The creation and deletion of indexes are the same as above, and will not be listed here...

Composite Index (Compound Index)  

  create a new table

CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,CITY VARCHAR(10),PHONE VARCHAR(10),PRIMARY KEY(ID) )

  A composite index is to add multiple columns to a unified index, such as the newly created table T_USER, we create a composite index for USERNAME+CITY+PHONE

ALTER TABLE t_user ADD INDEX name_city_phone(USERNAME,CITY,PHONE) //combine common indexes

ALTER TABLE t_user ADD UNIQUE name_city_phone(USERNAME,CITY,PHONE) //Combined unique index
  Such a composite index is actually equivalent to establishing three indexes ( USERANME, CITY, PHONE USERNAME, CITY USERNAME, PHONE ).
 
  Why is there no (CITY, PHONE) index? This is because MYSQL combines the results of the query "leftmost prefix". A simple understanding is to only start the combination from the far left.
 
  The composite index is not used when the query contains these three columns:
  
  Such a query statement will use the created composite index
SELECT * FROM t_user where USERNAME="parry" and CITY="广州" and PHONE="180"
SELECT * FROM t_user where USERNAME="parry" and CITY="广州"
SELECT * FROM t_user where USERNAME="parry" and PHONE="180"

  Such a query statement will not use the created composite index 

SELECT * FROM t_user where CITY="广州" and PHONE="180"
SELECT * FROM t_user where CITY="广州"
SELECT * FROM t_user where PHONE="180"

Index deficiencies

(1) The index improves the speed of query, but reduces the speed of INSERT, UPDATE, DELETE, because when inserting, modifying, and deleting data, it is necessary to operate the index file at the same time;
 
(2) If the index is not established, it will occupy a certain amount of disk space.

 Notes on using indexes

  (1) As long as the column contains NULL values, it will not be included in the index. As long as there is a column with NULL values ​​in the composite index, then this column is invalid for the composite index, so it is best not to allow the field to be used when designing the database. The default value is NULL;
 
     (2) Use short index
              If it is possible to specify a length for the index, for example: a VARCHAR(255) column, but the actual data stored is only 20 bits, the length of the index should be specified as 20 when creating the index, instead of not writing by default. as follows
ALTER TABLE t_user add INDEX U_INDEX(USERNAME(16)) 优于 ALTER TABLE t_user add INDEX U_INDEX(USERNAME)

  Using short indexes can not only improve query speed, but also save disk operations and I/O operations.

  (3) Index column sorting

      Mysql will only use one index when querying , so if the where clause has already used the index, the columns in the order by will not use the index, so the order by try not to include the sorting of multiple columns, if you must For multi-column sorting, it is best to use a composite index.

  (4) Like statement

    In general, the use of like is not encouraged. If it is not used, it should be noted that like "%aaa%" will not use an index; but like "aaa%" will use an index.

  (5) Do not use NOT IN and <> operations

Index method HASH and BTREE comparison

  (1)HASH

    For equivalence comparisons like "=" and " <=>"

  (2)BTREE

    The BTREE index is stored in a tree structure by looking at the name, and it is usually used in operators such as " =, >, >=, <, <=, BETWEEN, Like" and other operators with high query efficiency;

   Through comparison, it is found that we commonly use the BTREE index method. Of course, Mysql defaults to the BTREE method.

index  

  The index of the database is like the catalog of a book, which can speed up the query speed of the database.

  MYSQL index has four kinds of PRIMARY, INDEX, UNIQUE, FULLTEXT, among them PRIMARY, INDEX, UNIQUE is one kind, FULLTEXT is one kind.

  These four are single-column indexes, that is, they all act on a single column, so they are also called single-column indexes; but so an index can also act on multiple columns, which is called a composite index or a composite index.

single column index

  Create a new test table

CREATE TABLE T_USER( ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL);

  (1) PRIMARY: primary key index. The index column is unique and cannot be empty ; a table can only have one primary key index (the primary key index is usually specified when the table is created)

CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,PRIMARY KEY(ID))

  (2) NORMAL : Normal index. There are no restrictions on indexed columns;

  • specified when creating the table
    CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,INDEX USERNAME_INDEX(USERNAME(16))) //给列USERNAME建普通索引USERNAME_INDEX
  • ALTER statement specifies
    ALTER TABLE T_USER ADD INDEX U_INDEX (USERNAME) //Create a common index U_INDEX for the column USERNAME
  • drop index
    DROP INDEX U_INDEX ON t_user // delete the index U_INDEX in table t_user

    (3) UNIQUE : Unique index. The value of the index column must be unique, but nulls are allowed;

  • specified when creating the table
    CREATE TABLE t_user(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,UNIQUE U_INDEX(USERNAME)) //Add unique index T_USER to column USERNAME
  • ALTER statement specifies
    ALTER TABLE t_user ADD UNIQUE u_index(USERNAME) //Add a unique index u_index to the column T_USER
  • drop index
    DROP INDEX U_INDEX ON t_user

  (4) FULLTEXT : Index for full-text search. FULLTEXT works best when searching for very long articles. It is used for relatively short text, if it is only one or two lines of words, ordinary INDEX can also be used. The creation and deletion of indexes are the same as above, and will not be listed here...

Composite Index (Compound Index)  

  create a new table

CREATE TABLE T_USER(ID INT NOT NULL,USERNAME VARCHAR(16) NOT NULL,CITY VARCHAR(10),PHONE VARCHAR(10),PRIMARY KEY(ID) )

  A composite index is to add multiple columns to a unified index, such as the newly created table T_USER, we create a composite index for USERNAME+CITY+PHONE

ALTER TABLE t_user ADD INDEX name_city_phone(USERNAME,CITY,PHONE) //combine common indexes

ALTER TABLE t_user ADD UNIQUE name_city_phone(USERNAME,CITY,PHONE) //Combined unique index
  Such a composite index is actually equivalent to establishing three indexes ( USERANME, CITY, PHONE USERNAME, CITY USERNAME, PHONE ).
 
  Why is there no (CITY, PHONE) index? This is because MYSQL combines the results of the query "leftmost prefix". A simple understanding is to only start the combination from the far left.
 
  The composite index is not used when the query contains these three columns:
  
  Such a query statement will use the created composite index
SELECT * FROM t_user where USERNAME="parry" and CITY="广州" and PHONE="180"
SELECT * FROM t_user where USERNAME="parry" and CITY="广州"
SELECT * FROM t_user where USERNAME="parry" and PHONE="180"

  Such a query statement will not use the created composite index 

SELECT * FROM t_user where CITY="广州" and PHONE="180"
SELECT * FROM t_user where CITY="广州"
SELECT * FROM t_user where PHONE="180"

Index deficiencies

(1) The index improves the speed of query, but reduces the speed of INSERT, UPDATE, DELETE, because when inserting, modifying, and deleting data, it is necessary to operate the index file at the same time;
 
(2) If the index is not established, it will occupy a certain amount of disk space.

 Notes on using indexes

  (1) As long as the column contains NULL values, it will not be included in the index. As long as there is a column with NULL values ​​in the composite index, then this column is invalid for the composite index, so it is best not to allow the field to be used when designing the database. The default value is NULL;
 
     (2) Use short index
              If it is possible to specify a length for the index, for example: a VARCHAR(255) column, but the actual data stored is only 20 bits, the length of the index should be specified as 20 when creating the index, instead of not writing by default. as follows
ALTER TABLE t_user add INDEX U_INDEX(USERNAME(16)) 优于 ALTER TABLE t_user add INDEX U_INDEX(USERNAME)

  Using short indexes can not only improve query speed, but also save disk operations and I/O operations.

  (3) Index column sorting

      Mysql will only use one index when querying , so if the where clause has already used the index, the columns in the order by will not use the index, so the order by try not to include the sorting of multiple columns, if you must For multi-column sorting, it is best to use a composite index.

  (4) Like statement

    In general, the use of like is not encouraged. If it is not used, it should be noted that like "%aaa%" will not use an index; but like "aaa%" will use an index.

  (5) Do not use NOT IN and <> operations

Index method HASH and BTREE comparison

  (1)HASH

    For equivalence comparisons like "=" and " <=>"

  (2)BTREE

    The BTREE index is stored in a tree structure by looking at the name, and it is usually used in operators such as " =, >, >=, <, <=, BETWEEN, Like" and other operators with high query efficiency;

   Through comparison, it is found that we commonly use the BTREE index method. Of course, Mysql defaults to the BTREE method.

Guess you like

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