Home Database Mysql Tutorial MySQL在有索引列情况下select *的输出结果顺序_MySQL

MySQL在有索引列情况下select *的输出结果顺序_MySQL

Jun 01, 2016 pm 01:36 PM
yes

bitsCN.com

MySQL在有索引列情况下select *的输出结果顺序

 

创建一个表格,一个是主键列,一个是索引列。然后插入一批数据,调用select * from test_b,可以发现输出结果并没有按照Id有序,而是按照Type有序。

如果希望按照Id有序,可以使用force index (primary)这一hint语句。

[sql]

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排序。为了确认这一点,再多插入点数据观察,结论相同。

[sql] 

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时候学习思考得到的。
 

bitsCN.com
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 Article Tags

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)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

How do I configure SSL/TLS encryption for MySQL connections?

See all articles