Pagination Done the Right Way

Markus Winand
Markus WinandTrainer, Coach, Author at http://winand.at/
© 2013 by Markus Winand
iStockPhoto/Mitshu
Pagination done
the Right Way
@MarkusWinand
@SQLPerfTips
© 2013 by Markus Winand
Takeaways
‣OFFSET kills performance
‣Paging can be done without OFFSET
‣It’s faster and has fewer side effects
© 2013 by Markus Winand
Note
In this presentation
index means B-tree index.
A Trivial Example
A query to fetch the 10 most recent news:
se!e"# *
$%om news
whe%e #op&" ' 1234
o!de! b" da#e des$, %d des$
&%m%# 10;
$!ea#e %ndex .. on news(#op%$);
Using o%de% b( to get the most recent first and
!&m&# to fetch only the first 10.
Alternative SQL-2008 syntax (since PostgreSQL 8.4)
$e#"h $&%s# 10 %ows on!(
Worst Case: No Index for o%de% b(
L&m&# (a$#ua& !ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (!ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (!ows'10000)
Index *ond: (#op&" ' 1234)
Worst Case: No Index for order by
The limiting factor is the number of rows that match
the whe%e clause
(“Base-Set Size”).
The database might use
an index to satisfy the
whe%e clause, but must
still fetch all matching
rows to “sort” them.
Another Benchmark: Fetch Next Page
Fetching the next page is easy using the o$$se#
keyword:
se!e"# *
$%om news
whe%e #op&" ' 1234
o%de% b( da#e des", &d des"
o))se# 10
!&m&# 10;
Worst Case: No Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'20)
So%# Me#hod: #op-N heapso!# Memo!": 19(B
-> B&#map Heap S"an (a"#ua! %ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (a"#ua! %ows'10000)
Index *ond: (#op&" ' 1234)
Worst Case: No Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'30)
So%# Me#hod: #op-N heapso!# Memo!": 20(B
-> B&#map Heap S"an (a"#ua! %ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (a"#ua! %ows'10000)
Index *ond: (#op&" ' 1234)
Worst Case: No Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'40)
So%# Me#hod: #op-N heapso!# Memo!": 22(B
-> B&#map Heap S"an (a"#ua! %ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (a"#ua! %ows'10000)
Index *ond: (#op&" ' 1234)
Worst Case: No Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10000)
So%# Me#hod: ex#e!na& me!*e D%s(: 1200(B
-> B&#map Heap S"an (a"#ua! %ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (a"#ua! %ows'10000)
Index *ond: (#op&" ' 1234)
Worst Case: No Index for order by
Sorting might become the limiting factor when
browsing farther back.
Fetching the last page
can take considerably
longer than fetching
the first page.
Improvement 1: Indexed order by
se!e"# *
$%om news
whe%e #op&" ' 1234
o%de% b( da#e des", &d des"
o$$se# 10
!&m&# 10;
$!ea#e %ndex .. on news (#op%$, da#e, %d);
A single index to support the whe%e and o%de% b(
clauses.
Improvement 1: Indexed order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: (#op&" ' 0)
Improvement 1: Indexed order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'20)
Index *ond: (#op&" ' 0)
Improvement 1: Indexed order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'30)
Index *ond: (#op&" ' 0)
Improvement 1: Indexed order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'40)
Index *ond: (#op&" ' 0)
Improvement 1: Indexed order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10000)
So%# Me#hod: ex#e!na& me!*e D%s(: 1200(B
-> B&#map Heap S"an (a"#ua! %ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (a"#ua! %ows'10000)
Index *ond: (#op&" ' 1234)
Improvement 1: Indexed order by
Fetching the first page is not affected by the
Base-Set size!
Fetching the next page
is also faster.
However, PostgreSQL
might take a Bitmap
Index Scan when
browsing to the end.
© 2013 by Markus Winand
We can do better!
© 2013 by Markus Winand
Don’t touch what you don’t need
Improvement 2: The Seek Method
Instead of o$$se#, use a whe%e filter to remove the
rows from previous pages.
se!e"# *
$%om news
whe%e #op&" ' 1234
and (da#e, %d) < (p!e+_da#e, p!e+_%d)
o%de% b( da#e des", &d des"
!&m&# 10;
Only select the rows “before” (=earlier date) the
last!row from the previous page.
A definite sort order is really required!
© 2013 by Markus Winand
Side Note: Row Values
Besides scalar values, SQL also defines
“row values” or “composite values.”
‣ In the SQL standard since ages (SQL-92)
‣ All comparison operators are well defined
‣ E.g.: (x, y) > (a, b) is true iff
(x > a or (x=a and y>b))
‣ In other words, when (x,y) sorts after (a,b)
‣ Great PostgreSQL support since 8.0!
© 2013 by Markus Winand
Row Values: How it works
© 2013 by Markus Winand
Row Values: How it works
© 2013 by Markus Winand
Row Values: How it works
© 2013 by Markus Winand
Row Values: Emulating them
‣ There is only a single database that has sufficient
Row-Values support for this trick (PostgreSQL).
‣ With all other database, you must emulate it
according to this scheme:
!"#$#%&'()*+',)%-.%/
%%%%012%3
%%%%%%%%%%%3&'()*+',)%-%/4
%%%%%%%%%5$
%%%%%%%%%%%3&'()*+',)%.%/%012%&'()*6+%-%/4
%%%%%%%%4
See: http://use-the-index-luke.com/sql/partial-results/fetch-next-page#sb-equivalent-logic
Seek-Method without Optimal Index
se!e"# *
$%om news
whe%e #op&" ' 1234
and (da#e, &d) < (p%e+_da#e, p%e+_&d)
o%de% b( da#e des", &d des"
!&m&# 10;
$!ea#e %ndex .. on news (#op%$);
Seek Method w/o Index for order by
L&m&# (a$#ua& !ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (!ows'10000)
Re"he") *ond: (#op&" ' 1234)
-> B&#map Index S"an (!ows'10000)
Index *ond: (#op&" ' 1234)
Seek Method w/o Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (a$#ua& !ows'9990)
Rows Remo+ed b" F%&#e!: 10 (new &n 9.2)
-> B&#map Index S"an (a$#ua& !ows'10000)
Index *ond: (#op&" ' 1234)
Seek Method w/o Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (a$#ua& !ows'9980)
Rows Remo+ed b" F%&#e!: 20 (new &n 9.2)
-> B&#map Index S"an (a$#ua& !ows'10000)
Index *ond: (#op&" ' 1234)
Seek Method w/o Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (a$#ua& !ows'9970)
Rows Remo+ed b" F%&#e!: 30 (new &n 9.2)
-> B&#map Index S"an (a$#ua& !ows'10000)
Index *ond: (#op&" ' 1234)
Seek Method w/o Index for order by
L&m&# (a"#ua! %ows'10)
-> So%# (a$#ua& !ows'10)
So%# Me#hod: #op-N heapso!# Memo!": 18(B
-> B&#map Heap S"an (a$#ua& !ows'10)
Rows Remo+ed b" F%&#e!: 9990 (new &n 9.2)
-> B&#map Index S"an (a$#ua& !ows'10000)
Index *ond: (#op&" ' 1234)
Seek Method w/o Index for order by
Always needs to retrieve the full base set, but the
top-n sort buffer needs
to hold only 10 rows.
The response time remains
the same even when
browsing to the last page.
And the memory footprint
is very low!
Seek-Method with Optimal Index
se!e"# *
$%om news
whe%e #op&" ' 1234
and (da#e, &d) < (p%e+_da#e, p%e+_&d)
o%de% b( da#e des", &d des"
!&m&# 10;
$!ea#e %ndex .. on news (#op%$, da#e, %d);
Seek Method with index for order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: (#op&" ' 1234)
Seek Method with index for order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 23456)))
Seek Method with index for order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 34567)))
Seek Method with index for order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 45678)))
Seek Method with index for order by
L&m&# (a$#ua& !ows'10)
-> Index S"an Ba")wa%d (a$#ua& !ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 56789)))
Seek Method with index for order by
Successively browsing back doesn’t slow down.
Neither the size of the
base set(*) nor the fetched
page number affects the
response time.
(*) the index tree depth still affects the response time.
ComparisonOffsetSeek
W/O index for o%de% b( With index for o%de% b(
© 2013 by Markus Winand
Too good to be true?
The Seek Method has serious limitations
‣You cannot directly navigate to arbitrary pages
‣because you need the values from the previous page
‣Bi-directional navigation is possible but tedious
‣you need to revers the o%de% b( direction and RV comparison
‣Works best with full row values support
‣Workaround is possible, but ugly and less performant
‣Framework support?
‣jOOQ 3.3 introduced native support for the Seek-Method
‣order_query for Ruby by @glebm: http://github.com/glebm/order_query
© 2013 by Markus Winand
A Perfect Match for Infinite Scrolling
The “Infinite Scrolling” UI doesn’t need to ...
‣navigate to arbitrary pages
‣there are no buttons
‣Browse backwards
‣previous pages are
still in the browser
‣Stable results
‣New rows don’t affect the result
No need for de-duplication in JS
Also a Perfect Match for PostgreSQL
o%de% b(
support matrix
row values
support matrix
Popular
Popular
Advanced
Advanced
© 2013 by Markus Winand
About Markus Winand
Tuning developers for
high SQL performance
Training & co (one-man show):
winand.at
Geeky blog:
use-the-index-luke.com
Author of:
SQL Performance Explained
1 of 46

Recommended

Modern SQL in Open Source and Commercial Databases by
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesMarkus Winand
450.8K views174 slides
JavaOne 2011: Migrating Spring Applications to Java EE 6 by
JavaOne 2011: Migrating Spring Applications to Java EE 6JavaOne 2011: Migrating Spring Applications to Java EE 6
JavaOne 2011: Migrating Spring Applications to Java EE 6Bert Ertman
119.7K views72 slides
N1QL: What's new in Couchbase 5.0 by
N1QL: What's new in Couchbase 5.0N1QL: What's new in Couchbase 5.0
N1QL: What's new in Couchbase 5.0Keshav Murthy
1.2K views68 slides
Indexes: The neglected performance all rounder by
Indexes: The neglected performance all rounderIndexes: The neglected performance all rounder
Indexes: The neglected performance all rounderMarkus Winand
35.5K views49 slides
Backend to Frontend: When database optimization affects the full stack by
Backend to Frontend: When database optimization affects the full stackBackend to Frontend: When database optimization affects the full stack
Backend to Frontend: When database optimization affects the full stackMarkus Winand
3.7K views52 slides
Csharp intsight by
Csharp intsightCsharp intsight
Csharp intsightÁlvaro Cortés Téllez
780 views126 slides

More Related Content

Similar to Pagination Done the Right Way

Un petit guide de la domination du monde by
Un petit guide de la domination du mondeUn petit guide de la domination du monde
Un petit guide de la domination du mondeGeoffrey Dorne
2.3K views29 slides
OSGi - beyond the myth by
OSGi -  beyond the mythOSGi -  beyond the myth
OSGi - beyond the mythClément Escoffier
612 views196 slides
E-Primer Your Business Online by
E-Primer Your Business OnlineE-Primer Your Business Online
E-Primer Your Business Onlineguestfc9d8a
133 views24 slides
Al Fazl International Weekly26 June 2015 by
Al Fazl International  Weekly26 June 2015Al Fazl International  Weekly26 June 2015
Al Fazl International Weekly26 June 2015muzaffertahir9
659 views20 slides
FCIP SASS Talk by
FCIP SASS TalkFCIP SASS Talk
FCIP SASS TalkChris Schneider
716 views36 slides
The Lean Startup - simplified by
The Lean Startup - simplifiedThe Lean Startup - simplified
The Lean Startup - simplifiedStefano Bernardi
2.2K views19 slides

Similar to Pagination Done the Right Way(20)

Un petit guide de la domination du monde by Geoffrey Dorne
Un petit guide de la domination du mondeUn petit guide de la domination du monde
Un petit guide de la domination du monde
Geoffrey Dorne2.3K views
E-Primer Your Business Online by guestfc9d8a
E-Primer Your Business OnlineE-Primer Your Business Online
E-Primer Your Business Online
guestfc9d8a133 views
Al Fazl International Weekly26 June 2015 by muzaffertahir9
Al Fazl International  Weekly26 June 2015Al Fazl International  Weekly26 June 2015
Al Fazl International Weekly26 June 2015
muzaffertahir9659 views
Interaction design by feifei2011
Interaction designInteraction design
Interaction design
feifei2011422 views
treinamento-sdh-alcatel_compress.pdf by Master22
treinamento-sdh-alcatel_compress.pdftreinamento-sdh-alcatel_compress.pdf
treinamento-sdh-alcatel_compress.pdf
Master2210 views
LAMP_TRAINING_SESSION_6 by umapst
LAMP_TRAINING_SESSION_6LAMP_TRAINING_SESSION_6
LAMP_TRAINING_SESSION_6
umapst405 views
What the !@#$ is UX? A fun and concise introduction by Sean Buch
What the !@#$ is UX? A fun and concise introductionWhat the !@#$ is UX? A fun and concise introduction
What the !@#$ is UX? A fun and concise introduction
Sean Buch86 views
referente by betsycaru
referentereferente
referente
betsycaru142 views
Understanding variations of entanglement and complexity: A way to influence e... by RSD7 Symposium
Understanding variations of entanglement and complexity: A way to influence e...Understanding variations of entanglement and complexity: A way to influence e...
Understanding variations of entanglement and complexity: A way to influence e...
RSD7 Symposium541 views
Roberto Isaias - Derechos humanos by Roberto Isaias
Roberto Isaias - Derechos humanosRoberto Isaias - Derechos humanos
Roberto Isaias - Derechos humanos
Roberto Isaias188 views
William Isaías-Derechos humanos by William Isaias
William Isaías-Derechos humanosWilliam Isaías-Derechos humanos
William Isaías-Derechos humanos
William Isaias305 views

More from Markus Winand

Standard SQL features where PostgreSQL beats its competitors by
Standard SQL features where PostgreSQL beats its competitorsStandard SQL features where PostgreSQL beats its competitors
Standard SQL features where PostgreSQL beats its competitorsMarkus Winand
58.9K views77 slides
Four* Major Database Releases of 2017 in Review by
Four* Major Database Releases of 2017 in ReviewFour* Major Database Releases of 2017 in Review
Four* Major Database Releases of 2017 in ReviewMarkus Winand
1.5K views29 slides
Row Pattern Matching in SQL:2016 by
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Markus Winand
109.4K views92 slides
SQL Transactions - What they are good for and how they work by
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workMarkus Winand
33.9K views72 slides
Volkskrankheit "Stiefmuetterliche Indizierung" by
Volkskrankheit "Stiefmuetterliche Indizierung"Volkskrankheit "Stiefmuetterliche Indizierung"
Volkskrankheit "Stiefmuetterliche Indizierung"Markus Winand
16K views62 slides
SQL Performance - Vienna System Architects Meetup 20131202 by
SQL Performance - Vienna System Architects Meetup 20131202SQL Performance - Vienna System Architects Meetup 20131202
SQL Performance - Vienna System Architects Meetup 20131202Markus Winand
1.8K views16 slides

More from Markus Winand(6)

Standard SQL features where PostgreSQL beats its competitors by Markus Winand
Standard SQL features where PostgreSQL beats its competitorsStandard SQL features where PostgreSQL beats its competitors
Standard SQL features where PostgreSQL beats its competitors
Markus Winand58.9K views
Four* Major Database Releases of 2017 in Review by Markus Winand
Four* Major Database Releases of 2017 in ReviewFour* Major Database Releases of 2017 in Review
Four* Major Database Releases of 2017 in Review
Markus Winand1.5K views
Row Pattern Matching in SQL:2016 by Markus Winand
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
Markus Winand109.4K views
SQL Transactions - What they are good for and how they work by Markus Winand
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they work
Markus Winand33.9K views
Volkskrankheit "Stiefmuetterliche Indizierung" by Markus Winand
Volkskrankheit "Stiefmuetterliche Indizierung"Volkskrankheit "Stiefmuetterliche Indizierung"
Volkskrankheit "Stiefmuetterliche Indizierung"
Markus Winand16K views
SQL Performance - Vienna System Architects Meetup 20131202 by Markus Winand
SQL Performance - Vienna System Architects Meetup 20131202SQL Performance - Vienna System Architects Meetup 20131202
SQL Performance - Vienna System Architects Meetup 20131202
Markus Winand1.8K views

Recently uploaded

Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveNetwork Automation Forum
49 views35 slides
"Surviving highload with Node.js", Andrii Shumada by
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada Fwdays
49 views29 slides
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITShapeBlue
138 views8 slides
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineShapeBlue
154 views19 slides
Kyo - Functional Scala 2023.pdf by
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
443 views92 slides
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOsPriyanka Aash
103 views59 slides

Recently uploaded(20)

Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays49 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue138 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue154 views
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by Priyanka Aash
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOs
Priyanka Aash103 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue63 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue120 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue69 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson142 views
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by ShapeBlue
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
ShapeBlue59 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue75 views
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ... by ShapeBlue
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
ShapeBlue121 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue48 views
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue52 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE67 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc130 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue172 views

Pagination Done the Right Way

  • 1. © 2013 by Markus Winand iStockPhoto/Mitshu Pagination done the Right Way @MarkusWinand @SQLPerfTips
  • 2. © 2013 by Markus Winand Takeaways ‣OFFSET kills performance ‣Paging can be done without OFFSET ‣It’s faster and has fewer side effects
  • 3. © 2013 by Markus Winand Note In this presentation index means B-tree index.
  • 4. A Trivial Example A query to fetch the 10 most recent news: se!e"# * $%om news whe%e #op&" ' 1234 o!de! b" da#e des$, %d des$ &%m%# 10; $!ea#e %ndex .. on news(#op%$); Using o%de% b( to get the most recent first and !&m&# to fetch only the first 10. Alternative SQL-2008 syntax (since PostgreSQL 8.4) $e#"h $&%s# 10 %ows on!(
  • 5. Worst Case: No Index for o%de% b( L&m&# (a$#ua& !ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (!ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (!ows'10000) Index *ond: (#op&" ' 1234)
  • 6. Worst Case: No Index for order by The limiting factor is the number of rows that match the whe%e clause (“Base-Set Size”). The database might use an index to satisfy the whe%e clause, but must still fetch all matching rows to “sort” them.
  • 7. Another Benchmark: Fetch Next Page Fetching the next page is easy using the o$$se# keyword: se!e"# * $%om news whe%e #op&" ' 1234 o%de% b( da#e des", &d des" o))se# 10 !&m&# 10;
  • 8. Worst Case: No Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'20) So%# Me#hod: #op-N heapso!# Memo!": 19(B -> B&#map Heap S"an (a"#ua! %ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (a"#ua! %ows'10000) Index *ond: (#op&" ' 1234)
  • 9. Worst Case: No Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'30) So%# Me#hod: #op-N heapso!# Memo!": 20(B -> B&#map Heap S"an (a"#ua! %ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (a"#ua! %ows'10000) Index *ond: (#op&" ' 1234)
  • 10. Worst Case: No Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'40) So%# Me#hod: #op-N heapso!# Memo!": 22(B -> B&#map Heap S"an (a"#ua! %ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (a"#ua! %ows'10000) Index *ond: (#op&" ' 1234)
  • 11. Worst Case: No Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10000) So%# Me#hod: ex#e!na& me!*e D%s(: 1200(B -> B&#map Heap S"an (a"#ua! %ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (a"#ua! %ows'10000) Index *ond: (#op&" ' 1234)
  • 12. Worst Case: No Index for order by Sorting might become the limiting factor when browsing farther back. Fetching the last page can take considerably longer than fetching the first page.
  • 13. Improvement 1: Indexed order by se!e"# * $%om news whe%e #op&" ' 1234 o%de% b( da#e des", &d des" o$$se# 10 !&m&# 10; $!ea#e %ndex .. on news (#op%$, da#e, %d); A single index to support the whe%e and o%de% b( clauses.
  • 14. Improvement 1: Indexed order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: (#op&" ' 0)
  • 15. Improvement 1: Indexed order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'20) Index *ond: (#op&" ' 0)
  • 16. Improvement 1: Indexed order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'30) Index *ond: (#op&" ' 0)
  • 17. Improvement 1: Indexed order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'40) Index *ond: (#op&" ' 0)
  • 18. Improvement 1: Indexed order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10000) So%# Me#hod: ex#e!na& me!*e D%s(: 1200(B -> B&#map Heap S"an (a"#ua! %ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (a"#ua! %ows'10000) Index *ond: (#op&" ' 1234)
  • 19. Improvement 1: Indexed order by Fetching the first page is not affected by the Base-Set size! Fetching the next page is also faster. However, PostgreSQL might take a Bitmap Index Scan when browsing to the end.
  • 20. © 2013 by Markus Winand We can do better!
  • 21. © 2013 by Markus Winand Don’t touch what you don’t need
  • 22. Improvement 2: The Seek Method Instead of o$$se#, use a whe%e filter to remove the rows from previous pages. se!e"# * $%om news whe%e #op&" ' 1234 and (da#e, %d) < (p!e+_da#e, p!e+_%d) o%de% b( da#e des", &d des" !&m&# 10; Only select the rows “before” (=earlier date) the last!row from the previous page. A definite sort order is really required!
  • 23. © 2013 by Markus Winand Side Note: Row Values Besides scalar values, SQL also defines “row values” or “composite values.” ‣ In the SQL standard since ages (SQL-92) ‣ All comparison operators are well defined ‣ E.g.: (x, y) > (a, b) is true iff (x > a or (x=a and y>b)) ‣ In other words, when (x,y) sorts after (a,b) ‣ Great PostgreSQL support since 8.0!
  • 24. © 2013 by Markus Winand Row Values: How it works
  • 25. © 2013 by Markus Winand Row Values: How it works
  • 26. © 2013 by Markus Winand Row Values: How it works
  • 27. © 2013 by Markus Winand Row Values: Emulating them ‣ There is only a single database that has sufficient Row-Values support for this trick (PostgreSQL). ‣ With all other database, you must emulate it according to this scheme: !"#$#%&'()*+',)%-.%/ %%%%012%3 %%%%%%%%%%%3&'()*+',)%-%/4 %%%%%%%%%5$ %%%%%%%%%%%3&'()*+',)%.%/%012%&'()*6+%-%/4 %%%%%%%%4 See: http://use-the-index-luke.com/sql/partial-results/fetch-next-page#sb-equivalent-logic
  • 28. Seek-Method without Optimal Index se!e"# * $%om news whe%e #op&" ' 1234 and (da#e, &d) < (p%e+_da#e, p%e+_&d) o%de% b( da#e des", &d des" !&m&# 10; $!ea#e %ndex .. on news (#op%$);
  • 29. Seek Method w/o Index for order by L&m&# (a$#ua& !ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (!ows'10000) Re"he") *ond: (#op&" ' 1234) -> B&#map Index S"an (!ows'10000) Index *ond: (#op&" ' 1234)
  • 30. Seek Method w/o Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (a$#ua& !ows'9990) Rows Remo+ed b" F%&#e!: 10 (new &n 9.2) -> B&#map Index S"an (a$#ua& !ows'10000) Index *ond: (#op&" ' 1234)
  • 31. Seek Method w/o Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (a$#ua& !ows'9980) Rows Remo+ed b" F%&#e!: 20 (new &n 9.2) -> B&#map Index S"an (a$#ua& !ows'10000) Index *ond: (#op&" ' 1234)
  • 32. Seek Method w/o Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (a$#ua& !ows'9970) Rows Remo+ed b" F%&#e!: 30 (new &n 9.2) -> B&#map Index S"an (a$#ua& !ows'10000) Index *ond: (#op&" ' 1234)
  • 33. Seek Method w/o Index for order by L&m&# (a"#ua! %ows'10) -> So%# (a$#ua& !ows'10) So%# Me#hod: #op-N heapso!# Memo!": 18(B -> B&#map Heap S"an (a$#ua& !ows'10) Rows Remo+ed b" F%&#e!: 9990 (new &n 9.2) -> B&#map Index S"an (a$#ua& !ows'10000) Index *ond: (#op&" ' 1234)
  • 34. Seek Method w/o Index for order by Always needs to retrieve the full base set, but the top-n sort buffer needs to hold only 10 rows. The response time remains the same even when browsing to the last page. And the memory footprint is very low!
  • 35. Seek-Method with Optimal Index se!e"# * $%om news whe%e #op&" ' 1234 and (da#e, &d) < (p%e+_da#e, p%e+_&d) o%de% b( da#e des", &d des" !&m&# 10; $!ea#e %ndex .. on news (#op%$, da#e, %d);
  • 36. Seek Method with index for order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: (#op&" ' 1234)
  • 37. Seek Method with index for order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: ((#op&" ' 1234) AND (ROW(d#, &d) < ROW(‘...’, 23456)))
  • 38. Seek Method with index for order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: ((#op&" ' 1234) AND (ROW(d#, &d) < ROW(‘...’, 34567)))
  • 39. Seek Method with index for order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: ((#op&" ' 1234) AND (ROW(d#, &d) < ROW(‘...’, 45678)))
  • 40. Seek Method with index for order by L&m&# (a$#ua& !ows'10) -> Index S"an Ba")wa%d (a$#ua& !ows'10) Index *ond: ((#op&" ' 1234) AND (ROW(d#, &d) < ROW(‘...’, 56789)))
  • 41. Seek Method with index for order by Successively browsing back doesn’t slow down. Neither the size of the base set(*) nor the fetched page number affects the response time. (*) the index tree depth still affects the response time.
  • 42. ComparisonOffsetSeek W/O index for o%de% b( With index for o%de% b(
  • 43. © 2013 by Markus Winand Too good to be true? The Seek Method has serious limitations ‣You cannot directly navigate to arbitrary pages ‣because you need the values from the previous page ‣Bi-directional navigation is possible but tedious ‣you need to revers the o%de% b( direction and RV comparison ‣Works best with full row values support ‣Workaround is possible, but ugly and less performant ‣Framework support? ‣jOOQ 3.3 introduced native support for the Seek-Method ‣order_query for Ruby by @glebm: http://github.com/glebm/order_query
  • 44. © 2013 by Markus Winand A Perfect Match for Infinite Scrolling The “Infinite Scrolling” UI doesn’t need to ... ‣navigate to arbitrary pages ‣there are no buttons ‣Browse backwards ‣previous pages are still in the browser ‣Stable results ‣New rows don’t affect the result No need for de-duplication in JS
  • 45. Also a Perfect Match for PostgreSQL o%de% b( support matrix row values support matrix Popular Popular Advanced Advanced
  • 46. © 2013 by Markus Winand About Markus Winand Tuning developers for high SQL performance Training & co (one-man show): winand.at Geeky blog: use-the-index-luke.com Author of: SQL Performance Explained