question

Mark avatar image
Mark asked

Is there a way to force a table into memory?

If I have a table, temporary or otherwise, which I want to place in SQL Server memory, is there a way to *force* SQL to do that? If the answer is "SQL handles the memory and will keep it in memory if possible, so don't worry with it" then is there a practical way to verify that the table is indeed in memory? Will it stay there if it is in memory or disappear as soon as there is another server request? Also, if there is an associated index with the table, will the index automatically be in memory also?
memory
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image
Grant Fritchey answered
There used to be a way to pin a table into memory, but it caused all kinds of problems, so Microsoft removed it from the product. Now, SQL Server manages that memory, and as you say, it will keep it there if possible. It will stay in memory as long as it keeps getting accessed and no other query comes along that needs to dump it out of memory in order to free up space. No, the index won't automatically be in memory. Only if it's accessed and read into the cache. I'm not aware of a way to monitor this except to watch for physical i/o versus logical i/o in the queries.
8 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
+1 - I am running out of votes today! (5 left). @Mark - if you're interested, the old way was DBCC PINTABLE ( http://msdn.microsoft.com/en-us/library/ms178015.aspx)
5 Likes 5 ·
sp_lock avatar image sp_lock commented ·
+1 to you both. Interesting note from the article "This functionality was introduced for performance in SQL Server version 6.5. DBCC PINTABLE has highly unwanted side-effects. These include the potential to damage the buffer pool. DBCC PINTABLE is not required and has been removed to prevent additional problems. The syntax for this command still works but does not affect the server."
2 Likes 2 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Ha, ha, ha... So you can run it and it doesn't return an error, but it won't do anything. That's great. I'm surprised we're not seeing all kinds of problems from people "I pinned my table but performance still sucks, help, urgent, urgent."
1 Like 1 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
Proof positive you're not oooollld. I remember using this on SQL Server 6.5 to improve performance... and crash the server occasionally. But then, it was easy to crash the server on 6.5.
1 Like 1 ·
Mark avatar image Mark commented ·
Yes, that's interesting - thanks Matt (+1). PinTable... as on the donkey or something.
0 Likes 0 ·
Show more comments
Blackhawk-17 avatar image
Blackhawk-17 answered
Sure - write a bad query that ensures table and index scans and run it every five minutes :) You may "push" valid data out of the buffer but you will have effectively pinned the table into RAM. (Totally Tongue-In-Cheek!)
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
You could always clean out the buffers before you run it, just to be really sure.
2 Likes 2 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
OOOHH - good one!
0 Likes 0 ·
Mark avatar image Mark commented ·
Ha! Thanks Blackhawk, that would totally defeat the purpose, but it would work wouldn't it? 8-) Actually, even if I could keep a table in memory, it would be used only in very limited cases. But I won't bore you with the details of where it would be tempting to try it.
0 Likes 0 ·
sql_handle avatar image
sql_handle answered
In this blog post, Paul Randal addresses how to check the wasted buffer space of currently cache database contents. He uses sys.dm_os_buffer_descriptors to do that. That strategy can also be used to check how many blocks of a given table or index is in the buffer cache at the moment. http://www.sqlskills.com/blogs/paul/performance-issues-from-wasted-buffer-pool-memory/
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.