MySQL Oracle and SQL Server paging query example analysis

MySQL Oracle and SQL Server paging query example analysis

Recently, I have done a simple study on the data paging query of Oracle, MySQL, and SQL Server 2005, and posted the query statements of each for everyone to learn...

(I) Paging query of MySQL

MySQL's paging query is the simplest. You can use the keyword limit to query. The general query statement is:

select o.*from(sql) o limit firstIndex,pageSize

As shown in the following screenshot, the number of records displayed per page is 20:

Query (1-20) these 20 records

Query these 20 records (21-40)


MySQL paging query is so simple...

(II) Paging query of sqlserver2005

Before SQL Server 2005, the top keyword was used to implement paging query, but the efficiency was low. In SQL Server 2005 and later versions, the row_number() analytical function is used to complete paging query, which has greatly improved the efficiency. However, the SQL statement is more complicated. The general formula of paging query is given below:

selecttoppageSizeo.*from(selectrow_number()over(orderbyorderColumn)asrownumber,*from(sql)asowhererownumber>firstIndex;

Look at the screenshot below, 20 records are displayed per page:

Query (1-20) these 20 records

Query these 20 records (21-40)

Knowing the row_number function in sqlserver, paging is easy...

(III) Oracle paging query

Next, let's focus on Oracle's paging query. Oracle has relatively more paging query methods, ROWNUM and row_number(). Today we will mainly focus on two paging query statements with slightly better efficiency.

①ROWNUM query paging formula:

select*from(selecta.*,ROWNUMrn from(sql)a whereROWNUM<=(firstIndex+pageSize))wherern>firstIndex

The following screenshot shows a query executed in this way:

Query (1-21) these 20 records***** (there is no record with ID=6, so the maximum ID queried is 21)



Query (22-41) these 20 records***** (there is no record with ID=6, so the first ID to be queried is 22, and the maximum ID is 41)

②row_number() parsing function paging query method:

select*from(select*from(selectt.*,row_number()over(orderbyorderColumn)asrownumberfrom(sql)t) p wherep.rownumber>firstIndex)whererownum<=pageSize

The following screenshot shows the paging query effect using row_number():

Query (1-21) these 20 records***** (there is no record with ID=6, so the maximum ID queried is 21)



Query (22-41) these 20 records***** (there is no record with ID=6, so the first ID to be queried is 22, and the maximum ID is 41)


In the ROWNUM query method, there is a " where ROWNUM<(firstIndex+pageSize)order by idasc in the second-level SQL statement, which queries (firstIndex+pageSize) records from the data table. So if this value is very small, the efficiency will be very good. If this value is thousands for a large data form, for example: select* from wyuse where rownum<(5000) order by idasc , 5000 records will be selected at the beginning, and the efficiency will naturally be much slower....

Summarize

The above is the introduction of MySQL Oracle and SQL Server paging query by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Solution to the paging error problem of MySQL one-to-many association query
  • MySQL million-level data paging query optimization solution
  • Optimizing the performance of paging query for MySQL with tens of millions of data
  • Detailed explanation of MySQL limit usage and performance analysis of paging query statements
  • Detailed explanation of Mysql Limit paging query optimization
  • Introduction to the differences between paging query statements in Oracle, MySQL and SqlServe
  • Detailed explanation of paging query methods in mysql, mssql and oracle
  • Comparison of two solutions for paging query in MySQL
  • Examples of paging queries for three databases: oracle, mysql, and SqlServer
  • Several implementation methods and advantages and disadvantages of SQL paging query in MySQL

<<:  How to use JSZip compression in CocosCreator

>>:  Tomcat uses Log4j to output catalina.out log

Recommend

Solve the problem of specifying udp port number in docker

When Docker starts a container, it specifies the ...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

Vue Element-ui form validation rule implementation

Table of contents 1. Introduction 2. Entry mode o...

An example of refactoring a jigsaw puzzle game using vue3

Preface It took two days to reconstruct a puzzle ...

vue-element-admin global loading waiting

Recent requirements: Global loading, all interfac...

Should the Like function use MySQL or Redis?

Table of contents 1. Common mistakes made by begi...

Analyze the duration of TIME_WAIT from the Linux source code

Table of contents 1. Introduction 2. First, let&#...

The difference between this.$router and this.$route in Vue and the push() method

The official document states: By injecting the ro...

How to install and uninstall open-vswitch in Linux

1. Compile and install ovs from source code: Inst...

Mini Program to Implement Sieve Lottery

This article example shares the specific code of ...

HTML uncommon tags optgroup, sub, sup and bdo example code

Optgroup is used in the select tag to make the dro...

Detailed Analysis of Explain Execution Plan in MySQL

Preface How to write efficient SQL statements is ...