Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory-optimized table support for Sql Server #6405

Closed
AndriySvyryd opened this issue Aug 24, 2016 · 7 comments
Closed

Memory-optimized table support for Sql Server #6405

AndriySvyryd opened this issue Aug 24, 2016 · 7 comments
Labels
area-migrations area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Aug 24, 2016

Provide fluent API:

modelBuilder.Entity<Product>().ForSqlServerIsMemoryOptimized();

For on-premise SQL Server a file in a memory-optimized filegroup needs to be added to the database. We will dynamically create it in the migration if it doesn't exist.

The default durability is SCHEMA_AND_DATA we will not provide an API to change it to SCHEMA_ONLY. Likewise we won't have API to set MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT to false. Both can be accomplished by editing the migration.

A convention will set indexes to be nonclustered on memory-optimized tables. But the computed columns will will be up to the user to remove.

See https://msdn.microsoft.com/en-us/library/dn133165.aspx for more info on memory-optimized tables. And https://msdn.microsoft.com/en-us/library/dn246937.aspx for detailed description of limitations of memory-optimized tables.

@AndriySvyryd AndriySvyryd added this to the 1.1.0 milestone Aug 24, 2016
@AndriySvyryd AndriySvyryd self-assigned this Aug 24, 2016
@AndriySvyryd
Copy link
Member Author

AndriySvyryd commented Aug 25, 2016

@bricelam, @ajcvickers, @anpete, @divega This is what the SQL to 'seamlessly' create the necessary database file and filegroup would look like:

IF SERVERPROPERTY('IsXTPSupported') = 1 AND SERVERPROPERTY('EngineEdition') <> 5
    BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM [sys].[filegroups] [FG] JOIN [sys].[database_files] [F] ON [FG].[data_space_id] = [F].[data_space_id] WHERE [FG].[type] = N'FX' AND [F].[type] = 2)
        BEGIN
        DECLARE @db_name NVARCHAR(MAX) = DB_NAME();
        DECLARE @fg_name NVARCHAR(MAX);
        SELECT TOP(1) @fg_name = [name] FROM [sys].[filegroups] WHERE [type] = N'FX';

        IF @fg_name IS NULL
            BEGIN
            SET @fg_name = @db_name + N'_MODFG';
            EXEC(N'ALTER DATABASE CURRENT ADD FILEGROUP [' + @fg_name + '] CONTAINS MEMORY_OPTIMIZED_DATA;');
            END

        DECLARE @path NVARCHAR(MAX);
        SELECT TOP(1) @path = [physical_name] FROM [sys].[database_files] WHERE charindex('\', [physical_name]) > 0 ORDER BY [file_id];
        IF (@path IS NULL)
            SET @path = '\' + @db_name;

        DECLARE @filename NVARCHAR(MAX) = right(@path, charindex('\', reverse(@path)) - 1);
        SET @filename = REPLACE(left(@filename, len(@filename) - charindex('.', reverse(@filename))), '''', '''''') + N'_MOD';
        DECLARE @new_path NVARCHAR(MAX) = REPLACE(CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS NVARCHAR(MAX)), '''', '''''') + @filename;

        EXEC(N'
            ALTER DATABASE CURRENT
            ADD FILE (NAME=''' + @filename + ''', filename=''' + @new_path + ''')
            TO FILEGROUP [' + @fg_name + '];');
        END
    END

IF SERVERPROPERTY('IsXTPSupported') = 1
    EXEC(N'
        ALTER DATABASE CURRENT
        SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT ON;');

@divega
Copy link
Contributor

divega commented Aug 26, 2016

cc @jodebrui un case he can think of anything else we could do with the filegroup.

@ErikEJ
Copy link
Contributor

ErikEJ commented Aug 26, 2016

Should it not be:

IF SERVERPROPERTY('IsXTPSupported')  = 1

@AndriySvyryd
Copy link
Member Author

@ErikEJ we don't want to run this on SQL Azure, but it's a good suggestion, so I'll add it to the condition

@jodebrui
Copy link

Looks good. For reference, I created a similar script, that adds a filegroup if it's not there (in SQL Server) and configures recommended DB settings:
https://github.com/Microsoft/sql-server-samples/blob/master/samples/features/in-memory/t-sql-scripts/enable-in-memory-oltp.sql

What is your plan for recommended settings? I always recommend:

  • MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = ON
  • DB compat level at least 130 (for SQL2016 and Azure DB)

AndriySvyryd added a commit that referenced this issue Aug 30, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run on annotation set
Change conventions to not set cascade delete on foreign keys on on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add OldAnnotations to all alter operations
Add migrations annotations providers for IModel and ISequence
Diff migrations annotations for IModel, IEntityType and ISequence
Set migrations annotations on drop operations

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
AndriySvyryd added a commit that referenced this issue Aug 30, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run on annotation set
Change conventions to not set cascade delete on foreign keys on on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add OldAnnotations to all alter operations
Add migrations annotations providers for IModel and ISequence
Diff migrations annotations for IModel, IEntityType and ISequence
Set migrations annotations on drop operations

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
AndriySvyryd added a commit that referenced this issue Aug 30, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run on annotation set
Change conventions to not set cascade delete on foreign keys on on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add old values to all alter operations
Add migrations annotations providers for IModel and ISequence
Add migrations annotations providers for removing objects
Set migrations annotations on drop operations
Diff migrations annotations for IModel, IEntityType and ISequence

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
AndriySvyryd added a commit that referenced this issue Aug 31, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run on annotation set
Change conventions to not set cascade delete on foreign keys on on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add old values to all alter operations
Add migrations annotations providers for IModel and ISequence
Add migrations annotations providers for removing objects
Set migrations annotations on drop operations
Diff migrations annotations for IModel, IEntityType and ISequence

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
AndriySvyryd added a commit that referenced this issue Sep 1, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run on annotation set
Change conventions to not set cascade delete on foreign keys on on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add old values to all alter operations
Add migrations annotations providers for IModel and ISequence
Add migrations annotations providers for removing objects
Set migrations annotations on drop operations
Diff migrations annotations for IModel, IEntityType and ISequence

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
@AndriySvyryd
Copy link
Member Author

@jodebrui Added MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = ON. At the moment the are no plans to check the compat level, unless we get user feedback

@jodebrui
Copy link

jodebrui commented Sep 2, 2016

Sounds good. Thanks.
Yeah, with compat level you usually want to be careful, so may not want to change it automatically.

AndriySvyryd added a commit that referenced this issue Sep 2, 2016
Add ForSqlServerIsMemoryOptimized() EntityBuilder extension method
Add support for conventions to run when an annotation is set
Change conventions to not set cascade delete on foreign keys on memory-optimized tables
Add convention to configure keys and indexes as nonclustered on memory-optimized tables
Change IsClustered extension method on builders to take a bool?

Add AlterDatabaseOperation and AlterTableOperation
Add old values to all alter operations
Add migrations annotations providers for IModel and ISequence
Add migrations annotations providers for removed objects
Set migrations annotations on drop operations
Diff migrations annotations for IModel, IEntityType and ISequence

Add support for memory-optimized tables to RelationalDatabaseCleaner

Fixes #6405
Fixes #3122
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 2, 2016
@AndriySvyryd AndriySvyryd removed their assignment Sep 2, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations area-sqlserver closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

5 participants