efcore icon indicating copy to clipboard operation
efcore copied to clipboard

SQL Server Database FileGroup Support

Open mattAWL opened this issue 8 years ago • 9 comments

When creating code first migrations, there is no possibility to change the database filegroup of an object (table, index,...). Having all data and indicies in the PRIMARY filegroup of an SQL Server database could be a performance bottleneck in some scenarios. It would be great to:

  1. define such filegroups with entity framework code first
  2. use these filegroups while creating new code types/entities

For example:

[FileGroup("DATA")]
public class SomeEntity
 {
        [Required]
        [Key]
        public int Id { get; set; }

        [Required]
        [Index("IX_UserId")]
        [FileGroup( "IDX")]
        public int UserId { get; set }
}

-> Creates user table "SomeEntity" on filegroup "DATA" and non-clustered index "IX_UserId" on filegroup "IDX"

mattAWL avatar Jun 07 '18 07:06 mattAWL

Any news about a milestone for this?

pietrodicaprio avatar Jan 12 '19 13:01 pietrodicaprio

@pietrodicaprio This issue is in the Backlog milestone. This means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.

ajcvickers avatar Jan 12 '19 23:01 ajcvickers

Entity Framework 7 (the upcoming complete re-write) may address additional infrastructural aspects of the SQL Server database footprint (it is a huge API), such as FILEGROUP and FILESIZE constructs and other DBA essentials. I believe the goal of EF is to be ORM, and perhaps infrastructure considerations are not part of the REPOSITORY pattern. The beauty about scripted DDL is that it is very easy to create and parameterize. Entity Framework 6.x is very interesting, but I am not sure how much DBA work can be added in such a way that it becomes useful to ORM developers trying to "avoid" TSQL.

Notably, for me to learn TSQL DDL was very hard. Now, I am struggling to learn ASP.NET Core and EF Core, but I am finally starting to appreciate it after about 17-18 weeks, and I am also learning the vagaries of HTTP Web applications. It has been an extremely difficult learning curve, but seems to be reaching a crescendo thanks to the great help from guys like ajcvickers et al and Dino Esposito.

By the way, acjvickers, was your relative in the Government of Canada in the House of Commons as a Sergeant at Arms?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Resort UX Inc.
-- Purpose: Creates the Resort UX Database
-- SQL Server 2017 Version
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
USE [master]
GO

