I have an application which is generating PDFs & inserting it into SQL using file stream.
I am facing performance issue while inserting Filestream data, currently I am able to insert about 7-10 files only per second.
I am using server with configuration of core-2-duo, 4GB RAM, 250GB hard drive, x64 Windows Server 2012 R2 & SQL Server 2012.
Around 1 million file I need to save in DB daily, so I want to finish this as much as possible as fast. My file size are ranging from 7KB - 10MB.
I have also followed many best practices (http://blogs.msdn.com/b/blogdoezequiel/archive/2011/02/11/best-practices-on-filestream-implementations.aspx#.U6BQVpSSz1o) to improve the performance but still I am not able to achieve good performance. I have also tried SQL Server 2014 but it doesn't help.
What else I need to do in order to achieve the faster performance while inserting data?
I can't share more code other than this. Here is the code with which I am inserting file stream data into SQL:
In below logic, the arrStream is of type byte[] (array of byte).
DbCommand objDbCommand = null; try { SqlDatabase objSqlDatabase = new SqlDatabase("Database ConnectionString"); objDbCommand = objSqlDatabase.GetStoredProcCommand("Insert FileStream StoredProcedure Name"); objDbCommand.CommandTimeout = 60; objSqlDatabase.AddInParameter(objDbCommand, "@FileName", DbType.String, strFileName); objSqlDatabase.AddInParameter(objDbCommand, "@FileStream", DbType.Binary, arrStream); objSqlDatabase.AddInParameter(objDbCommand, "@StreamID", DbType.String, strFileStreamID); objSqlDatabase.AddParameter(objDbCommand, "@ReturnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, 0); objSqlDatabase.ExecuteNonQuery(objDbCommand); return (Int32)objSqlDatabase.GetParameterValue(objDbCommand, "ReturnValue"); } catch (Exception ex) { throw; } finally { if (objDbCommand.Connection != null) { objDbCommand.Connection.Close(); objDbCommand.Connection.Dispose(); } }
Below is how I am inserting data into SQL using stored procedure:
CREATE PROCEDURE [dbo].[InsertFileStreamDataToSQL] ( @StreamID UNIQUEIDENTIFIER, @FileName VARCHAR(200), @FileStream VARBINARY(MAX) ) AS BEGIN INSERT INTO [dbo].FilesVault(Stream_id, Name, file_stream) VALUES (@StreamID, @FileName, @FileStream) RETURN @@ERROR END GO
Thanks,
Rajesh Khunt
Blog : http://programmersgeek.wordpress.com/
Twitter : http://twitter.com/#!/r_rajeshkhunt
Linkedin : http://in.linkedin.com/in/rajeshkhunt