Filetable is one of the best features shipped with SQL Server 2012. It makes our life easier when you need to manage your files and folders via SQL Server when it actually resides in the file system of windows. However, you can easily access them using windows application and SQL Server as well.
Pre-requisites :
- SQL Server 2012 – FileTables – Prerequisites- Enable / Create / Alter
- SQL Server 2012 – FileTables – Data Definition Language (DDL) Statements
- SQL Server 2012 – FileTables – Data Manipulation Language (DML) Statements
Problem :
Sometimes, you execute a select statement against a filetable using SQL Server Management Studio (SSMS) and it becomes inaccessible and even after spending so much time, the query does not return the result set. However, you can still access it via windows file system. In normal scenario, filetable returns result set in less than few seconds (depending upon the size of filetable). Now what to do ?
Solution :
First of all, I cancelled the query that kept on running for hours. It seemed to me that this filetable had been locked because of some transaction. Just to test this scenario, I executed the same query with no lock table hint and it returned the result set as shown below.
USE SampleDB GO SELECT * FROM [dbo].[CVBank] WITH (NOLOCK) GO --OUTPUT
Wow, so the file table was indeed locked. So half of the problem is solved. Now, in order to fix this issue, I need to identify which file(s) is/are being used and due to which, the filetable has been apparently locked.
Given below is the script that will give you the file(s) name being used and due to which, the filetable has been locked. I found this script on MSDN.
USE SampleDB GO SELECT opened_file_name FROM sys.dm_filestream_non_transacted_handles WHERE fcb_id IN ( SELECT request_owner_id FROM sys.dm_tran_locks ); GO --OUTPUT
As mentioned above, I identified the lock held by the filetables and in order to avoid this issue, you can take necessary action to close this file.
Let me know if you come across this situation in real world.