MySQL与分页_MySQL
如果和MSSQL的TOP语法相比,那么MySQL的LIMIT语法要显得优雅了许多。使用它来分页是再自然不过的事情了。
最基本的分页方式:
SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ...
在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就是确保使用了索引:
举例来说,如果实际SQL类似下面语句,那么在category_id, id两列上建立复合索引比较好:
SELECT * FROM articles WHERE category_id = 123 ORDER BY id LIMIT 50, 10
子查询的分页方式:
随着数据量的增加,页数会越来越多,查看后几页的SQL就可能类似:
SELECT * FROM aricles WHERE category_id = 123 ORDER BY id LIMIT 10000, 10
一言以蔽之,就是越往后分页,LIMIT语句的偏移量就会越大,速度也会明显变慢。
此时,我们可以通过子查询的方式来提高分页效率,大致如下:
SELECT * FROM articles WHERE category_id = 123 AND id >= (
SELECT id FROM articles ORDER BY id LIMIT 10000, 1
) LIMIT 10

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Go and context elegantly for error handling In Go programming, error handling is a very important task. Handling errors gracefully improves code readability, maintainability, and stability. The context package of Go language provides us with a very convenient way to handle error-related operations. This article will introduce how to use Go and context elegantly for error handling, and provide relevant code examples. Introduction The error handling mechanism of Go language is implemented by returning an error value.

Introduction to Laravel Framework: An Elegant New Choice for PHP Development Introduction: Over the past few years, the field of PHP development has been constantly evolving and growing. Among the many PHP frameworks, Laravel is undoubtedly one of the most popular frameworks. It is loved by developers for its clear and concise syntax, comprehensive functionality and excellent performance. This article will introduce you to the basic features of the Laravel framework and demonstrate its flexible and elegant development style through code examples. 1. Features of Laravel framework 1. Elegant generation

The switch statement in the PHP language is a control flow structure used to execute different code blocks according to different conditions. Normally, after each case statement is executed, a break statement is used to jump out of the switch statement. But sometimes, we want to continue executing the next case or multiple cases without using break. This article will introduce how to elegantly use switch statements in PHP development without using break statements. First, let’s look at a simple swit

PyCharm Tips: Handle the code automatic line wrapping problem gracefully. In the process of using PyCharm for Python programming, you often encounter the code automatic line wrapping problem, especially when a line of code is too long, automatic line wrapping may affect the readability and beauty of the code. sex. How to deal with this problem gracefully and make the code cleaner and more readable? This article will introduce some techniques for dealing with code automatic line wrapping in PyCharm, and demonstrate it through specific code examples. 1. Using backslash() in Python

PHP Error Handling: How to Handle Error Messages Elegantly Introduction: Error handling is a very important aspect when writing PHP code. Whether you're developing a new application or maintaining legacy code, you need to consider how to handle error messages. Correctly handling error messages can improve the robustness and maintainability of your application. This article will introduce some ways to handle PHP error messages gracefully and provide code examples. Error reporting settings In PHP, you can decide how to handle error messages by setting the error reporting level.

PHP exception handling: gracefully handle errors in the program Introduction: During the development process, various errors will inevitably occur in the program. These errors can be caused by incorrect user input, server configuration errors, database connection issues, and more. In order to ensure the stability and reliability of the program, we need to handle these errors correctly. PHP exception handling mechanism provides an elegant and efficient way to handle errors in your program. This article will introduce the principles and practices of PHP exception handling in detail and provide some code examples.

In today's software development field, frameworks are one of the indispensable tools for developers. It can help developers quickly build applications, improve development efficiency, and reduce development costs. As a popular and powerful code editor, Visual Studio Code (VSCode for short) combined with various frameworks can provide developers with a more efficient and convenient development experience. Choose a framework that suits your project. Before using a framework, you first need to choose a suitable framework based on your project needs and technology stack. often

With the development of the Internet, microservice architecture is becoming more and more popular among Internet companies. SpringCloud provides a complete microservice solution based on SpringBoot. This article will introduce how to use the Spring Cloud framework to practice microservices. 1. Overview of microservice architecture The so-called microservice architecture is to split the application into some smaller, autonomous service units. The services work together through lightweight communication mechanisms. Each service can be built and deployed independently.
