Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2014 Memory Optimized Table with Entity Framework 6.1 CodeFirst

My application uses two lookup tables I want to be Memory Optimized Tables.

There seems to be no Attribute yet to declare this;

Is there a possibility to hook into the table creation process and look for an custom attribute and modify the create table command?

I use Entity Framework 6.1 with Code First.

I seek a way to make it declarative, so if e.g. EF 6.2 or newer version supports it official, i can drop my hack.

like image 276
NickD Avatar asked Apr 12 '14 08:04

NickD


People also ask

What is memory optimized table in SQL Server?

What are Memory Optimized Tables? A Memory Optimized Table, starting in SQL Server 2014, is simply a table that has two copies, one in active memory and one durable on disk whether that includes data or just Schema Only, which I will explain later.

Which indexes can be scannable in parallel in memory optimized table?

Starting in SQL Server 2016, the query plan for a memory-optimized table can scan the table in parallel. This improves the performance of analytical queries. Hash indexes also became scannable in parallel in SQL Server 2016. Nonclustered indexes also became scannable in parallel in SQL Server 2016.

How do I create a memory table in SQL Server?

Create a memory-optimized data filegroup and add a container to the filegroup. Create memory-optimized tables and indexes. For more information, see CREATE TABLE (Transact-SQL). Load data into the memory-optimized table and update statistics after loading the data and before creating the compiled stored procedures.


2 Answers

I'd replace the CreateTable with the specific sql you want to optimise your database, called in the Up migration using Sql("create table etc..").

This is restricted to Sql Server 2014 and further restricted by other requirements for memory optimisation. Not worth the candle trying to do it with attributes within EF.

like image 163
Colin Avatar answered Oct 05 '22 05:10

Colin


Maybe you can create an Entity Framework DB initializer that drop and recreate the table?

Something like (pseudo code!):

    public class MyDbInitializer : CreateDatabaseIfNotExists<MyDbContext>
    {
        protected override void Seed(MyDbContext context)
        {
            context.Database.ExecuteSqlCommand("DROP TABLE MyTable");
            context.Database.ExecuteSqlCommand("CREATE TABLE MyTable (bla bla bla)  WITH (MEMORY_OPTIMIZED=ON)");
        }
    }

I can't test this feature because I don't have SQL 2014 but I have used the Seed method to alter my database in many situations so it should work.

You can then assign your database initializer using:

System.Data.Entity.Database.SetInitializer(new MyDbInitializer());
like image 25
Davide Icardi Avatar answered Oct 05 '22 05:10

Davide Icardi