About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Tuesday, December 23, 2014

Pagination prior to SQL Server 2012



Pagination is common requirement for any application, where we pull data in chunks depending upon the page number and size of page. Here in this article we will see how we can achieve pagination through SQL server prior to SQ: Server 2012,

Here we are declaring a table Employee with three fields.

create table Employees (
empid int NOT NULL,
empname varchar(25) NOT NULL,
salary decimal(15,2)
)
Command(s) completed successfully.

Lets insert few data in the table.

declare @i int = 0
while @i < 10000
begin
insert into Employees
select round(rand() * 10000,0),'abc' + cast(round(rand()*10,0) as varchar), rand()*1000
set @i = @i + 1
end

Now since our table is ready, its time to enter implement pagination, here in the below script, we have taken 2 variable, @PageNo, which indicate which page to see for data and second one is @Size which says the number of records in each page. Here we are using Row_Number ranking function, Approach is

  • ·         We are giving a row number in sequence to all rows of table using ROW_NUMBER Rank function.
  • ·         Then according to the value of page number and page size, we are extracting the records.
  • ·         Eg. If @PageNo is 4 and row @PageSize is 5 then the row to return should  have 20-25 as row numbers.


declare @pageNo int
set @pageNo=1
declare @Size int
set @Size=5
select * from (select ROW_NUMBER ()  over ( order by empid asc ) as rowno , employees.* from Employees ) s
where s.rowno > (@pageNo*@Size) and s.rowno <=((@pageNo+1)*@Size)

rowno                empid       empname                   salary
-------------------- ----------- ------------------------- ---------
1                    10          abc5                      80.35
2                    11          abc9                      287.90
3                    12          abc5                      815.79
4                    13          abc5                      433.73
5                    14          abc4                      645.19


That’s all one need to do to implement Pagination in SQL Server Prior to SQL Server 2012.

Everything gets better when you have tea and SQL Server J


Post Reference: Vikram Aristocratic Elfin Share

Tuesday, December 16, 2014

Partitioning Large table


Partitioning is a feature is introduced to aid in maintenance on larger tables and to present fast ways to load and remove large amounts of data from a table. Partitioning can boost query performance.

Each table has a default partition which we call it PRIMARY partition which contain all data. To add partitions, you need to define a partition function and the corresponding partition scheme. A partition scheme uses the partition function to map data ranges to appropriate file groups. If the partition function defines three ranges, then partition scheme must map them to four file groups. Three ranges defined by the partition function and the forth, catchall range, for all data outside the boundaries of the predefined ranges.

Lets take an example to understand the partition. Here we are creating a table with three field id, name and salary. Later we will base of partition on ID field.

create table tab_partitionDemo
(id int identity(1,1),
name varchar(150),
salary int)
Command(s) completed successfully.

Lets insert 1000 dumy records in newly created table.

declare @rwCnt int = 10000
while @rwCnt >= 1
begin
     insert into tab_partitionDemo values('abc' + cast(@rwCnt as varchar), 10*rwCnt)
     set @rwCnt = @rwcnt -1
end

Now we have our table ready, let add two file group. A filegroup is a logical storage unit. Every database has a primary filegroup that contains the primary data file (.mdf). An additional, user-defined, filegrups can be created to contain secondary files (.ndf). We will create two file group here, one which will store first 5,000 records and the other one will store data from 5,000 to 10,000.

ALTER DATABASE TestDB ADD FILEGROUP partitionDemoFG01_50K 
GO 
ALTER DATABASE TestDB ADD FILEGROUP partitionDemoFG50K_10K 
GO 
Command(s) completed successfully.

Now its turn to add file(.ndf) to the newly created filegroup, which will hold data.

ALTER DATABASE TestDB
  ADD FILE
  (NAME = N'data_01_10',
  FILENAME = N'D:\SQL Server\Data\data_FilePartitionDemoF01_50K.ndf',
  SIZE = 5000KB,
  MAXSIZE = 10000MB,
  FILEGROWTH = 500MB)
  TO FILEGROUP partitionDemoFG01_50K 
GO 
ALTER DATABASE TestDB
  ADD FILE
  (NAME = N'data_11_20',
  FILENAME = N'D:\SQL Server\Data\data_partitionDemoF50K_10K.ndf',
  SIZE = 5000KB,
  MAXSIZE = 10000MB,
  FILEGROWTH = 500MB)
  TO FILEGROUP partitionDemoFG50K_10K
GO 
Command(s) completed successfully.

To check the file created we can query sys.database_files

SELECT name as [FileName], physical_name as [FilePath]
FROM sys.database_files where type_desc = 'ROWS'
GO

Now its time to create partition function, a partition function is one which maps the table data to the partition based upon the partition column. Here in below code we are creating 3 partition on the bases of int value field i.e ID.

CREATE PARTITION FUNCTION IDPartitionFunction (int)
AS
RANGE LEFT FOR VALUES (5000,10000 );
Command(s) completed successfully.

Now we need to map the partition to each partition group with the help of Partition Schema.

CREATE PARTITION SCHEME IDPartitionSchema
AS PARTITION IDPartitionFunction
TO (partitionDemoFG01_50K,partitionDemoFG50K_10K,[PRIMARY]);
Command(s) completed successfully.

