Translate

Tuesday, August 7, 2012

File Table

A SQL Server FileTable is a special table that allows storing directory and files into the SQL Server. With this option, you can access these files and directories from windows applications as if they were stored in the file system.

Files stored in the FileTable are exposed to windows through a windows share.

If you create or change a file through the windows share the command is captured by a SQL Server component and the changes are applied to the corresponding data in the FileTable.

Since FileTable feature builds on top of SQL Server FILESTREAM technology, first you need to enable your SQL Server instance for filestream.

To enable FILESTREAM feature on SQL Server 2008:

Open SQL Server Configuration Manager (Start > Programs > Microsoft SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager)

Navigate to the SQL Server Services node and select the SQL Server instance you want to modify SQL Server (MSSQLSERVER)

Click the FILESTREAM tab and select the checkboxes to enable FILESTREAM and enter a share name for the files as shown below image.

clip_image001

Next is to enable the filestream access level which can be done from either using a T-SQL or from the interface.

EXEC sys.sp_configure N'filestream access level', N'2'

GO

RECONFIGURE WITH OVERRIDE

GO

If you prefer to use user interface you can enable file stream by Right Click Server name from the management studio and select Properties and then select Advanced option and finally you can select the filestream option from the given option.

clip_image003

Let us create a database with filestream set to on.

USE MASTER

GO

IF DATABASEPROPERTYEX('FileTableDatabase','Version') > 0

DROP DATABASE FileTableDatabase

CREATE DATABASE FileTableDatabase

ON PRIMARY

(

NAME = N'FileTableDatabase_Data',

FILENAME = N'D:\FileTable\FileTableDatabase.mdf'

),

FILEGROUP FilestreamFG CONTAINS FILESTREAM

(

NAME = N'FileTableDatabase_FileStream',

FILENAME= 'D:\FileTable\Data'

)

LOG ON

(

NAME = N'FileTableDatabase_Log',

FILENAME = N'D:\FileTable\FileTableDatabase_log.ldf'

)

WITH FILESTREAM

(

NON_TRANSACTED_ACCESS = FULL,

DIRECTORY_NAME = N'FileTable'

)

Next step is to create a File Table which will be the container for the files and directories.

USE FileTableDatabase

GO

CREATE TABLE FillTableDocument AS FileTable

WITH

(

FileTable_Directory = 'FileTableDocument'

)

You do not have a user interface option to create FileTables hence you need to write T-SQL as shown above.

After creating the FillTable, if you navigate to SQL Server Management Studio and expand FillTables node under the Tables node, you will see the created FileTable. If you expand Columns, you will see list columns.

clip_image004

In the File Table created, you can right click and select Explore FileTables Directory as shown in the below image.

clip_image005

This will take you to the File Directory where you can create your files. Now you have the option of placing files and creating directories in this folder which will automatically see from the File Table.

As you can see from the below image three different types of files are copied to the FileTable Directory and there is another sub folder is created along with few files. These files were copied from the operating system not from the SQL Server.

clip_image007

If you query the File table, you will see manually copied files are updated to the SQL Server.

clip_image009

If you want to rename files, you can do it from the OS level as well as from the T-SQL query as shown in the below script. T-SQL script will update table and accordingly file name will be changed and vice-versa. .

UPDATE

[FileTableDatabase].[dbo].[FillTableDocument]

SET name = 'FileTable4.txt'

WHERE [stream_id] = '2EB5608D-F270-E111-80DB-101F74EE0B01'

Since path_locator has data type of heirachyid you can use all the CLR functions.

Following will give you the operating system root path for the given File Table.

SELECT FileTableRootPath('dbo.FillTableDocument') as RootPath

For each file you can get the full file path with the following query.

SELECT name,file_stream.GetFileNamespacePath() full_path ,is_directory

FROM dbo.FillTableDocument c

Which has the following output.

clip_image010

Another important point to note here is, all these files will not be saved in SQL Server database. This means that though you have large files in the operating system, your table sizes or database sizes will not be large in size. When you are querying the table, if you are selecting the file_stream column, for large files it will take little while to run the query since it reads the file stream by reading the entire file.

You can utilize File Table for handle image libraries, video libraries and CV management system.

Triggering a New File

Most of the time, users are required to track new file comes to the folder, For example, you might need to execute a SSIS package once new file is received. If you have File Table option enabled, you can simply create an INSERT trigger to the File table and execute SSIS within a trigger.

Monday, July 30, 2012

Database Snapshots in Index Rebuilding

 

Yesterday I posted a blog about database snapshot and this is somewhat an extension to it. Yesterday we checked the behavior of a database snapshot with database backups. Now let us check it’s relation with Indexes.

Let us create a database and relevant table along with indexes.

image

Then I manually insert data so that my indexes are fragmented which is shown below.

image

Then I created snapshot and as yesterday verified the file sizes.

image

Then did a index rebuild and verified the file sizes again.

image

