偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

數(shù)據(jù)庫查詢的分頁優(yōu)化技巧

數(shù)據(jù)庫
本文介紹一種常用的技巧:通過簡單的重新構造一下查詢SQL語句從而大幅提高查詢性能的方法。

分頁瀏覽功能是常見的Web應用功能,對于MySQL數(shù)據(jù)庫來說可以很輕松的使用limit語句實現(xiàn)分頁,而對于SQL Server數(shù)據(jù)庫來說,常見的方法是使用數(shù)據(jù)集本身的游標實現(xiàn)分頁,這種方法對于少量數(shù)據(jù)來說沒什么問題,但是對于稍大一點的數(shù)據(jù)量,例如幾十萬條數(shù)據(jù),則查詢速度會降低很多,這里我介紹一種常用的技巧,只要簡單的重新構造一下查詢SQL語句,就能大幅提高查詢性能的方法。

在分頁算法中,影響查詢速度的關鍵因素在于返回數(shù)據(jù)集的大小,我們先在數(shù)據(jù)表中設置一個名為id的主鍵,數(shù)值為自增量的整數(shù),然后通過重構查詢SQL語句,就可以實現(xiàn)SQL查詢的優(yōu)化,重構的SQL如下所示:

select top 頁大小 *
from table1
where id<=
(select min (id) from
(select top ((頁碼-1)*頁大小) id from table1 order by id desc) as T
)
order by id desc


  下面的JSP演示代碼中,intPageSize為頁大小,intPage為頁碼,id為主鍵,演示了操作一個t_Product表,并加入各類查詢條件之后的重構SQL的主要語句,經(jīng)過實際調(diào)試,經(jīng)過這樣簡單優(yōu)化后的SQL查詢速度遠遠高于優(yōu)化前的查詢速度。

String sql=" from t_Product where 1=1 and ";
String ProductName = request.getParameter("ProductName");
if (ProductName!=null) sql=sql+"ProductName like '%" + ProductName + "%' and " ;
sql=sql.substring(0,sql.length()-4); // 去掉尾部的 and 字符串
sql="select top " + String.valueOf(intPageSize) + " *" +sql+" and id <=(select min(id) from (select top " + String.valueOf(intPage*intPageSize) + " id " + sql + " order by id desc) as T) "; //通過子查詢加快速度
sql=sql+" order by id desc ";

【編輯推薦】

  1. 三種優(yōu)化MySQL數(shù)據(jù)庫查詢的方法簡介
  2. 優(yōu)化SQL Server數(shù)據(jù)庫查詢方法
  3. Oracle數(shù)據(jù)庫查詢的五個技巧
責任編輯:yangsai 來源: 月光博客
相關推薦

2009-07-06 21:20:34

SQL Server數(shù)

2012-07-23 14:30:33

Oracle

2011-05-19 10:29:40

數(shù)據(jù)庫查詢

2010-08-26 14:39:54

Infobright數(shù)

2013-01-04 10:00:12

MySQL數(shù)據(jù)庫數(shù)據(jù)庫查詢優(yōu)化

2013-05-21 10:06:11

數(shù)據(jù)庫查詢優(yōu)化

2011-08-15 10:22:19

分頁查詢數(shù)據(jù)庫

2011-03-14 13:51:21

LAMPMySQL

2021-01-31 17:50:41

數(shù)據(jù)庫查詢程序員

2023-07-12 08:55:16

PawSQL數(shù)據(jù)庫

2010-11-25 14:21:16

MySQL查詢分頁

2018-04-10 14:36:18

數(shù)據(jù)庫MySQL優(yōu)化技巧

2009-07-01 10:01:33

JSP分頁查詢MySQL數(shù)據(jù)庫

2011-04-02 09:33:13

MySQL數(shù)據(jù)庫查詢效率

2011-04-02 09:23:19

MySQL數(shù)據(jù)庫查詢效率

2011-03-01 16:30:55

Oracle

2011-04-02 09:33:08

MySQL數(shù)據(jù)庫查詢效率

2011-03-11 16:25:53

Oracle數(shù)據(jù)庫

2010-08-27 10:20:11

DB2數(shù)據(jù)庫優(yōu)化

2022-06-20 05:40:25

數(shù)據(jù)庫MySQL查詢
點贊
收藏

51CTO技術棧公眾號