search
HomeDatabaseMysql TutorialSome advanced uses of limit in mysql

Some advanced uses of limit in mysql

May 10, 2018 am 10:07 AM
limitmysqlusage

Usually when we use sql statements, we use some relatively simple usages. Below, I will use some examples to talk about some advanced usage methods of limit in Mysql.


mysql limit efficiency:

select `id`,`title`,`descript`,`created` from myvbga_table where click = xxx limit offset, limit; //Summary: If there is no blob/text field and the single-line record is relatively small, you can set the limit larger, which will speed up the process.

limit offset value is relatively small:

select `id`,`title`,`descript`,`created` from vbga_table limit 10,10 //Multiple times Run, the time remains between 0.0004-0.0005

Select `id`,`title`,`describe`,`created` From vbga_table Where click >=(Select click From vbga_table Order By click limit 10,1 ) limit 10 //Multiple runs, the time remains between 0.0005-0.0006, mainly 0.0006

limit offset value is relatively large:

select `id`, `title`,`describe`,`created` from vbga_table limit 10000,10 //Run multiple times, the time remains around 0.0187

Select `id`,`title`,`descript`,`created` From vbga_table Where click >=(Select click From vbga_table Order By click limit 10000,1) limit 10 //Run multiple times, the time remains at around 0.0061, only 1/3 of the former. It can be expected that the larger the offset, the better the latter.

Mysql limit usage:

The LIMIT clause can be used to force the SELECT statement to return the specified number of records

SELECT `id`,` title`,`descript`,`created` FROM vbga_table LIMIT [offset,] rows | rows OFFSET offset

mysql> SELECT `id`,`title`,`descript`,`created` FROM vbga_table LIMIT 5 ,10; //Retrieve record rows 6-15 //In order to retrieve all record rows from a certain offset to the end of the recordset, you can specify the second parameter as -1:

mysql> SELECT `id`,`title`,`describe`,`created` FROM vbga_table LIMIT 95,-1; //Retrieve record rows 96-last. //If only one parameter is given, it means returning the maximum number of record rows:

mysql> SELECT `id`,`title`,`describe`,`created` FROM vbga_table LIMIT 5; //Retrieve the first 5 record rows//In other words, LIMIT n is equivalent to LIMIT 0 ,n.

mysql limit subquery usage example:

select `id`,`title`,`descript`,`created` from vbga_table where id in (select t. id from (select `id`,`title`,`descript`,`created` from vbga_table limit 10)as t)

mysql limit offset usage:

SELECT keyword FROM `zjoe_table` WHERE advertiserid='59' order by keyword LIMIT 2 OFFSET 1; //For example, in this SQL, limit is followed by 2 pieces of data, and offset is followed by reading from the 1st piece.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

SELECT `keyword` FROM `zjoe_table` WHERE advertiserid='59' ORDER BY keyword LIMIT 2,1; //And this SQL, after limit is to start reading from the 2nd item, and read 1 piece of information.

Usage of limit variable in mysql stored procedure

CREATE PROCEDURE Getble_table(_id int,_limit int)

BEGIN

PREPARE s1 FROM 'SELECT `id`,`title`,`descript`,`created` FROM ble_table WHERE Cityid=? ORDER BY sendtime DESC LIMIT ?';

set @a=_id;

set @b=_limit;

EXECUTE s1 USING @a,@b;

DEALLOCATE PREPARE s1;

END;

The above is what I summarized Some advanced usage of limit in Mysql, I hope it will be helpful to everyone in the future.
Related articles:

How to read data through PHP MySQL

PHP related to connecting to MySQL Knowledge and its operations

The above is the detailed content of Some advanced uses of limit in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.