I have done a number of courses for Pluralsight. Last year when SQL Server 2014 was released, I planned to do a session on the same. The course SQL Server 2014 Administration New Features was a lot of hardwork but it did give me a chance to learn something new and share. Do let me know if you have every got a chance to listen to this course. Would love to hear your feedback.
As part of the course, one of the module included the basics of InMemory capabilities of SQL Server 2014. One of the listener from the course pinged me on Twitter with some interesting conversation. This inspired me to write this blog post.
Follower: Thanks for the InMemory basics, I had a simple doubt.
Pinal: Yes, please tell me. Let me see if I can answer the same.
Follower: I plan to upgrade my server to SQL 2014.
Pinal: Glad the course is helping you. Is there any doubt?
Follower: Yes, I might be planning to use the InMemory capabilities for couple of databases as part of application upgrade.
Follower: I want to know can I restrict the amount of memory a particular InMemory OLTP Database can take.
Pinal: Can you please elaborate a bit please?
Follower: Simple Pinal, I want one DB not to take more than 20% of Memory on my server and the other InMemory DB not to take more than another 40% Memory available on the server.
Follower: As part of upgrade I am consolidating and hence these restrictions can be of great help.
Pinal: Now I get it, it is possible with Resource Governor. Havent you tried it?
Follower: I think these are great pointers, I will dig it up. Thanks again.
Pinal: You are welcome. I will write it as a blog for sure in future.
This conversation has been in my minds for a while. It has taken some time to finally get this blog. The script one needs to write is simple. Let me walk you through the same.
- Create the Resource Pool
- Create the InMemory OLTP Filegroup enabled DB
- Bind the Resource Pool to our database
- Check the DB metadata
- Make the DB Offline and Online to make the changes take effect
- Clean up
As the script says, let us first create our Resource Group.
-- Step 1: Create the Resource Pool. Limit to 40% memory.
CREATE RESOURCE POOL InMemory_Pool WITH (MAX_MEMORY_PERCENT = 40);
ALTER RESOURCE GOVERNOR RECONFIGURE;
Next is to create the InMemory DB. This is same as used in the previous blog – Beginning In-Memory OLTP with Sample Example.
-- Step 2: Create the InMemory DB
CREATE DATABASE InMemory
ON PRIMARY(NAME = InMemoryData,
FILENAME = 'd:\data\InMemoryData.mdf', size=100MB),
-- Memory Optimized Data
FILEGROUP [InMem_FG] CONTAINS MEMORY_OPTIMIZED_DATA(
NAME = [InMemory_InMem_dir],
FILENAME = 'd:\data\InMemory_InMem_dir')
LOG ON (name = [InMem_demo_log], Filename='d:\data\InMemory.ldf', size=50MB)
The next step is where the magic begins. We need to bind the DB and the resource pool. This can be achieved using the next command:
-- Step 3: Bind the resource pool to DB
EXEC sp_xtp_bind_db_resource_pool 'InMemory', 'InMemory_Pool'
The success for this step can be viewed with this message:
A binding has been created. Take database ‘InMemory’ offline and then bring it back online to begin using resource pool ‘InMemory_Pool’
The next logical step is to check the metadata if the same has been mapped. Use the sys.databases DMV for this.
-- Step 4: Check the Database metadata
SELECT dbs.database_id, dbs.name, dbs.resource_pool_id
FROM sys.databases dbs
WHERE name LIKE 'InMemory'
Just like how resource governor configuration needs to be reconfigured. We need to do something similar for databases to make this changes take effect. We will need to take the DB offline and bring it online.
-- Step 5: For RG to take effect, make DB Offline and Online
ALTER DATABASE InMemory SET OFFLINE
ALTER DATABASE InMemory SET ONLINE
That is it. We are good now. Our InMemory DB will not take more than 40% of the memory allocated to SQL Server. Though this was a simple concept, I thought was worth a share. If you would like to clean up this experiment, please use the below script.
-- Clean up
DROP DATABASE InMemory
DROP RESOURCE POOL InMemory_Pool
ALTER RESOURCE GOVERNOR RECONFIGURE;
To learn such interesting SQL Server 2014 enhancements, feel free to listen to my Pluralsight course for more such topics.
Reference: Pinal Dave (https://blog.sqlauthority.com)