【mysql】一个关于order by排序的问题

I have a table

CREATE TABLE `tableMain` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `value1` varchar(45) NOT NULL,
   ‘value2‘ varchar(50) NOT NULL,
   ‘value3‘ int NOT NULL,
   ‘value4‘ timestamp NOT NULL,
   ‘value5‘ int NOT NULL
PRIMARY KEY (`id`)
)

So I create that table and I want it to be always ordered by value2, if there is two a like it should sort by value3 and after that value4.

So I try to do it by that

ALTER TABLE tableMain
ORDER BY value2 ASC, value3 ASC, value4 ASC

And when I run that code I get an error:

Error Code: 1105. ORDER BY ignored as there is a user-defined clustered   index in the table ‘tableMain‘

If you want rows in a particular sequence, specify an appropriate ORDER BY clause in your query.

The idea that rows need to be "ordered" in a table flies in the face of relational database theory.

A relation is a set of tuples; altering the "order" of tuples within a relation does not alter the relation.

Translating the theory into practice, with the InnoDB storage engine, it doesn‘t make sense to specify ORDER BY as a table attribute, since an InnoDB table will always be ordered by its cluster index.

In the case of the MyISAM storage engine, specifying ORDER BY may improve performance of some queries. The ALTER TABLE ... ORDER BY statement only reorganizes the table one time. The "order" of the rows may not be preserved when subsequent DELETEINSERT and UPDATEstatements are run.

To reiterate: if you need rows returned in a particular order, you can not depend on the "order" that rows are physically stored in a table. It‘s imperative that you include an ORDER BY on the query.

To really improve performance with large tables, adding appropriate indexes is the way to go.



As to why your classmates get the statement to run, and your statement returns an error... the most likely explanation is that their tables are using the MyISAM storage engine, while your table is using the InnoDB storage engine.

(Whatever your assignment is, changing the storage engine of your table can not be the right answer... MyISAM storage engine is an appropriate choice for some use cases; but InnoDB is the most appropriate choice for traditional "relational database" uses cases.)



If your requirement is that your InnoDB table to "always be ordered by" a set of columns (for whatever reason), then have the cluster index include those columns as the leading columns. You can do that by declaring those columns as the leading columns of the PRIMARY KEY of your table. You can create a UNIQUE INDEX on the id column.

CREATE TABLE `tableMain`
( `id` INT(11) NOT NULL AUTO_INCREMENT
, `value1` VARCHAR(45) NOT NULL
, `value2` VARCHAR(50) NOT NULL
, `value3` INT NOT NULL
, `value4` TIMESTAMP NOT NULL
, `value5` INT NOT NULL
, PRIMARY KEY (`value2`,`value3`,`value4`,`id`)
, UNIQUE KEY `tableMain_UX1` (`id`)
)

In reality, we‘d never do this... because any secondary indexes are going to include the PRIMARY KEY values as the "pointer" back to the cluster index, and that‘s going to be an incredible waste of resources. In practice, we‘d leave id as the PRIMARY KEY of the table, and create a secondary index on the other columns...

CREATE TABLE `tableMain`
( `id` INT(11) NOT NULL AUTO_INCREMENT
, `value1` VARCHAR(45) NOT NULL
, `value2` VARCHAR(50) NOT NULL
, `value3` INT NOT NULL
, `value4` TIMESTAMP NOT NULL
, `value5` INT NOT NULL
, PRIMARY KEY (`id`)
, KEY `tableMain_IX1` (`value2`,`value3`,`value4`)
)

source:http://stackoverflow.com/questions/29781052/order-by-ignored-as-there-is-a-user-defined-clustered-index-in-the-table/29781290#29781290

时间: 2024-10-01 03:39:44

【mysql】一个关于order by排序的问题的相关文章

MySQL中order by排序时,数据存在null咋办