Now we need to redistribute the table data to various partition, for that we need to either create clustered index on selective table column or if it already present then drop and recreate with On clause on Partition column.

create clustered index ix_tab_partitionDemo_id
on  dbo.tab_partitionDemo(ID)
ON IDPartitionSchema(id)
Command(s) completed successfully.

This is the only thing one need to do for partition, lets check whether our data get distributed to various partition by querying sys.index table.

SELECT o.name objectname,i.name indexname, partition_id, partition_number, [rows]
FROM sys.partitions p
INNER JOIN sys.objects o ON o.object_id=p.object_id
INNER JOIN sys.indexes i ON i.object_id=p.object_id and p.index_id=i.index_id
WHERE o.name LIKE '%tab_partitionDemo%'

objectname        indexname               partition_id      partition_number  rows
tab_partitionDemo ix_tab_partitionDemo_id 72057594148093952 1                 5000
tab_partitionDemo ix_tab_partitionDemo_id 72057594148159488 2                 5000
tab_partitionDemo ix_tab_partitionDemo_id 72057594148225024 3                 0

Here we saw 10K records of the table get distributed to two partition. Now lets add one more records with partition column value more then 10k.

insert into dbo.tab_partitionDemo values('Rakesh',40000)

SELECT o.name objectname,i.name indexname, partition_id, partition_number, [rows]
FROM sys.partitions p
INNER JOIN sys.objects o ON o.object_id=p.object_id
INNER JOIN sys.indexes i ON i.object_id=p.object_id and p.index_id=i.index_id
WHERE o.name LIKE '%tab_partitionDemo%'

objectname        indexname               partition_id      partition_number  rows
tab_partitionDemo ix_tab_partitionDemo_id 72057594148093952 1                 5000
tab_partitionDemo ix_tab_partitionDemo_id 72057594148159488 2                 5000
tab_partitionDemo ix_tab_partitionDemo_id 72057594148225024 3                 1

So we can see the newly created row go to primary partition. That’s what we were expecting. Cool!!!

SQL Server, where there is mountain of happiness sits :)


Post Reference: Vikram Aristocratic Elfin Share

Sunday, December 14, 2014

Database bakup with PDF extension

Yes, I know you drag your buttock to the edge of your chair, be aware don’t let it fall on ground, they are precious, handle it with care :D. SQL Server does not have problem with the file extension as long as the file is a valid file. So you can name you bakup extension with any type.

But general industry standard says .BAK for full bakup, .DIFF for Differential and .TRN for transactional backup.

Let try out with .PDF extension.

BACKUP DATABASE [TestDB] TO  DISK = N'D:\SQL Server\myDatabaseBakup.pdf'
WITH FORMAT,  MEDIANAME = N'MyTest1',  NAME = N'TestDB-Full Database Backup'
GO
Processed 11432 pages for database 'TestDB', file 'TestDB' on file 1.
Processed 2 pages for database 'TestDB', file 'TestDB_log' on file 1.
BACKUP DATABASE successfully processed 11434 pages in 5.080 seconds (17.582 MB/sec).

Lets now try to restore it.

USE [master]
RESTORE DATABASE [MyTest101] FROM  DISK = N'D:\SQL Server\myDatabaseBakup.pdf'
WITH  FILE = 1,  MOVE N'TestDB' TO N'D:\SQL Server\Log\MyTest101.mdf',  MOVE N'TestDB_log' TO N'D:\SQL Server\Log\MyTest101_log.ldf'
GO
Processed 11432 pages for database 'MyTest101', file 'TestDB' on file 1.
Processed 2 pages for database 'MyTest101', file 'TestDB_log' on file 1.
RESTORE DATABASE successfully processed 11434 pages in 4.236 seconds (21.086 MB/sec).

Conclusion: SQL Server does not have problem with the file extension as long as the file is a valid file.

For coders world is colorful, this is just because of you SQL Server :) Ah! You are evergreen!!!

Post Reference: Vikram Aristocratic Elfin Share

Know the type of bakup and database without restoring it to your database

I was killing out time with one of my SQL DBA community, while juggling various queries, a particular question squeeze my interest lime, the question was

How to find which database and type of bakup it is without restoring the bak file 

The answer to this is very simple, lets try to see with an example, I have a bakup file at “D:\SQL Server\myBakupFile.bak” lets see how we can get the detail of bakup type and database name without restoring it.

Step1: Verify whether the bakup file is valid for restore.

RESTORE VERIFYONLY FROM
disk = 'D:\SQL Server\myBakupFile.bak'
The backup set on file 1 is valid.

Step2: Get the header info of the bak file.

RESTORE HEADERONLY FROM 
disk = 'D:\SQL Server\myBakupFile.bak'

BackupName                  DatabaseName 
--------------------------  ---------------
TestDB-Full Database Backup TestDB       

(1 row(s) affected)

That’s all!!! :D pretty simple

Conclusion: Use HEADERONLY option of Restore to get the bak file information without actually restoring the bakup file to the server.

Pretty simple you are, anyone who came closer to you, fall in love with you, SQL Server :)