Home Database Mysql Tutorial oracle分页查询sql原理与语句

oracle分页查询sql原理与语句

Jun 07, 2016 pm 05:46 PM
Paging query

很多朋友使用多了mysql limit进行分页到了oracle中就不知道如何写分页了,其实oracle分页与mssql分页很像的,下面我来给大家介绍oracle分页语句。

很多朋友使用多了mysql limit进行分页到了oracle中就不知道如何写分页了,其实oracle分页与mssql分页很像的,下面我来给大家介绍oracle分页语句。

最简单使用方法

 代码如下 复制代码

t2.*
from (select rownum r,t1.* from youtable t1 where rownum) t2
where t2.r>?

小于最大的,大于最小的,就是你需要的条数.如果你取40-50条之间的

查询格式:

 代码如下 复制代码

SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM )
WHERE RN >= 21

其中最内层的查询SELECT * FROM TABLE_NAME表示不进行翻页的原始查询语句。ROWNUM = 21控制分页查询的每页的范围。
上面给出的这个分页查询语句,在大多数情况拥有较高的效率。分页的目的就是控制输出结果集大小,将结果尽快的返回。在上面的分页查询语句中,这种考虑主要体现在WHERE ROWNUM 选择第21到40条记录存在两种方法,一种是上面例子中展示的在查询的第二层通过ROWNUM

 代码如下 复制代码

SELECT * FROM

(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
)
WHERE RN BETWEEN 21 AND 40

后来发现数据越大就查询越慢了,后来得出一些测试结果分享给大家。

--1:无ORDER BY排序的写法。(效率最高)
--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!)

 代码如下 复制代码