order by排序是最常用的功能,但是排序有时会遇到数据为空null的情况,这样排序就会乱了,这里以MySQL为例,记录我遇到的问题和解决思路. 问题: 网页要实现table的行鼠标拖拽排序,我用AngularJs集成了一个TableDnD开源插件,可以实现,然后在数据库表中增加一个排序字段indexId,但是原来的大量数据是没有排序过的,所以该字段为null. 这样order by时,为null的数据就会排在最前边. 写个测试表模拟一下,如下效果: 解决办法: 最优办法:利用MySQL中的一

MYSQL order by排序与索引关系总结

MySQL InnoDB B-Tree索引使用Tips 这里主要讨论一下InnoDB B-Tree索引的使用,不提设计,只管使用.B-Tree索引主要作用于WHERE和ORDER BY子句.这里讨论的均在MySQL-Server-5.1.42测试 CREATE TABLE `friends` ( `ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, `uid`bigint(20) UNSIGNED NOT NULL DEFAULT '0', `fuid`

MySQL order by 排序结果不正确

新建一张测试表: CREATE TABLE `tb1` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `a` decimal(19,2) NOT NULL, `acid` bigint(20) NOT NULL, `prid` bigint(20) NOT NULL, PRIMARY KEY (`id`), KEY `idx_prid` (`prid`), KEY `idx_acid` (`acid`) ) ENGINE=InnoDB AUTO_INCRE

Mysql group by,order by,dinstict优化

1.order by优化 实现方式: 1. 根据索引字段排序,利用索引取出的数据已经是排好序的,直接返回给客户端: 2. 没有用到索引,将取出的数据进行一次排序操作后返回给客户端. 1 EXPLAIN SELECT m.id,m.subject,c.content FROM group_message m,group_message_content c WHERE m.group_id = 1 AND m.id = c.group_msg_id ORDER BY m.user_id\G; opt

玩得一手好注入之order by排序篇

看了之前Gr36_前辈在先知上的议题,其中有提到排序注入,这个在最近经常遇到这样的问题,所以先总结下order by 排序注入的知识. 0×00 背景 看了之前Gr36_前辈在先知上的议题,其中有提到排序注入,这个在最近经常遇到这样的问题,所以先总结下order by 排序注入的知识. 0×01 环境信息 测试环境:操作系统ubuntu0.14.04.1 MYSQL:5.5.55-0 测试代码: <?php $mysql_server="10.10.10.136″; $mysql_user

mysql 字符串按照数字类型排序

一个varchar,char的字段 存放 1+,12- ,11等字符串将字段*1或者+0可以将MySQL字符串字段按数值排序 order by 字段名称+0 desc/asc的形式进行排序 order by 字段名称*1 desc/asc的形式进行排序 原文地址:https://www.cnblogs.com/grimm/p/10297157.html

MySQL高级 之 order by、group by 优化

order by示例 示例数据: Case 1 Case 2 Case 3 Case 4 结论:order by子句,尽量使用Index方式排序,在索引列上遵循索引的最佳左前缀原则. 复合(联合)索引形如 key (‘A1’,’A2’,’A3’ ),排序的思路一般是,先按照A1来排序,A1相同,然后按照A2排序,以此类推,这样对于(A1),(A1,A2), (A1,A2,A3)的索引都是有效的,但是对于(A2,A3)这样的索引就无效了.尽量避免因索引字段的缺失 或 索引字段顺序的不同 引起的Fi

单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询

今日内容 表查询 单表查询: where group by 分组 having distinct 去重 order by 排序 limit 多表查询 子查询 连表查询 单表查询 前期表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) u

MySQL 数字字符串按照数字排序

一个关于MySQL字符串排序,在数据里面定义的是varchar类型,实际存放的是Int类型的数据,按一下查询语句进行排序: 将字段*1或者+0可以将MySQL字符串字段按数值排序 如: 1 select * from table where 1 order by id*1 desc; 或者 1 select * from table where 1 order by id+0 desc; 除了上述方法外,这里附上一种排序方法,利用find_in_set()进行无敌排序 参考链接:https://