具有大量查找列的表的最佳实践

时间:2013-02-07 17:37:39

标签: sql rdbms

确定这么一点背景......作为程序员 - 首先是有限的数据库设计经验,我犯了几个新的错误,即统一的代码查找表和另一个表上的EAV实现。我目前正在尝试将这些工作重新编写为更传统的RDBMS,但我想绝对确定我这次正确地做到了。

我的大问题表是一个包含150个左右列的大型表,其中包含我们必须向州报告的客户数据。大多数此类数据使用特定于州的查找值,因此根据我当前的设计方案,我最终会将75-100 FK用于不同的查找表。

现在我们需要对这些数据进行报告,因此我需要能够轻松地引用代码值和数据。每个属性的描述。我能想到的唯一选择是创建两个单独的视图(或者可能是一个大视图),这些视图将为我提供数据。这似乎是一个相当繁琐的创建和维护过程,特别是考虑到所收集的数据可能会发生变化。如果这是标准做法,我可以做腿部工作,但我很好奇有一个更好的方法,我只是不知道。

USE [TestCompany]
GO

/****** Object:  Table [CLI].[AttributesTable]    Script Date: 02/07/2013 15:01:34 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [CLI].[AttributesTable](
    [AttributesTableID] [int] NOT NULL,
    [ChartID] [int] NOT NULL,
    [AdmitCounty] [int] NULL,
    [AdmissionReason] [int] NULL,
    [Gender] [int] NULL,
    [Race] [int] NULL,
    [Occupation] [int] NULL,
    [MaritalStatus] [int] NULL,
    [Education] [int] NULL,
    [SpecialEducation] [int] NULL,
    [Impairment] [int] NULL,
    [Hispanic] [int] NULL,
    [HearingStatus] [int] NULL,
    [ExpectedPaysource] [int] NULL,
    [PublicAssistance] [int] NULL,
    [Dietary] [int] NULL,
    [EmploymentStatus] [int] NULL,
    [LivingArrangements] [int] NULL,
    [IncomeSource] [int] NULL,
    [LegalStatus] [int] NULL,
    [CommitType] [int] NULL,
    [EnrolledInSchool] [nchar](10) NULL,
    [GradePointAverage] [int] NULL,
    [EducationProgram] [int] NULL,
    [HIV] [int] NULL,
    [SelfHelpPrograms] [int] NULL,
    [MediationPrescribed] [int] NULL,
    [DischargeReason] [int] NULL,
    [DischargeReferral] [int] NULL,

 CONSTRAINT [PK_AttributesTable] PRIMARY KEY CLUSTERED 
(
    [AttributesTableID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AdmissionReason_Code] FOREIGN KEY([AdmissionReason])
REFERENCES [LKP].[AdmissionReasonCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AdmissionReason_Code]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_CommitType_Code] FOREIGN KEY([CommitType])
REFERENCES [LKP].[CommitTypeCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_CommitType_Code]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_Dietary_Code] FOREIGN KEY([Dietary])
REFERENCES [LKP].[DietaryCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_Dietary_Code]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_DischargeReason_Code] FOREIGN KEY([DischargeReason])
REFERENCES [LKP].[DischargeReasonCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_DischargeReason_Code]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_DischargeReferral_Code] FOREIGN KEY([DischargeReferral])
REFERENCES [LKP].[DischargeReferralCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_DischargeReferral_Code]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_Chart] FOREIGN KEY([ChartID])
REFERENCES [CLI].[Chart] ([ChartID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_Chart]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_CommitTypeCode] FOREIGN KEY([CommitType])
REFERENCES [LKP].[CommitTypeCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_CommitTypeCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_EducationCode] FOREIGN KEY([Education])
REFERENCES [LKP].[EducationCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_EducationCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_EducationProgramIndicatorCode] FOREIGN KEY([EducationProgram])
REFERENCES [LKP].[EducationProgramIndicatorCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_EducationProgramIndicatorCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_EmploymentStatusCode] FOREIGN KEY([EmploymentStatus])
REFERENCES [LKP].[EmploymentStatusCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_EmploymentStatusCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_GenderCode] FOREIGN KEY([Gender])
REFERENCES [LKP].[GenderCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_GenderCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_GPACode] FOREIGN KEY([GradePointAverage])
REFERENCES [LKP].[GPACode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_GPACode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_HearingStatusCode] FOREIGN KEY([HearingStatus])
REFERENCES [LKP].[HearingStatusCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_HearingStatusCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_HispanicCode] FOREIGN KEY([Hispanic])
REFERENCES [LKP].[HispanicCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_HispanicCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_HIVTestCode] FOREIGN KEY([HIV])
REFERENCES [LKP].[HIVTestCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_HIVTestCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_ImpairmentCode] FOREIGN KEY([Impairment])
REFERENCES [LKP].[ImpairmentCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_ImpairmentCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_IncomeSourceCode] FOREIGN KEY([IncomeSource])
REFERENCES [LKP].[IncomeSourceCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_IncomeSourceCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_LegalStatusCode] FOREIGN KEY([LegalStatus])
REFERENCES [LKP].[LegalStatusCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_LegalStatusCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_LivingArrangementCode] FOREIGN KEY([LivingArrangements])
REFERENCES [LKP].[LivingArrangementCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_LivingArrangementCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_MaritalStatusCode] FOREIGN KEY([MaritalStatus])
REFERENCES [LKP].[MaritalStatusCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_MaritalStatusCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_MedicationPrescribedCode] FOREIGN KEY([MediationPrescribed])
REFERENCES [LKP].[MedicationPrescribedCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_MedicationPrescribedCode]
GO

ALTER TABLE [CLI].[AttributesTable]  WITH CHECK ADD  CONSTRAINT [FK_AttributesTable_PaySourceCode] FOREIGN KEY([ExpectedPaysource])
REFERENCES [LKP].[PaySourceCode] ([CodeID])
GO

ALTER TABLE [CLI].[AttributesTable] CHECK CONSTRAINT [FK_AttributesTable_PaySourceCode]
GO

2 个答案:

答案 0 :(得分:1)

如果您最关心的是报告,您可能希望构建一个“报告”或“分析”数据库(通常使用ETL或Extact/Transform/Load工具执行)。这些数据库通常会被选择性地去标准化 - 例如,您没有对“性别”代码表进行fk引用,而是在报告表中有'Male'。实际程度会根据需要而变化,并且应该通过视图呈现,直到出现特定的性能级别问题。

但是,对于日常工作,例如您的“实时”数据,您需要采用标准化方法。
我假设Chart表格有visitedOncollectingCaregiverId等等。任何重要的东西,(几乎)始终收集,并且'静态' ,应该进入这个表。例如,比赛在访问期间不太可能改变(并且可以说应该是静态患者记录的一部分)。
任何有点'稀疏',但经常收集的东西,可能需要进入“相关”表。类似的东西:

Education_Status
=================
chart_id  -- fk reference to chart.id
school  -- fk reference to attribute-specific code table
program  -- see above
grade_point_average  -- DECIMAL(3, 2)

...等等。
任何完全可选的,很少收集的东西都可能最终出现在EAV中。请注意,根据“区域”,这些设置可能不止一个;另外,这些也应尽可能“编码”出来:

Unusual_Symptom_Type
======================
id  -- autogen
name  -- varchar(20) (eg - 'Turning Blue', 'Allergic to Oxygen', etc)

Unusual_Symptom
====================
chart_id  -- fk reference to chart.id
unusual_symptom_type_id  -- fk reference to unusual_symptom_type.id
note -- varchar(20) (eg - 'Afternoon Sky', 'Combusts', etc)

当然,您必须重新组合此报告。这是“交易”数据库固有的“弱点” - 数据质量通常不错,更新很简单,但以“可读”格式将它们全部粘在一起可能会很痛苦。尽可能构建视图,将相关区域粘在一起。

答案 1 :(得分:0)

我认为观点,可能是较小的观点,是你能做的最好的事情。多少?根据您的情况和要求。您可以为每个报告或报告组创建一个视图。这一切都取决于......这就是我的想法和意愿。仅供参考 - 我不是DBA,而是开发人员。