未加星标

MySQL may return results in non-deterministic order with ‘order by’

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Whenever we want a query’s results sorted, you may think of using the clause “ order by.” You are correct: most of the time,mysql will return the results in expected order with “order by.”

Be careful, though. Sometimes MySQL may return results in the non-deterministic order even with “ order by.”

Indeed, if a query is ordered by a non-unique column, it may return results in an unexpected order. I tested the below example on MySQL 5.1.73, 5.5.54 and 5.6.19 and got the same result. However, whenI applied the same example on MySQL 5.7.17, it returned the results in an unexpected order differently.

Follow me step-by-step and see how MySQL returns results in a non-deterministic order. Step 1-4 is for MySQL 5.1.73, 5.5.54 and 5.6.19, Step 5 is for MySQL 5.7.17. After the example, I will explain the reason behind this output.

Step 1. Create the table as below:

CREATE TABLE `user_rankings` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `sort_order` INT(11) NOT NULL, `equals_role` VARCHAR(40) NOT NULL DEFAULT '', `equals_user_id` INT(11) NOT NULL, `metric_posts` INT(11) NOT NULL DEFAULT '0', `metric_pageviews` INT(11) NOT NULL DEFAULT '0', `metric_logins` INT(11) NOT NULL DEFAULT '0', `metric_minutes_online` INT(11) NOT NULL DEFAULT '0', `average_message_rating` INT(11) NOT NULL DEFAULT '0', `registration_age` INT(11) NOT NULL DEFAULT '0', `metric_tags` INT(11) NOT NULL DEFAULT '0', `rank_formula_enabled` tinyint(1) DEFAULT NULL, `rank_formula` VARCHAR(5000) DEFAULT NULL, `rank_name` VARCHAR(80) NOT NULL, `rank_bold` tinyint(1) NOT NULL DEFAULT '0', `rank_color` VARCHAR(6) NOT NULL DEFAULT '', `rank_icon` VARCHAR(255) NOT NULL DEFAULT '', `rank_icon_right` VARCHAR(255) NOT NULL DEFAULT '', `rank_icon_thread` VARCHAR(255) NOT NULL DEFAULT '', `grant_roles` VARCHAR(4100) NOT NULL DEFAULT '', `remove_roles` VARCHAR(4100) NOT NULL DEFAULT '', `kudos_weight` INT(11) DEFAULT NULL, `deleted` tinyint(1) NOT NULL DEFAULT '0', `row_version` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `IDX_USER_RANKINGS_ROW_VERSION` (`row_version`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8;

Step 2. Insert into the table 31 rows of data as below: INSERT INTO `user_rankings` VALUES (1,0,'Administrator,Ipsos Admin',-1,0,0,0,0,0,0,0,1,'hasRole(\"Administrator\") || hasRole(\"Ipsos Admin\") || hasRole(\"Acat_Moderator\") || hasRole(\"Bcat_Moderator\") || hasRole(\"Ccat_Moderator\") || hasRole(\"Dcat_Moderator\") || hasRole(\"Ecat_Moderator\") || hasRole(\"Fcat_Moderator\") || hasRole(\"Gcat_Moderator\") || hasRole(\"Hcat_Moderator\") || hasRole(\"Icat_Moderator\") || hasRole(\"Jcat_Moderator\") || hasRole(\"Kcat_Admin\") || hasRole(\"Lcat_Admin\") || hasRole(\"Mcat_Admin\") || hasRole(\"Ncat_Admin\") || hasRole(\"Ocat_Admin\") || hasRole(\"Pcat_Admin\") || hasRole(\"Qcat_Admin\") || hasRole(\"Rcat_Admin\") || hasRole(\"Scat_Admin\") || hasRole(\"Tcat_Admin\") || hasRole(\"Ucat_Admin\") || hasRole(\"Vcat_Admin\") || hasRole(\"Wcat_Admin\") || hasRole(\"Xcat_Admin\") || hasRole(\"Ycat_Admin\") || hasRole(\"Zcat_Admin\") || hasRole(\"Dcat_Moderator\") || hasRole(\"Ccat_Moderator\") || hasRole(\"Bcat_Moderator\") || hasRole(\"Ecat_Moderator\")','Community Manager',1,'009966','/i/rank_icons/admin.gif','','','','',7,0,'2016-10-25 00:16:34'), (2,1,'Moderator',-1,0,0,0,0,0,0,0,0,'','Moderator',1,'009966','/i/rank_icons/moderator.gif','','','','',7,0,'2016-07-07 01:48:03'), (3,2,'Member',-1,0,0,0,0,0,0,0,0,'hasRole(\"Member\") || hasRole(\"Acat_Member\") || hasRole(\"Bcat_Member\") || hasRole(\"Ccat_Member\") || hasRole(\"Dcat_Member\") || hasRole(\"Ecat_Member\") || hasRole(\"Fcat_Member\") || hasRole(\"Gcat_Member\") || hasRole(\"Hcat_Member\") || hasRole(\"Icat_Member\") || hasRole(\"Jcat_Member\") || hasRole(\"Kcat_Member\") || hasRole(\"Lcat_Member\") || hasRole(\"Mcat_Member\") || hasRole(\"Ncat_Member\") || hasRole(\"Ocat_Member\") || hasRole(\"Pcat_Member\") || hasRole(\"Qcat_Member\") || hasRole(\"Rcat_Member\") || hasRole(\"Scat_Member\") || hasRole(\"Tcat_Member\") || hasRole(\"Ucat_Member\") || hasRole(\"Vcat_Member\") || hasRole(\"Wcat_Member\") || hasRole(\"Xcat_Member\") || hasRole(\"Ycat_Member\") || hasRole(\"Zcat_Member\")','Member',0,'666666','','','','','',-1,0,'2016-09-27 07:37:42'),(4,3,'Client',-1,0,0,0,0,0,0,0,1,'hasRole(\"Client\") || hasRole(\"Acat_Client\") || hasRole(\"Bcat_Client\") || hasRole(\"Ccat_Client\") || hasRole(\"Dcat_Client\") || hasRole(\"Ecat_Client\") || hasRole(\"Fcat_Client\") || hasRole(\"Gcat_Client\") || hasRole(\"Hcat_Client\") || hasRole(\"Icat_Client\") || hasRole(\"Jcat_Client\") || hasRole(\"Kcat_Client\") || hasRole(\"Lcat_Client\") || hasRole(\"Mcat_Client\") || hasRole(\"Ncat_Client\") || hasRole(\"Ocat_Client\") || hasRole(\"Pcat_Client\") || hasRole(\"Qcat_Client\") || hasRole(\"Rcat_Client\") || hasRole(\"Scat_Client\") || hasRole(\"Tcat_Client\") || hasRole(\"Ucat_Client\") || hasRole(\"Vcat_Client\") || hasRole(\"Wcat_Client\") || hasRole(\"Xcat_Client\") || hasRole(\"Ycat_Client\") || hasRole(\"Zcat_Client\")','VIP',1,'000000','/html/rank_icons/VIP.png','','','','',3,0,'2016-09-27 07:33:14'),(5,4,'Administrator',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 805) && (minutes_online >= 6125) && (net_kudos_weight_received >= 1225) && (net_kudos_weight_given >= 350) && hasRole(\"Administrator\")','Super Star',1,'fd4403','/html/rank_icons/rank_superstar.png','','','','',3,0,'2016-07-07 01:48:03'),(6,5,'Administrator',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 630) && (minutes_online >= 4775) && (net_kudos_weight_received >= 1000) && (net_kudos_weight_given >= 240) && hasRole(\"Administrator\")','Rising Star',1,'fb8717','/html/rank_icons/rank_risingstar.png','','','','',3,0,'2016-07-07 01:48:03'),(7,6,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 480) && (minutes_online >= 3650) && (net_kudos_weight_received >= 800) && (net_kudos_weight_given >= 160)','Star',1,'fcb223','/html/rank_icons/rank_star.png','','','','',3,0,'2016-07-07 01:48:03'), (8,7,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 355) && (minutes_online >= 2750) && (net_kudos_weight_received >= 625) && (net_kudos_weight_given >= 100)','Honored Member',1,'663399','/html/rank_icons/rank_honoredmember.gif','','','','',2,1,'2016-07-25 19:20:02'), (9,8,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 280) && (minutes_online >= 2050) && (net_kudos_weight_received >= 475) && (net_kudos_weight_given >= 60)','Esteemed Member',1,'9933CC','/html/rank_icons/rank_esteemedmember.gif','','','','',2,1,'2016-07-25 19:19:43'),(10,9,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 215) && (minutes_online >= 1375) && (net_kudos_weight_received >= 350) && (net_kudos_weight_given >= 30)','Respected Member',1,'CC00CC','/html/rank_icons/rank_respectedmember.gif','','','','',2,1,'2016-07-25 19:19:27'),(11,10,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 150) && (minutes_online >= 775) && (net_kudos_weight_received >= 250) && (net_kudos_weight_given >= 5)','Trusted Member',1,'FF66CC','/html/rank_icons/rank_trustedmember.gif','','','','',2,1,'2016-07-25 19:19:12'),(12,11,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 90) && (logins >= 10) && (minutes_online >= 225)','Super Contributor',0,'0f68d6','/html/rank_icons/rank_supercontributor.png','','','','',1,1,'2016-07-25 19:18:52'),(13,12,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 45) && (logins >= 7) && (minutes_online >= 135)','Regular Contributor',0,'02b1cb','/html/rank_icons/rank_Regularcontributor.png','','','','',1,1,'2016-07-25 19:18:43'),(14,13,'',-1,0,0,0,0,0,0,0,1,'(overall_posts >= 10) && (logins >= 5) && (minutes_online >= 75) && (net_kudos_wei

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: MySQLSQLAUInnoDBTIAUTCUUTRYRIM
分页:12
转载请注明
本文标题:MySQL may return results in non-deterministic order with ‘order by’
本站链接:http://www.codesec.net/view/534260.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(47)