Română (România)English (United Kingdom)French (Fr)
Wednesday, 07 April 2010 10:56

MSSQLs’ Version of MySQLs’ LIMIT

One of MySQLs’ things I like, besides being free, is the LIMIT clause. The syntax is simple – SELECT * FROM Table A WHERE 1=1 LIMIT OFFSET, ROW_COUNT, where offset is the offset of the first row to return and row_count is the number of rows to return. This clause allows web programmers to paginate a large set of records from a database.

Pagination is useful when you are dealing with huge numbers of records. Let’s take for example the case of a Bookstore that decides to open virtual store. It has more than 100,000 books for online sale. If a customer visits the website and loads the books page, he will have to wait a while until all of the records are displayed and after that he will have to scroll through thousands of records to find a book. Now let’s multiply the number of online customers by 1,000, all accessing the books page simultaneously. About 100 million records will be queried from the database – imagine the overhead. This is where the magic of pagination comes in – if you decide to display only 50 records per page and let the customer go to the next 50 records (by accessing the next page) you will increase the readability and reduce the loading time of the page and reduce the overhead. In this case only 50,000 records will be queried, that’s half the number of records of the first query per customer.

Published in Programming