MySQL在有索引列情况下select *的输出结果顺序
创建一个表格,一个是主键列,一个是索引列。然后插入一批数据,调用select * from test_b,可以发现输出结果并没有按照Id有序,
创建一个表格,一个是主键列,一个是索引列。然后插入一批数据,调用select * from test_b,可以发现输出结果并没有按照Id有序,而是按照Type有序。
如果希望按照Id有序,,可以使用force index (primary)这一hint语句。
mysql> CREATE TABLE `test_b` (
-> `Id` int(11) NOT NULL,
-> `Type` int(11) DEFAULT NULL,
-> PRIMARY KEY (`Id`),
-> KEY `IDX_Type` (`Type`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.20 sec)
mysql> insert into test_b values(1,1),(2,6),(3,2),(7,3),(4,1);
Query OK, 5 rows affected (0.09 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from test_b;
+----+------+
| Id | Type |
+----+------+
| 1 | 1 |
| 4 | 1 |
| 3 | 2 |
| 7 | 3 |
| 2 | 6 |
+----+------+
5 rows in set (0.03 sec)
mysql> select * from test_b force index (primary);
+----+------+
| Id | Type |
+----+------+
| 1 | 1 |
| 2 | 6 |
| 3 | 2 |
| 4 | 1 |
| 7 | 3 |
+----+------+
5 rows in set (0.00 sec)
观察select * from test_b的前两条结果:(1,1),(4,1),当Type相等的时候,按照Id排序。为了确认这一点,再多插入点数据观察,结论相同。
mysql> insert into test_b values(9,3),(6,3),(10,3);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from test_b ;
+----+------+
| Id | Type |
+----+------+
| 1 | 1 |
| 4 | 1 |
| 3 | 2 |
| 6 | 3 |
| 7 | 3 |
| 9 | 3 |
| 10 | 3 |
| 2 | 6 |
+----+------+
8 rows in set (0.00 sec)
默认情况下为什么会结果按照索引列有序呢?这还要从数据库内部的运行机制说起。首先系统会查询索引表(test_b_indexed_type),该索引表的主键是索引列type(通常为了保证主键唯一性,type后面会添加一个id后缀),通过索引列查到Id,然后拿着这些Id去test_b中查询最终结果。为了最高效,扫描索引表的时候会顺着type主键往下扫,然后拿扫得的id去“逐个”请求test_b,于是自然就出现了按照索引列有序的结果。
当Type列的值一致的时候,插入到索引列的数据可以根据Id顺序插入到索引表中,保证了当Type一致的时候,会按照Id排序。
以上,是思考OceanBase实现二级索引以及并发Get时候学习思考得到的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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 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.

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.

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.

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 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
