MySQL提高分頁效率_Mysql

來源:互聯網
上載者:User

下面就是大資料量時提高分頁的效率的測試代碼,分享給大家。

--提高分頁效率:實現分頁時唯讀取顯示資料,需要先在資料庫建立資料庫“TestForPaging”use TestForPaginggo--建立表SomeDatacreate table SomeData(id int primary key,name varchar(30) null,description text)go--插入資料insert into SomeData values(1,'num1','第1條')goinsert into SomeData values(2,'num2','第2條')goinsert into SomeData values(3,'num3','第3條')goinsert into SomeData values(4,'num4','第4條')goinsert into SomeData values(5,'num5','第5條')go--資料條目總數select count(*) from SomeDatago--給每條記錄添加一個資料層級select name,description,ROW_NUMBER() over(order by id desc)as dataLevel from SomeDatago--查看指定的資料層級間的資料條目select dataLevel,name,description from(select name,description,row_number() over(order by id desc)as dataLevel from SomeData) as datawithleverl where dataLevel between 2 and 4go--實現查看指定的資料層級間的資料條目的預存程序create procedure GetDataPaged(@startRowIndex int,@maximumRows int,@sort varchar)AS--確保指定sortif len(@sort)=0set @sort='id'--帶參數的查詢select dataLevel,name,description from(select name,description,row_number() over(order by @sort desc)as dataLevel from SomeData) AS datawithleverlWHERE dataLevel > (@startRowIndex*10) AND dataLevel <= (@startRowIndex*10 + @maximumRows)go

以上就是本文的的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.