MySQL basic study notes-index design and use

Index design and use

1. Index overview

All MySQL column types can be indexed. Using indexes on related columns is the best way to improve the performance of SELECT operations. According to the maximum number of indexes and maximum index length of each table that can be defined by the storage engine, each storage engine supports at least 16 indexes for each table, and the total index length is at least 256 bytes.

The tables of the MyISAM and InnoDB storage engines are all BTREE indexes created by default . MySQL currently does not support functional indexes, but supports prefix indexes, that is, to create an index on the first N characters of the index field. The length of the prefix index is related to the storage engine. For MyISAM storage engine tables, the index prefix length can reach 1000 bytes, while for InnoDB storage engine tables, the index prefix length is up to 767 bytes.

MySQL also supports full-text indexing, which can be used for full-text search. But in the current latest version, only the MyISAM storage engine supports FULLTEXT indexes, and it is limited to CHAR, VARCHAR and TEXT columns. Indexes are always performed on the entire column, and local indexes are not supported.

You can also create indexes for spatial column types, but only MyISAM storage indexes support spatial indexes, and the indexed fields must be non-empty.

By default, the MEMORY storage engine uses HASH index, but it also supports BTREE index .

Indexes can be created at the same time when the table is created, or new indexes can be added at any time. The syntax for creating a new index is:

CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[USING index_type]
ON tbl_name(index_col_name,...)

index_col_name:
	col_name[(length)][ASC|DESC]

You can also use the syntax of ALTER TABLE to add indexes. The syntax is similar to CREATE INDEX. For example, to create a 10-byte prefix index for the city table:

mysql> create index cityname on city(city(10));
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

The syntax for index deletion is:

DROP INDEX index_name ON tbl_name
mysql> drop index cityname on city;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

2. The principle of index design

  • The index column to be searched is not necessarily the column to be selected. In other words, the most suitable index column is the column that appears in the WHERE clause, or the column in the join clause, rather than the column that appears in the select list after the SELECT keyword.
  • Use a unique index. Consider the distribution of values ​​in a column. The larger the cardinality of the indexed column, the better the index effect.
  • Use short indexes.
  • Use the leftmost prefix.
  • Don't over-index.
  • For InnoDB storage engine tables, records will be saved in a certain order by default. If there are clearly defined primary keys, they will be saved in the order of primary keys. If there is no primary key, but there is a unique index, it is stored in the order of the unique index. If there is neither a primary key nor a unique index, then an internal column is automatically generated in the table and stored in the order of this column.

3. BTREE index and HASH index

The tables of the MEMORY storage engine can choose to use BTREE index or HASH index , and the two different types of indexes have different usage ranges. HASH index has some important characteristics that require special attention when using it:

  • Only used for equality comparisons using = or <=> operators;
  • The optimizer cannot use HASH index to speed up ORDER BY operations;
  • MySQL cannot determine approximately how many rows are between the two values. If a MyISAM table is changed to a HASH indexed MEMORY table, it will affect the execution efficiency of some queries;
  • Only use the entire keyword to search for a line.

For BTREE indexes, when using >, <, >=, <=, BETWEEN, != or <>, or the LIKE'pattern' operator, you can use the index on the relevant column.

The following range queries are applicable to BTREE index and HASH index:

SELECT * FROM t1 WHERE key_col = 1 OR key_col IN (15,18,20);

The following range queries are only applicable to BTREE indexes:

SELECT * FROM t1 WHERE key_col > 1 AND key_col < 10;
SELECT * FROM t1 WHERE key_col LIKE 'ab%' OR key_col BETWEEN 'lisa' AND 'simon';

For example, create a MEMORY storage engine table city_memory that is identical to the city table:

mysql> CREATE TABLE city_memory( 
	 > city_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, 
	 > city VARCHAR(50) NOT NULL,
     > country_id SMALLINT UNSIGNED NOT NULL,
     > last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
     > PRIMARY KEY(city_id), KEY idx_fk_country_id (country_id) )ENGINE=Memory DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

When using the Memory table, if it is the HASH index created by default, you must pay attention to the writing of SQL statements to ensure that the upper index can be used. If you must use the range query, you should choose to create the BTREE index when creating the index.


4. Summary

Indexes are used to quickly find rows with a specific value in a column . If you do not use an index, MySQL must start with the first record and then read the entire table until it finds the relevant rows. The larger the table, the more time it takes. If the query column in the table has an index, MySQL can quickly reach a location to search the middle of the data file, and there is no need to look at all the data.

Most MySQL indexes (such as PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT, etc.) are stored in BTREE. Only the spatial index uses BTREE, and the MEMORY table also supports HASH index.

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/108541467