Friday, February 7, 2014

Pagination

There are instances when you want to display large result sets to the end user. The best way to display large result set is to split them i.e.  apply pagination. So developers had their own hacky ways of achieving pagination using “top”, “row_number” etc. But from SQL Server 2012 onwards we can do pagination by using “OFFSET” and “FETCH’ commands.
For instance let’s says we have the following customer table which has 12 records. We would like to split the records in to 6 and 6.

So doing pagination is a two-step process: -
  • First mark the start of the row by using “OFFSET” command.
  • Second specify how many rows you want to fetch by using “FETCH” command.
You can see in the below code snippet we have used “OFFSET” to mark the start of row from “0”position. A very important note order by clause is compulsory for “OFFSET” command.
select * from
tblcustomer order by customercode
offset 0 rows – start from zero
In the below code snippet we have specified we want to fetch “6” rows from the start “0”position specified in the “OFFSET”.
fetch next 6 rows only
Now if you run the above SQL you should see 6 rows.

To fetch the next 6 rows just change your “OFFSET” position. You can see in the below code snippet I have modified the offset to 6. That means the row start position will from “6”.
select * from
tblcustomer order by customercode
offset 6 rows



fetch next 6 rows only
The above code snippet displays the next “6” records , below is how the output looks.

Sequence Object in SQL Server 2012



This feature is personally feel it just mimics Oracle’s sequence objects. if Oracle has it why not SQL Server. A sequence object generates sequence of unique numeric values as per specifications. we have something similar like this called as “Identity” columns. But the big difference is sequence object is independent of a table while identity columns are attached to a table.

Below is a simple code to create a sequence object. You can see we have created a sequence object called as “MySeq” with the following specification:-

  • Starts with value 1.
  • Increments with value 1 Minimum value it should start is with zero.
  • Maximum it will go to 100. No cycle defines that once it reaches 100 it will throw an error.
  • If you want to restart it from 0 you should provide “cycle”.
  • “cache 50”  specifies that till 50 the values are already incremented in to cache to reduce IO. If you specify “no cache” it will make input output on the disk.

 

create sequence MySeq as int

               start with 1  -- Start with value 1

               increment by 1-- Increment with value 1

               minvalue 0 -- Minimum value to start is zero

               maxvalue 100 -- Maximum it can go to 100

               no cycle -- Do not go above 100

               cache 50 -- Increment 50 values in memory rather than incrementing from

IO

To increment the value we need to call the below select statement. This is one more big difference as compared to identity.In identity the values increment when rows are added here we need to make an explicit call.

SELECT NEXT VALUE FOR dbo.MySequence AS seq_no;

 Once you exceed to the MaxValue of Sequence SQL-Server throws error

Msg 11728, Level 16, State 1, Line 1

The sequence object 'TestSEQ' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.

 Limitations and Restrictions

The NEXT VALUE FOR function cannot be used in the following situations

·         When a database is in read-only mode

·         In subqueries including common table expressions and derived tables.

·         In views, in user-defined functions, or in computed columns

·         In a statement using the DISTINCT, UNION, UNION ALL, EXCEPT or INTERSECT operator.

·         In a statement using the ORDER BY clause unless NEXT VALUE FOR … OVER (ORDER BY …) is used.

·         In a VALUES clause that is not part of an INSERT statement.

·         In a statement using TOP, OFFSET, or when the ROWCOUNT option is set.

·         In the WHERE clause of a statement.

 You can get one or more values out of the sequence object. You can also ALTER the sequence object to RESTART it, or change many of its properties

-- Restart the sequence from 1

 ALTER SEQUENCE dbo.TestSEQ

RESTART WITH 2

INCREMENT BY 2

MINVALUE 2

MAXVALUE 10

CACHE 20

NO CYCLE;

Note – Alter statement cannot have the Start Value




 

Column Store Indexes

To get the Column Index right click on the indexes folder as “Non-Clustered Column store Index” , as shown in the below figure.


So let’s quickly understand what exactly it does. Now Relational database store data “row wise”. These rows are further stored in 8 KB page size.
For instance you can see in the below figure we have table with two columns “Column1” and “Column2”. You can see how the data is stored in two pages i.e. “page1” and “page2”. “Page1” has two rows and “page2” also has two rows. Now if you want to fetch only “column1”, you have to pull records from two pages i.e. “Page1” and “Page2”, see below for the visuals.
As we have to fetch data from two pages its bit performance intensive.


If somehow we can store data column wise we can avoid fetching data from multiple pages. That’s what column store indexes do. When you create a column store index it stores same column data in the same page. You can see from the below visuals, we now need to fetch “column1” data only from one page rather than querying multiple pages.