Detailed explanation of the pitfalls of mixing MySQL order by and limit

Detailed explanation of the pitfalls of mixing MySQL order by and limit

In MySQL, we often use order by for sorting and limit for paging. When we need to sort first and then paging, we often use a similar expression: select * from table name order by sort field limt M,N. However, this writing style hides deeper usage traps. When there is duplicate data in the sorting field, it is easy for the sorting results to be inconsistent with expectations.
As shown in the table:

insert image description here

insert image description here

When querying the first and last pages:

insert image description here

insert image description here

Solution:

SELECT * FROM purchaseinfo ORDER BY actiontime,id LIMIT 0,2;

The actual execution results above have proved that there is often a gap between reality and imagination. The actual SQL execution is not executed in the above way. In fact, MySQL will optimize Limit here. For specific optimization methods, see the official document: https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html

/*
Navicat MySQL Data Transfer

Source Server: Local mysql8.0
Source Server Version : 80018
Source Host : localhost:3308
Source Database : baihe

Target Server Type : MYSQL
Target Server Version : 80018
File Encoding: 65001

Date: 2020-06-09 14:47:37
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for purchaseinfo
-- ----------------------------
DROP TABLE IF EXISTS `purchaseinfo`;
CREATE TABLE `purchaseinfo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) DEFAULT '0',
  `inout` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
  `money` int(11) DEFAULT NULL,
  `actiontime` datetime DEFAULT NULL COMMENT 'jiaoyi',
  PRIMARY KEY (`id`),
  KEY `UserId` (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of purchaseinfo
-- ----------------------------
INSERT INTO `purchaseinfo` VALUES ('7', '1', 'I', '10000', '2020-06-09 18:05:41');
INSERT INTO `purchaseinfo` VALUES ('8', '7788', 'O', '20000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('9', '7788', 'I', '20000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('10', '7788', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('11', '1', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('12', '1', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('13', '1', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('14', '1', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('15', '1', 'O', '50000', '2020-06-09 18:05:15');
INSERT INTO `purchaseinfo` VALUES ('16', '1', 'O', '50000', '2020-06-09 18:05:15');

This is the end of this article about the pitfalls of mixing MySQL order by and limit. For more information about mixing MySQL order by and limit, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to data duplication when using limit+order by in MySql paging
  • Solution to the problem that order by is not effective in MySQL subquery
  • Summary of three ways to implement ranking in MySQL without using order by
  • How to use MySQL group by and order by together
  • How to use indexes to optimize MySQL ORDER BY statements
  • Mysql sorting and paging (order by & limit) and existing pitfalls
  • Query process and optimization method of (JOIN/ORDER BY) statement in MySQL
  • MySQL briefly understands how "order by" works
  • Detailed explanation of Mysql's method of optimizing order by statement
  • Details on using order by in MySQL

<<:  A practical guide to Vue project first screen performance optimization components

>>:  The design process of Alibaba's Mid-Autumn Festival logo combined with the website

Recommend

Two box models in web pages (W3C box model, IE box model)

There are two types of web page box models: 1: Sta...

Summary of pitfalls encountered in installing mysql and mysqlclient on centos7

1. Add MySQL Yum repository MySQL official websit...

Detailed tutorial on installing pxc cluster with docker

Table of contents Preface Preliminary preparation...

Detailed explanation of commonly used nginx rewrite rules

This article provides some commonly used rewrite ...

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

How to choose and use PNG, JPG, and GIF as web image formats

So which one of these formats, GIF, PNG, and JPG,...

Solution to MySQL 8.0 cannot start 3534

MySQL 8.0 service cannot be started Recently enco...

Understand the principle of page replacement algorithm through code examples

Page replacement algorithm: The essence is to mak...

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take ef...

MySQL permissions and database design case study

Permissions and database design User Management U...

BUG of odd width and height in IE6

As shown in the figure: But when viewed under IE6...

Use of Linux dynamic link library

Compared with ordinary programs, dynamic link lib...

JS realizes the case of eliminating stars

This article example shares the specific code of ...