I am having some problems with storing Images in our database. We currently have about 400k records but I expect this to increase into the millions quite quickly. At the moment I am already experiencing performance issues so this is a major concern. I did a bit of research before deciding to use SQL Server to store the images and everything i read indicated that it would be capable of doing this. 

I designed the table to be very simple containing 3 columns...

* Id (Primary Key, unique identifier, not null)
* ImageHash (unique identifier, not null)
* BinaryImage (varbinary(max), not null)

The logic was that I generate the ImageHash in my application code. The image hash is used for lookups before an insert to see if the binary image already exists in the database. The rest of the time I am simply querying the table using the Id directly.

I am using .NET Entity Framework to perform my Data Access. The Id column is being generated on insert, not sure if that is best practice or not. 

Here is the Create Script of my Table. I created an index for the ImageHash, however I don't really have a great understanding of SQL Server Indexes.

```
CREATE TABLE [dbo].[ImageContent](
    [Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [ImageHash] [uniqueidentifier] NOT NULL,
    [BinaryImage] [varbinary](max) NOT NULL,
 CONSTRAINT [PK_ImageData] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

ALTER TABLE [dbo].[ImageContent] ADD  CONSTRAINT [DF_ImageData_Id]  DEFAULT (newid()) FOR [Id]

```

And the Index....

```
CREATE NONCLUSTERED INDEX [ImageHash_Index] ON [dbo].[ImageContent]
(
    [ImageHash] ASC
)
INCLUDE (   [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

```

I have rebuilt all the indexes but that has not resolved the issue. I have been playing around with SQL Server Profiler and I identified SQL Insert that was causing the problem (Generated from Entity Framework). Here is the statement with the binary but I have truncated the majority of it with ... This was timing out after 30 seconds...

```
exec sp_executesql N'declare @generated_keys table([Id] uniqueidentifier)
insert [dbo].[ImageContent]([ImageHash], [BinaryImage])
output inserted.[Id] into @generated_keys
values (@0, @1)
select t.[Id]
from @generated_keys as g join [dbo].[ImageContent] as t on g.[Id] = t.[Id]
where @@ROWCOUNT > 0',N'@0 uniqueidentifier,@1 varbinary(max) ',@0='DF76D1FF-5C05-58E0-0933-1ADBCC6345A8',@1=0xFFD8FFE1214545786966000049492A00080000000D00000103...

```

So my question are... 

* Can anyone see some major issues with the way I have set this up?
* Are there any recommendations that you can give me to help me improve the performance?
* Is SQL Server capable of storing millions of images in this way?

Thanks in advance for your time!