Home > Database > Mysql Tutorial > body text

MySQL中Order By实现原理分析[组图]_MySQL

WBOY
Release: 2016-06-01 13:54:20
Original
1088 people have browsed it

  下面将通过实例分析两种排序实现方式及实现图解:

  假设有 Table A 和 B 两个表结构分别如下:

1 sky@localhost : example 01:48:21> show create table AG
2 
3 *************************** 1. row ***************************
4 
5 Table: A
6 
7 Create Table: CREATE TABLE `A` (
8 
9 `c1` int(11) NOT NULL default '0',
10 
11 `c2` char(2) default NULL,
12 
13 `c3` varchar(16) default NULL,
14 
15 `c4` datetime default NULL,
16 
17 PRIMARY KEY (`c1`)
18 
19 ) ENGINE=MyISAM DEFAULT CHARSET=utf8
20 
21 sky@localhost : example 01:48:32> show create table BG
22 
23 *************************** 1. row ***************************
24 
25 Table: B
26 
27 Create Table: CREATE TABLE `B` (
28 
29 `c1` int(11) NOT NULL default '0',
30 
31 `c2` char(2) default NULL,
32 
33 `c3` varchar(16) default NULL,
34 
35 PRIMARY KEY (`c1`),
36 
37 KEY `B_c2_ind` (`c2`)
38 
39 ) ENGINE=MyISAM DEFAULT CHARSET=utf8

  1、利用有序索引进行排序,实际上就是当我们Query 的ORDER BY 条件和Query 的执行计划中所利用的Index的索引键(或前面几个索引键)完全一致,且索引访问方式为rang、ref 或者index的时候,MySQL可以利用索引顺序而直接取得已经排好序的数据。这种方式的ORDER BY 基本上可以说是最优的排序方式了,因为MySQL不需要进行实际的排序操作。

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!