Home Database Mysql Tutorial mysql index learning tutorial_MySQL

mysql index learning tutorial_MySQL

Oct 09, 2016 am 08:33 AM
mysql index

In mysql, indexes can be divided into two types: hash index and btree index.

Under what circumstances can B-tree index be used?

1.Full value matching index

For example:

orderID="123"

2. Match leftmost prefix index query

For example: create a joint index on userid and date fields.
Then if you enter userId as the condition, then the userid can be used in the index. If you directly enter date as the condition, then the index cannot be used.

3. Match column prefix query

For example: order_sn like ‘134%’ This can use the index.

4. Match range value query

createTime>'2015-01-09' and createTime<'2015-01-10'

5. Exactly match the left front column and range match another column

For example:

userId=1 and createTime>'2016-9-18'

6. A query that only accesses the index is called a covering index, and the index includes the data of the query column.

Limitations of BTREE index

1. If the search does not start from the leftmost column of the index, the index cannot be used.

For example, create a joint index:

orderId and createTime fields create a joint index. If you only enter the condition of createTIme without the condition of orderid, this index will not be used.

2. When using an index, you cannot skip the indexed columns.

Three columns:

Date, name, and phone number form columns and indexes. If you only enter date and phone number when querying, you can only use date as an index for filtering.

3.NOT IN and <> operations cannot use indexes.

4. If there is a range query for a certain column in the query, all columns to the right of it cannot use the index.

Features of hash index

Hash index is implemented based on hash table. Hash index can only be used when the query conditions accurately match all columns in the hash index. It can only be an equivalent query.

For all columns in the hash index, the storage engine will calculate a hash code for each row, and the hash code is stored in the hash index.

Limitations:

1. It must be read twice, first read the hash to find the corresponding row, and then read the corresponding row data.

2.Hash index cannot be used for sorting.

3. Only precise search is supported, partial index search is not supported, and range search is not supported.

hash conflict:

Hash indexes cannot be used on fields with poor selectivity, but must be used to create hash indexes on columns with strong selectivity.

For example: don’t create a hash index on the gender field.

Why use index?

1. Indexes greatly reduce the amount of data that the storage engine needs to scan. Index is smaller than data size.

2. Indexes can help us sort to avoid using temporary tables. Indexes are ordered.

3. Index can turn random I/0 into sequential IO

Are the more indexes, the better?

1. Indexes will increase the cost of write operations

2. Too many indexes will increase query optimizer and selection time.

Strategy for indexing

1. Expressions or functions cannot be used on index columns

For example: select * from product where to_days(out_date) –to_days(current_date)<=30, out_date is the index column.

Change to:

select* from product where out_date

2.The index size cannot exceed a certain value.
inodb index column size is 200 length.

3. Selectivity of prefix and index columns.

create index idx_NAME on table (account);

4. Union index

How to choose the order of index columns.

1. Columns that are often indexed.

2. Columns with high selectivity are given priority.

3. Create indexes on small columns.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Several situations of mysql index failure Several situations of mysql index failure Feb 21, 2024 pm 04:23 PM

Common situations: 1. Use functions or operations; 2. Implicit type conversion; 3. Use not equal to (!= or <>); 4. Use the LIKE operator and start with a wildcard; 5. OR conditions; 6. NULL Value; 7. Low index selectivity; 8. Leftmost prefix principle of composite index; 9. Optimizer decision; 10. FORCE INDEX and IGNORE INDEX.

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Under what circumstances will mysql index fail? Under what circumstances will mysql index fail? Aug 09, 2023 pm 03:38 PM

MySQL indexes will fail when querying without using index columns, mismatching data types, improper use of prefix indexes, using functions or expressions for querying, incorrect order of index columns, frequent data updates, and too many or too few indexes. . 1. Do not use index columns for queries. In order to avoid this situation, you should use appropriate index columns in the query; 2. Data types do not match. When designing the table structure, you should ensure that the index columns match the data types of the query; 3. , Improper use of prefix index, you can use prefix index.

MySQL index left prefix matching rules MySQL index left prefix matching rules Feb 24, 2024 am 10:42 AM

MySQL index leftmost principle principle and code examples In MySQL, indexing is one of the important means to improve query efficiency. Among them, the index leftmost principle is an important principle that we need to follow when using indexes to optimize queries. This article will introduce the principle of the leftmost principle of MySQL index and give some specific code examples. 1. The principle of index leftmost principle The index leftmost principle means that in an index, if the query condition is composed of multiple columns, then only the leftmost column in the index can be queried to fully satisfy the query conditions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

What are the classifications of mysql indexes? What are the classifications of mysql indexes? Apr 22, 2024 pm 07:12 PM

MySQL indexes are divided into the following types: 1. Ordinary index: matches value, range or prefix; 2. Unique index: ensures that the value is unique; 3. Primary key index: unique index of the primary key column; 4. Foreign key index: points to the primary key of another table ; 5. Full-text index: full-text search; 6. Hash index: equal match search; 7. Spatial index: geospatial search; 8. Composite index: search based on multiple columns.

How to use MySQL indexes rationally and optimize database performance? Design protocols that technical students need to know! How to use MySQL indexes rationally and optimize database performance? Design protocols that technical students need to know! Sep 10, 2023 pm 03:16 PM

How to use MySQL indexes rationally and optimize database performance? Design protocols that technical students need to know! Introduction: In today's Internet era, the amount of data continues to grow, and database performance optimization has become a very important topic. As one of the most popular relational databases, MySQL’s rational use of indexes is crucial to improving database performance. This article will introduce how to use MySQL indexes rationally, optimize database performance, and provide some design rules for technical students. 1. Why use indexes? An index is a data structure that uses

Performance optimization strategies for data update and index maintenance of PHP and MySQL indexes and their impact on performance Performance optimization strategies for data update and index maintenance of PHP and MySQL indexes and their impact on performance Oct 15, 2023 pm 12:15 PM

Performance optimization strategies for data update and index maintenance of PHP and MySQL indexes and their impact on performance Summary: In the development of PHP and MySQL, indexes are an important tool for optimizing database query performance. This article will introduce the basic principles and usage of indexes, and explore the performance impact of indexes on data update and maintenance. At the same time, this article also provides some performance optimization strategies and specific code examples to help developers better understand and apply indexes. Basic principles and usage of indexes In MySQL, an index is a special number

See all articles