SELECT *
  FROM (Select ROWNUM AS ROWNO, T.*
           from k_task T
          where Flight_date between to_date('20060501', 'yyyymmdd') and
                to_date('20060731', 'yyyymmdd')
            AND ROWNUM WHERE TABLE_ALIAS.ROWNO >= 10;

--2:有ORDER BY排序的写法。(效率最高)
--(经过测试,此方法随着查询范围的扩大,速度也会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (SELECT TT.*, ROWNUM AS ROWNO
           FROM (Select t.*
                    from k_task T
                   where flight_date between to_date('20060501', 'yyyymmdd') and
                         to_date('20060531', 'yyyymmdd')
                   ORDER BY FACT_UP_TIME, flight_no) TT
          WHERE ROWNUM where TABLE_ALIAS.rowno >= 10;

--3:无ORDER BY排序的写法。(建议使用方法1代替)
--(此方法随着查询数据量的扩张,速度会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (Select ROWNUM AS ROWNO, T.*
           from k_task T
          where Flight_date between to_date('20060501', 'yyyymmdd') and
                to_date('20060731', 'yyyymmdd')) TABLE_ALIAS
WHERE TABLE_ALIAS.ROWNO    AND TABLE_ALIAS.ROWNO >= 10;
--TABLE_ALIAS.ROWNO  between 10 and 100;

--4:有ORDER BY排序的写法.(建议使用方法2代替)
--(此方法随着查询范围的扩大,速度会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (SELECT TT.*, ROWNUM AS ROWNO
           FROM (Select *
                    from k_task T
                   where flight_date between to_date('20060501', 'yyyymmdd') and
                         to_date('20060531', 'yyyymmdd')
                   ORDER BY FACT_UP_TIME, flight_no) TT) TABLE_ALIAS
where TABLE_ALIAS.rowno BETWEEN 10 AND 20;


--5另类语法。(有ORDER BY写法)
--(语法风格与传统的SQL语法不同,不方便阅读与理解,为规范与统一标准,不推荐使用。)

 代码如下 复制代码
With partdata as(
  SELECT ROWNUM AS ROWNO, TT.*  FROM (Select *
                  from k_task T
                 where flight_date between to_date('20060501', 'yyyymmdd') and
                       to_date('20060531', 'yyyymmdd')
                 ORDER BY FACT_UP_TIME, flight_no) TT
   WHERE ROWNUM     Select * from partdata where rowno >= 10;

--6另类语法 。(无ORDER BY写法)

 代码如下 复制代码

With partdata as(
  Select ROWNUM AS ROWNO, T.*
    From K_task T
   where Flight_date between to_date('20060501', 'yyyymmdd') and
         To_date('20060531', 'yyyymmdd')
     AND ROWNUM     Select * from partdata where Rowno >= 10;   

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP database connection to implement paging query How to use PHP database connection to implement paging query Sep 08, 2023 pm 02:28 PM

How to use PHP database connection to implement paging query. When developing web applications, it often involves the need to query the database and perform paging display. As a commonly used server-side scripting language, PHP has powerful database connection functions and can easily implement paging queries. This article will introduce in detail how to use PHP database connection to implement paging query, and attach corresponding code examples. Prepare the database Before we start, we need to prepare a database containing the data to be queried. Here we take the MySQL database as an example,

PHP and PDO: How to perform paging queries and display data PHP and PDO: How to perform paging queries and display data Jul 29, 2023 pm 04:10 PM

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

How to create high-performance MySQL data paging queries using Go language How to create high-performance MySQL data paging queries using Go language Jun 17, 2023 am 09:09 AM

With the rapid development of the Internet, data processing has become an important skill in enterprise application development. MySQL database is often one of the most commonly used data stores in many applications. In MySQL, data paging query is a common data retrieval operation. This article will introduce how to use Go language to implement high-performance MySQL data paging query. 1. What is data paging query? Data paging query is a commonly used data retrieval technology, which allows users to browse only a small amount of data on a page without loading it all at once.

How to use Mysql for paging query in ThinkPHP6 How to use Mysql for paging query in ThinkPHP6 Jun 20, 2023 pm 02:01 PM

With the rapid development of the Internet, the development of web applications is becoming more and more complex. And paging query is one of the common functions in web applications. ThinkPHP6 is a web framework that helps developers develop applications quickly. In this article, we will discuss how to perform paginated queries using MySQL in ThinkPHP6. First, we need to create the database in ThinkPHP6. The statement to create a database in MySQL is as follows: CREATEDATABASE

How to use MongoDB with PHP for paginated query How to use MongoDB with PHP for paginated query Jul 07, 2023 pm 09:28 PM

Overview of how PHP uses MongoDB for paginated queries: MongoDB is a non-relational database often used to store large amounts of document data, while PHP is a popular server-side scripting language. In this article, we will introduce how to use PHP to connect to MongoDB and implement paging query function. Step 1: Install the MongoDB extension. To interact with MongoDB in PHP, you need to install the MongoDB extension. The MongoDB extension can be installed with the following command: p

Paging query skills for PHP and Oracle database Paging query skills for PHP and Oracle database Jul 11, 2023 pm 11:09 PM

Paging query techniques for PHP and Oracle database When developing a dynamic web page, if you need to display a large amount of data, you need to perform paging query. Paginated query is a technique that divides data into smaller pages so that users can browse and navigate easily. In this article, we will discuss how to implement paginated queries using PHP and Oracle database, and provide relevant code examples. 1. Preparation Before starting, we need to make sure that we have installed and configured PHP and Oracle database. If still

How to use thinkorm to easily implement paging query function How to use thinkorm to easily implement paging query function Jul 31, 2023 pm 10:41 PM

Overview of how to use thinkorm to easily implement paging query function: When developing a website or application, it is often necessary to perform paging query on the data in the database in order to display part of the data on the page and provide page turning function. This article will introduce how to use the thinkorm framework to easily implement the paging query function, and provide relevant code examples. Step 1: Install thinkorm First, you need to install the thinkorm framework in the project. You can use the following command to install it: composerrequi

MySql paging query: How to deal with performance issues of paging in large databases MySql paging query: How to deal with performance issues of paging in large databases Jun 15, 2023 pm 03:28 PM

In modern applications, most data needs to be displayed in pages. When applications need to process large amounts of data, this puts pressure on servers and databases, causing queries to take longer. Mysql is currently one of the most popular relational databases. This article will discuss how to optimize the performance of Mysql paging queries. Principle of paging Before starting optimization, we must first understand the principle of paging. The main principle of paging query is to divide the table data into several pages, and then query the data one by one. For example, if we need to

See all articles