2

I am using SQL Server 2008 and C# for a CLR Stored Procedure. Is there any way to keep in memory a big object and to retrieve this object after a second call of the Stored Procedure?

In the second call, I need to retrieve the data of this big object as quickly as possible. This object is like a table.

If there is no way, I was thinking about the serialization. Or I was thinking to implement something by myself to speed-up the process. What do you think?

Thanks!

1
  • What about serializeing it to a blob in a temp in temp db. Commented Jul 15, 2011 at 14:26

2 Answers 2

1

The easiest way would be to serialize it to a file somewhere. I wouldn't hold it in memory because you are not given any hooks as to when to clear this memory.

There are a few posts about file access here:

http://social.msdn.microsoft.com/Forums/en-US/sqlnetfx/thread/45b46ce0-a4b3-45d0-86ec-1c1638fb7cb2

If this is a table-like structure, then another easy and probably fast-enough way is to create a temporary table that the CLR can select out of using a data reader. Personally I would go this route and prove it isn't fast enough before taking other routes.

Sign up to request clarification or add additional context in comments.

2 Comments

I think reading from a serialized file could be faster than to read the data from a SQL table, don't you?
Not necessarily, file access is guaranteed to hit disc, which may or may not be fast access. Reading from a table in the database could be faster if SQL is able to cache it.
1

The only way to store an object in memory within sql server is to place it in a static field. This can work, however doing so requires writing some relatively tricky code to ensure it is thread-safe and the lifetime is managed correctly. Under sql server, the clr appdomain may be unloaded and regenerated at essentially any point in time, so one must be cautious to account for this. There is also the possibility of multiple appdomains for the same assembly living concurrently but processing different query requests during ddl operations.

Adam Machanic has written an article about some of these implications of using static fields as a cache and the security implications and best-practices associated with doing so.

3 Comments

Thank you for this point. I need to put my hands on a little bit!
In the SQL CLR, you can only use static fields if the assembly is registered without using the safe option.
This isn't strictly true - one is allowed to use static fields in safe assemblies if they are marked either readonly or decorated with a CompilerGenerated attribute. The static check is more of a guideline for "do you know what you're doing" than a hard requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.