Home Database Mysql Tutorial MySQL、SQL Server、Oracle数据库分页查询及分析(操作手册)

MySQL、SQL Server、Oracle数据库分页查询及分析(操作手册)

Jun 07, 2016 pm 03:10 PM

MySQL、SQL Server、Oracle数据库分页查询及分析(操作手册)

1、MySQL分页查询

方式1:

select * from table order by id limit m, n;

  该语句的意思为,查询m+n条记录,去掉前m条,返回后n条记录。无疑该查询能够实现分页功能,但是如果m的值越大,查询的性能会越低(越后面的页数,查询性能越低),因为MySQL同样需要扫描过m+n条记录。

方式2:

select * from table where id > #max_id# order by id limit n;

  该查询每次会返回n条记录,却无需像方式1扫描过m条记录,在大数据量的分页情况下,性能可以明显好于方式1,但该分页查询必须要每次查询时拿到上一次查 询(上一页)的一个最大id(或最小id)。该查询的问题就在于,我们有时没有办法拿到上一次查询(上一页)的最大id(或最小id),比如当前在第3 页,需要查询第5页的数据,该查询方法便爱莫能助了。

方式3:

  为了避免能够实现方式2不能实现的查询,就同样需要使用到limit m, n子句,为了性能,就需要将m的值尽力的小,比如当前在第3页,需要查询第5页,每页10条数据,当前第3页的最大id为#max_id#:

select * from table where id > #max_id# order by id limit 20, 10;

  其实该查询方式是部分解决了方式2的问题,但如果当前在第2页,需要查询第100页或1000页,性能仍然会较差。

方式4:

select * from table as a inner join (select id from table order by id limit m, n) as b on a.id = b.id order by a.id;

  该查询同方式1一样,m的值可能很大,但由于内部的子查询只扫描了字段id,而不是整张表,,所以性能要强于方式1查询,并且该查询能够解决方式2和方式3不能解决的问题。

方式5:

select * from table where id > (select id from table order by id limit m, 1) limit n;

  该查询方式同方式4,同样通过子查询扫描字段id,效果同方式4。至于性能的话,方式5的性能会略好于方式4,因为方式5不需要在进行表的关联,而是一个简单的比较。

--------------------------------------------------------------------------------

2、SQL Server分页查询

方式1:

  假设页数是10,现在要拿出第5页的内容,查询语句如下:
  --10代表分页的大小

select top 10 *
from test
where id not in
(
 --40是这么计算出来的:10*(5-1)
 select top 40 id from test order by id
)
order by id

  原理:需要拿出数据库的第5页,就是40-50条记录。首先拿出数据库中的前40条记录的id值,然后再拿出剩余部分的前10条元素

方式2:

  还是以上面的结果为例,采用另外的一种方法
  --数据的意思和上面提及的一样

select top 10 *
from test
where id >
(
 select isnull(max(id),0)
 from
  (
  select top 40 id from test order by id
  ) A
)
order by id

  原理:先查询前40条记录,然后获得其最id值,如果id值为null的,那么就返回0,然后查询id值大于前40条记录的最大id值的记录。这个查询有一个条件,就是id必须是int类型的。

方式3:

select top 10 *
from
(
 select row_number() over(order by id) as rownumber,* from test
) A
where rownumber > 40

  原理:先把表中的所有数据都按照一个rowNumber进行排序,然后查询rownuber大于40的前十条记录
  这种方法和Oracle中的一种分页方式类似,不过只支持2005版本以上的

方式4:

  存储过程查询
   创建存储过程

alter procedure pageDemo
@pageSize int,
@page int
AS
declare @temp int
set @temp=@pageSize*(@page - 1)
begin
 select top (select @pageSize) * from test where id not in (select top (select @temp) id from test) order by id
end
执行存储过程
exec 10,5

--------------------------------------------------------------------------------

3、Oracle分页查询

方式1:

  根据ROWID来分

select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from(select rowid rid,cid from
t_xiaoxi  order by cid desc) where rownum9980) order by cid desc;

  执行时间0.03秒

方式2:

  按分析函数来分

select * from (select t.*,row_number() over(order by cid desc) rk from t_xiaoxi t) where rk9980;

  执行时间1.01秒

方式3:

  按ROWNUM来分

select * from(select t.*,rownum rn from(select * from t_xiaoxi order by cid desc) t where rownum

  rn>9980;执行时间0.1秒

  其中t_xiaoxi为表名称,cid为表的关键字段,取按CID降序排序后的第9981-9999条记录,t_xiaoxi表有70000多条记录
  个人感觉1的效率最好,3次之,2最差

本文永久更新链接地址

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

Video Face Swap

Video Face Swap

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

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)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

See all articles