So the sizes are increased which means index rebuild does effect the database snapshots.

Saturday, July 28, 2012

Database Snapshot, is it only for Data Modifications?

Database Snapshot consists of a read-only static view of the database without including the uncommitted transactions.

If I put all of the above stuff in one picture, it will be like this.

image

In simple terms, when we create a database snapshot, empty spare file will be created As and when data is changed in the database, data pages are moved to the sparse file. So if you are reading from the snapshot, modified data pages will be read from the sparse file while non-modified file pages are read from the source database.

Now the question is, Is it only the data modifications are sent to the sparse file.

Let us examine this.
 
Let me create a database and some sample data for it.
 
image

Then I will take a full back and update few data as shown below.

image

Now then, back to the business end.

Let us create a database snapshot.

image

After creating this let us see the size of the snap shot file size.

image

So the file size of the snapshot file is 3.06 MB but size on the disk is 128KB which is actual data size.

Now let me take another backup.

image

Let us see the file size again.

image

What! how come, I didn’t do any data changes, still the size on disk has increased more than twice without any data modification.

Explanation

When there are data changes after a full backup, it will set a bit value in page so that for differential backup it can identify which pages need to taken into the differential backup. This value will be reset with a full backup. Since you have done a full backup after the database snapshot creation, full backup will modify the data page. So database snapshot is not only for the data modification but anything changes to the data page will move the page to the database snapshot.

Friday, July 27, 2012

Anything-SQL Lightning Talks - 2nd Edition

The SS SLUG is organizing the 2nd Edition of Anything-SQL Lightning Talks for the August 2012 meet-up. This is where a group of presenters will speak for 5 minutes on any topic related to SQL Server. We are signing up people for this session and would like you to talk as part of it. Interested? Want more information? Just click here...

SS SLUG August 2012 Meet-up

We are planning for the August 2012 SS SLUG meet-up to be held at Pearson Lanka, in Orion City - Dematagoda, happening on August 15th. We shall provide more information on that soon.

SQL Server 2008 R2 SP2 Released

Customers are highly encouraged to stay on a supported service pack to ensure they are on the latest and most secure version of SQL Server 2008 R2.  To obtain SQL Server 2008 R2 SP2 with its improved security and supportability please visit the links below:

·        Download SQL Server 2008 R2 SP2

·        Download SQL Server 2008 R2 SP2 Express

·        Download SQL Server 2008 R2 SP2 Feature Pack

Thursday, July 26, 2012

Oracle Unveils Migration Tool for Microsoft SQL Server to MySQL

Oracle is going after users of Microsoft's SQL Server with a new tool for migrating data from SQL Server to its own MySQL database, the vendor announced Wednesday.

The tool has been built into Oracle's MySQL Workbench administration console, and with it, applications written for SQL Server can be easily tweaked for MySQL, Oracle said in a statement.

Oracle is also releasing an improved version of the MySQL Installer for Windows environments, as well as a new MySQL Notifier for Windows tool that "helps developers and DBAs to easily monitor, start and stop their MySQL database instances, with the Microsoft SQL Server look and feel," Oracle said.

In addition, Oracle is offering a new plug-in that allows users with no experience on MySQL to work with MySQL data inside Microsoft Excel.

Oracle claims that MySQL users can experience up to 90% less total cost of ownership compared to running SQL Server 2012.

More at:

http://www.oracle.com/us/corporate/press/1715479

http://www.computerworld.com/s/article/9229646/Oracle_hopes_to_poach_Microsoft_SQL_Server_users_with_MySQL_migration_tool?taxonomyId=18

Monday, July 23, 2012

Find Orphaned Users In SQL Server

Orphan users can occur once you detach databases or restore a database from another SQL Server database instance. Read this FAQ how to manage those instances.

Database Tuning Advisor (DTA) Naming Conventions

DBAs are using Database Tuning Advisor a.k.a. DTA get recommendations for better performance of their databases.

Following is the index script created from the DTA.

image

in this index name is _dta_index_Contact_13_341576255__K1_4_6 and people might think that this does not have any standards, but it does have a standard.

You have four parts in this index naming.

image

A

This is straight forward. It is the object name or the table name.

B

13 is the database id for the Adventureworks.

image

C

Object Id of the contact.

image

D

Columns order of the key columns.

image

Saturday, July 21, 2012

Restoring to Existing Database

If you want to restore to the existing databases, first you need to make sure that database is not accessible by any other users. So you need to change the database into single user mode before restoring.

However, in SQL Server 2012 you have this option inbuilt with the restoring option.

image

Thursday, July 19, 2012

Cache Plans for Estimated Queries

Let me put this in simple. Yes when you click estimated query plan as shown in the below image it will cache the query plan.

clip_image001

Let us verify this.

Let us clear the cache first and write the query first and then if you click the Display Estimated Execution Plan button

clip_image002

Then if you examine the cache plan with following query.

clip_image003

you can see that query is cached. This is fixed in SQL Server 2012.