CREATE DATABASE [ResortUX]
CONTAINMENT = NONE
ON PRIMARY -- The Primary Filegroup should never have user data on it.
( NAME = N'ResortUX_SYSDATA_0', FILENAME = N'F:\OLTP_DATA\ResortUX\System\ResortUX_SYSDATA_0.MDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_SYSDATA_1', FILENAME = N'F:\OLTP_DATA\ResortUX\System\ResortUX_SYSDATA_1.MDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Corporate] 
( NAME = N'ResortUX_CORPORATE_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Corporate\ResortUX_CORPORATE_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_CORPORATE_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Corporate\ResortUX_CORPORATE_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_CORPORATE_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Corporate\ResortUX_CORPORATE_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_CORPORATE_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Corporate\ResortUX_CORPORATE_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Guest] 
( NAME = N'ResortUX_GUEST_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Guest\ResortUX_Guest_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_GUEST_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Guest\ResortUX_Guest_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_GUEST_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Guest\ResortUX_Guest_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_GUEST_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Guest\ResortUX_Guest_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Insurance] 
( NAME = N'ResortUX_INSURANCE_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Insurance\ResortUX_INSURANCE_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_INSURANCE_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Insurance\ResortUX_INSURANCE_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Logistic]
( NAME = N'ResortUX_LOGISTIC_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Logistic\ResortUX_LOGISTIC_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_LOGISTIC_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Logistic\ResortUX_LOGISTIC_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB), 
( NAME = N'ResortUX_LOGISTIC_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Logistic\ResortUX_LOGISTIC_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB), 
( NAME = N'ResortUX_LOGISTIC_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Logistic\ResortUX_LOGISTIC_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB), 

FILEGROUP [Metadata]
( NAME = N'ResortUX_METADATA_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Metadata\ResortUX_METADATA_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_METADATA_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Metadata\ResortUX_METADATA_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB), 

FILEGROUP [Reserve] -- anything that can be reserved and / or paid for.
( NAME = N'ResortUX_RESERVE_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Reserve\ResortUX_RESERVE_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_RESERVE_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Reserve\ResortUX_RESERVE_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_RESERVE_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Reserve\ResortUX_RESERVE_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_RESERVE_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Reserve\ResortUX_RESERVE_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Security] 
( NAME = N'ResortUX_SECURITY_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Security\ResortUX_SECURITY_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_SECURITY_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Security\ResortUX_SECURITY_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Service] DEFAULT
( NAME = N'ResortUX_SERVICE_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Service\ResortUX_SERVICE_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_SERVICE_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Service\ResortUX_SERVICE_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_SERVICE_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Service\ResortUX_SERVICE_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_SERVICE_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Service\ResortUX_SERVICE_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Staff] 
( NAME = N'ResortUX_STAFF_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Staff\ResortUX_STAFF_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_STAFF_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Staff\ResortUX_STAFF_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),

FILEGROUP [Stock] 
( NAME = N'ResortUX_STOCK_0', FILENAME = N'F:\OLTP_DATA\ResortUX\Stock\ResortUX_STOCK_0.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_STOCK_1', FILENAME = N'F:\OLTP_DATA\ResortUX\Stock\ResortUX_STOCK_1.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_STOCK_2', FILENAME = N'F:\OLTP_DATA\ResortUX\Stock\ResortUX_STOCK_2.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB),
( NAME = N'ResortUX_STOCK_3', FILENAME = N'F:\OLTP_DATA\ResortUX\Stock\ResortUX_STOCK_3.NDF' , SIZE = 25MB , MAXSIZE = UNLIMITED, FILEGROWTH = 10MB)

LOG ON -- Log files only fill 1 at a time. The second log file is just exposed here as a precautionary measure.
( NAME = N'ResortUX_TLOG_0', FILENAME = N'F:\OLTP_LOG\ResortUX\TransactionLogs\ResortUX_TLOG_0.LDF' , SIZE = 102400KB , MAXSIZE = 204800KB , FILEGROWTH = 20480KB ),
( NAME = N'ResortUX_TLOG_1', FILENAME = N'F:\OLTP_LOG\ResortUX\TransactionLogs\ResortUX_TLOG_1.LDF' , SIZE = 102400KB , MAXSIZE = 204800KB , FILEGROWTH = 20480KB )
GO

USE [ResortUX]
GO
CREATE SCHEMA [Corporate] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Metadata] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Guest] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Insurance] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [TSQL] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Logistic] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Reserve] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Security] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Service] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Staff] AUTHORIZATION [dbo];
GO
CREATE SCHEMA [Stock] AUTHORIZATION [dbo];
GO

ALTER DATABASE [ResortUX] COLLATE SQL_Latin1_General_CP1_CS_AS;
ALTER DATABASE [ResortUX] SET RECOVERY SIMPLE
ALTER DATABASE [ResortUX] SET COMPATIBILITY_LEVEL = 110
GO

ALTER DATABASE [ResortUX] SET ANSI_NULL_DEFAULT ON
ALTER DATABASE [ResortUX] SET ANSI_NULLS ON 
ALTER DATABASE [ResortUX] SET ANSI_PADDING ON
ALTER DATABASE [ResortUX] SET ANSI_WARNINGS ON
ALTER DATABASE [ResortUX] SET QUOTED_IDENTIFIER OFF
GO

ALTER DATABASE [ResortUX] SET ARITHABORT ON
ALTER DATABASE [ResortUX] SET NUMERIC_ROUNDABORT ON 
GO

ALTER DATABASE [ResortUX] SET AUTO_CLOSE OFF 
ALTER DATABASE [ResortUX] SET AUTO_SHRINK OFF 
ALTER DATABASE [ResortUX] SET AUTO_UPDATE_STATISTICS ON 

GO
ALTER DATABASE [ResortUX] SET CURSOR_CLOSE_ON_COMMIT OFF 
ALTER DATABASE [ResortUX] SET CURSOR_DEFAULT GLOBAL 
ALTER DATABASE [ResortUX] SET CONCAT_NULL_YIELDS_NULL ON 
GO

ALTER DATABASE [ResortUX] SET RECURSIVE_TRIGGERS OFF 
ALTER DATABASE [ResortUX] SET DISABLE_BROKER 
ALTER DATABASE [ResortUX] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
ALTER DATABASE [ResortUX] SET DATE_CORRELATION_OPTIMIZATION OFF 
ALTER DATABASE [ResortUX] SET TRUSTWORTHY OFF 
ALTER DATABASE [ResortUX] SET ALLOW_SNAPSHOT_ISOLATION OFF 
ALTER DATABASE [ResortUX] SET PARAMETERIZATION SIMPLE 
ALTER DATABASE [ResortUX] SET READ_COMMITTED_SNAPSHOT OFF 
ALTER DATABASE [ResortUX] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [ResortUX] SET PAGE_VERIFY CHECKSUM  
ALTER DATABASE [ResortUX] SET DB_CHAINING OFF 
ALTER DATABASE [ResortUX] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
ALTER DATABASE [ResortUX] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO

ALTER DATABASE [ResortUX] SET READ_WRITE 
ALTER DATABASE [ResortUX] SET MULTI_USER 
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
BEGIN
	EXECUTE [ResortUX].[dbo].[sp_fulltext_database] @action = 'enable'
END
GO

--Verify the collation setting.  
SELECT name, collation_name, *
FROM sys.databases  
WHERE name = N'ResortUX';  

CubeSpark avatar Feb 10 '19 22:02 CubeSpark

please give me an example for set filegroup in fluent api of EF-Core mapper.

hamidrh avatar Dec 17 '19 20:12 hamidrh

Hamid, the only way I can give a CREATE FILEGROUP to you is in TSQL.

ADD FILEGROUP is a different instruction.

Will that suffice?

Bob Letts Calayugan Music Manhttps://www.youtube.com/channel/UC05ImhqwEzN1E6C51FcdfxA (416)724-9252 {landline} [email protected]

CubeSpark avatar Dec 17 '19 21:12 CubeSpark

Hamid,

Latter-version Microsoft SQL Server databases require the following minimums, and note that TSQL DDL (data definition language) can also SET the DEFAULT FILEGROUP for a database.

I am fairly certain that a database must have a default file group named PRIMARY. Also, the following is true:

  1. There must be at least 1 Transaction Data File to store data. This file must be stored in a FILEGROUP.

  2. There must be at least 1 Transaction LOG File to record all relevant events. This file is NOT stored in a FILEGROUP.

  3. There must be at least 1 [PRIMARY] FILEGROUP with at least 1 Data File

  4. There can be many FILEGROUPS, each with many files, on different storage types.

  5. The Database Engine handles threading differently and more effectively when there are multiple FILEGROUPS, as long as there are multiple FILES in each.

  6. The "Law of Diminishing Returns" also applies. The idea is to balance the conceptual thinking behind data storage with the physical constraints of the servers or the virtualization of that.

  7. I do not believe that Azure supports FILEGROUPS, but I could be wrong.

Bob Letts Calayugan Music Manhttps://www.youtube.com/channel/UC05ImhqwEzN1E6C51FcdfxA (416)724-9252 {landline} [email protected]

CubeSpark avatar Dec 27 '19 19:12 CubeSpark

We need this. We have complex and advance database architecture and we need to specify filegroups for indexes and tables.

aspnerd avatar Jul 21 '20 19:07 aspnerd

Any update?

@ajcvickers

safwanzho avatar Sep 18 '23 04:09 safwanzho

This issue is in the Backlog milestone. This means that it is not planned for the next release (EF Core 8.0). We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.

ajcvickers avatar Sep 18 '23 09:09 ajcvickers