简体   繁体   English

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

[英]Best practice for tables with large number of lookup columns

OK so a little background...being a programmer-first with limited DB design experience, I made a couple newb mistakes, namely a unified code lookup table and an EAV implementation on another table. 好的,所以有一点背景...在数据库设计经验有限的情况下,我是程序员第一,我犯了一些新错误,即统一代码查找表和另一个表的EAV实现。 I'm currently trying to rework those into more of a traditional RDBMS, but I want to make absolute sure I'm doing it right this time. 我目前正在尝试将其改编为更多的传统RDBMS,但我想绝对确保这次我做对了。

My big problem table is a large table of 150 or so columns that contains client data we have to report to the State. 我的大问题表是一个包含150个左右列的大表,其中包含我们必须向纽约州报告的客户数据。 Most of this data uses state-specific lookup values, so with my current design plan, I will end up with 75-100 FK's to different lookup tables. 这些数据大多数使用特定于状态的查找值,因此,按照我当前的设计计划,最终我将获得75-100 FK到不同的查找表。

Now we will need to do reporting on this data, so I will need to be able to easily reference both the code values & descriptions for each attribute. 现在我们需要对该数据进行报告,因此我将需要能够轻松地引用每个属性的代码值和描述。 The only option I can think is to create two seperate views (or perhaps one massive view) that will flatten the data out for me. 我能想到的唯一选择是创建两个单独的视图(或一个大视图),这些视图将为我整理数据。 It seems like a fairly tedious process to create and maintain, especially given that the data collected can change. 创建和维护似乎是一个相当繁琐的过程,尤其是考虑到所收集的数据可能会发生变化。 I'm ok doing the legwork if this is the standard practice, but I'm curious is there is a better method I'm just unaware of. 如果这是标准做法,我可以做些腿法,但是我很好奇,是我没有意识到的更好的方法。

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

If you're mostly concerned about reporting, you may wish to construct a 'reporting' or 'analytics' database (usually performed with an ETL, or Extact/Transform/Load , tool). 如果您最关心报表,则可能希望构建“报表”或“分析”数据库(通常使用ETL或Extact / Transform / Load工具执行)。 These databases are often selectively de-normalized - for instance, instead of having fk references to a 'gender' code table, you'd have 'Male' in your reporting table. 这些数据库通常会选择性地进行非规范化处理-例如,在报告表中将没有'Male'字样,而是使用fk引用“性别”字代码表。 The actual degree varies as necessary, and should probably be presented through a view, until there are specific performance-level problems. 实际程度会根据需要变化,并且可能应该通过视图提出,直到出现特定的性能级别问题为止。

However, for day-to-day work, such as what your 'live' data looks like, you'll want a normalized approach. 但是,对于日常工作(例如“实时”数据的样子),您将需要一种标准化的方法。
I'm assuming the Chart table has something like visitedOn , collectingCaregiverId , etc. Anything important, which is (almost) always collected, and 'static', should go in this table. 我假设Chart表具有类似visitedOncollectingCaregiverId类的visitedOn 。任何重要的事物(几乎总是被收集)和“静态”事物都应放在此表中。 Race, for example, isn't likely to change during the course of a visit (and arguably should be part of the static patient record). 例如,种族在访问过程中不太可能改变(可以说应该是静态患者记录的一部分)。
Anything which is somewhat 'sparse', but often collected, probably needs to go in a 'related' table. 任何有点“稀疏”但经常收集的东西都可能需要放在“相关”表中。 Something like: 就像是:

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)

... and so on. ... 等等。
Anything which is completely optional, and rarely collected, may end up in an EAV. 任何完全可选且很少收集的内容都可能会出现在EAV中。 Note that there may be more than one of these setups, based on 'area'; 请注意,根据“区域”,这些设置可能不止一种; also, these should also be 'coded out', as much as possible: 同样,这些也应尽可能“编码”:

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)

You'd have to re-assemble this for reporting, of course. 当然,您必须重新组合才能进行报告。 This is the inherent 'weakness' of a 'transactional' database - the data quality is usually decent, and updates are simple, but sticking it all together in a 'readable' format can be painful. 这是“事务性”数据库的固有“弱点”-数据质量通常很不错,并且更新很简单,但是将它们全部组合成“可读”格式可能会很痛苦。 Construct views where possible, to stick relevant areas together. 尽可能构建视图,将相关区域粘合在一起。

I think the views, possibly smaller views, is the best thing you can do. 我认为这些视图(可能是较小的视图)是您可以做的最好的事情。 How many? 多少? Depending on your situation and requirements. 根据您的情况和要求。 You may create a view for each report or group of your reports. 您可以为每个报告或报告组创建一个视图。 It all depends... This is what I think and would do. 这全都取决于...这就是我的想法并且会做。 FYI - I'm not a DBA, but developer. 仅供参考-我不是DBA,而是开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 中查找表的最佳实践 - Best Practice For Lookup Tables In SQL 在多列上连接大量表的策略? - A strategy to join a large number of tables on multiple columns? 索引是否可以帮助包含大量列但记录数量较少的表? - Do indexes help tables with large number of columns but small number of records? 将大量Wikipedia表刮到MySQL数据库的最佳方法 - Best method to scrape large number of Wikipedia tables to MySQL database 将大型csv导入规范化的关系数据库(具有多个表)的最佳实践是什么 - What's the best practice to import a large csv to normalized relational database( with multiple tables) Mysql 连接表 - 最佳实践? - Mysql Join tables - best practice? 将表与案例连接的最佳实践 - Best practice for joining tables with case 具有不同内容的表格的最佳实践 - Best practice for tables with varying content SQL:如何将大量列和大量表中的特定值连接到一个查询中? - SQL: How to join specific values from a large number columns and a large number of tables into one query? 如何合并两个具有不同列号的表,同时删除具有大量列的表的重复项 - How do I merge two tables with different column number while removing duplicates for tables with a large number of columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM