mysql index types normal, unique, full text

Question 1: mysql index difference between the type of normal, unique, full text of what is?

normal: Specifies the general index

unique: represents a unique, does not allow duplicate index, if the field is to ensure that information is not repeated, for example ID number as an index, it can be set to unique

full textl: indicates the index of full-text search. FULLTEXT searching for a long time an article, the best results. Used in a relatively short text, if it is a line or two of ordinary INDEX can.

Summary, the index category is determined by the characteristics of the field content indexing, the most common is usually normal.

Question 2: In actual operation, should be selected as the index table in which fields?

In order to make more efficient use of the index, when creating the index, you must consider creating an index and what type of indexes created on which fields, there are seven principles:

1. Select unique index
2. Is often necessary to sort, group and joint field operations indexing
3. Indexed field often as a query of
4. Limit the number of index
5. Try to use a small amount of index data
6. Try to use the prefix to index
7. Remove unused or little-used index

A, MySQL: B-tree index stored in a format 

  Memory storage engine can be selected or BTree Hash Index, Hash index can only be used or the equation = <=> comparison. 

  1, the general index: create index on Tablename (column list) 

  ALTER the Add the TableName Table index (column list) 

  Create the TableName Table ([...], index [the IndexName] (column list) 

  2, unique index: create index uNIQUE 

  the ALTER ... the Add uNIQUE 

  primary key: one kind of unique index must be specified as key primary 

  3, full-text indexing: start from version 3.23.23 to support full-text indexing and full-text search, FULLTEXT, 

  can char, varchar or text types create a column. 

  4, a separate index, multi-column index: 

  multiple separate index with a single multi-column index different query results, because: 

  when executing a query, MySQL can only use an index, a limit will choose from more indexes most strict index. 

  5, the most left-prefix (leftmost Prefixing): multi-column index, for example: fname_lname_age index, the following search criteria MySQL will use 

  fname_lname_age index: firstname, lastname, age; firstname , lastname; firstname, otherwise will not use. 

Second, determine what type of index created from sql query, how to optimize queries 

  Select the index column: 

  . A performance optimization process, choose to create an index on which the column is one of the most important steps. Consider using the index are mainly 

  two types of columns: columns appear in the where clause, clause appears in the join column. 

  B larger. Consider the distribution of values in a column, the column cardinality index, the better index. 

  c. Using short index, if the index of a string column, should specify a prefix length, an index can save a lot of space, improve query speeds. 

  d. using the most left-prefix 

  e. Do not over-indexing, only to maintain the desired index. Each additional index should take up extra disk space and reduce the performance of write operations. 

  When modifying the table of contents, index must be updated, and sometimes may require reconstruction, so the more the index, the longer the time spent.

  MySQL only at the operator before using the index: <, <=, =,  >,> =, between, in,

  and sometimes the like (not begin with wildcard characters% or _ in the case). 

mysql index classification 

in database tables, index the fields can greatly improve query speed. By advantage of these indexes, you can make more efficient MySQL query and run. Index is the key to fast search. Build MySQL MySQL index for the efficient operation is very important. Here are some common types of MySQL indexes. 
1, the general type of index 
that is the most basic index types, and it does not limit the uniqueness and the like. General index can be created in several ways: 
(1) create an index, such as CREATE INDEX index name ON tablename (1 column name, column name 2, ...); 
(2) modify the table, such as ALTER TABLE tablename ADD INDEX name index (1 column name, column name 2, ...); 
(3) when creating the table specified index, such as CREATE TABLE tablename ([...], iNDEX index name (1 column name, column name 
2, ...)); 
2, the only index 
this index and the previous "general index" is basically the same, but with one difference: all values of the indexed column can only appear once, that must be unique. Unique index can be created in several ways: 
(1) creating the index, for example (column list) CREATE UNIQUE INDEX index name TableName the ON; 
(2) modify a table, such as the name ALTER TABLE tablename ADD UNIQUE index (column list); 
(3) when creating the table specified index, such as cREATE tABLE tablename (, uNIQUE index name [...] (column column 
list)); 
3, the primary key 
primary key is a unique index, but it must designated as the "PRIMARY KEY". If you've ever used the type AUTO_INCREMENT column, you may already be familiar with the concept of the primary key class. In the primary key table creation generally specified, for example, "CREATE TABLE tablename ([...] , PRIMARY KEY ( column list));." However, we can also be added to modify the table by way of the primary key, for example, "ALTER TABLE tablename ADD PRIMARY KEY (column list);." Each table can have only one primary key. (Primary key index corresponding to the polymerization, the index is to find the fastest) 
4, a separate index and multi-column index 
Index can be a separate index, it can be multi-column index. 
(1) separate index is a commonly used index column field, a common index. 
(2) multi-column index is an index containing a plurality of column fields 
alter table student add index sy (name  , age, score);
index sy on a multi-column index, multi-column index to be effective in the following cases: 
SELECT * from student where name = 'jia' and age> = '12' // where the conditions contained in the index field of the first column and 
the second field 
select * from student where name = ' jia' // where the first column contains only condition field 
select * from student where name = ' jia' and score <60 // where field conditions are contained in the first column and third word 
sections 
are summarized: multi-column index only index in the first column contains the field conditions where valid when 
5, select the index column 
should be how to choose index column, first look at the query conditions, general conditions in the query column as an index

Guess you like

Origin blog.csdn.net/qq_35972907/article/details/95171235