2011-11-29 55 views
0

我需要创建一个包含一千个字段(列)的表格,我不知道如何处理表现以及如何维护它,请帮助我提出建议。数据库性能和1000列维护。

+3

为什么你需要1000列? –

+1

来一次@AdamWagner:我几乎肯定你*不*需要1000列。描述你的场景;但我敢打赌,你阅读数据库规范化将是一个更好的解决方案。 – Amadan

+1

请不要这样做。它是可以完成的那些事情之一,但不应该有很多原因。一个正常的人不能保存1000条信息。 2. SQL Server每个数据行有8k的限制,所以你要使用大对象类型,我想你会使用1000 varchar(max),你将永远无法优化它。它现在不是数据库,它是一个电子表格。 –

回答

1

如果绝大多数时间大多数值为NULL,那么您应升级到SQL Server 2008并使用稀疏列,请参阅Using Sparse ColumnsUsing Column Sets

如果你的列值不是大部分NULL,那么我质疑你的数据模型的健全性。

0

你将不得不首先要做的事情

  1. 正常化
  2. 定义实体,并分离出不同的表。绘制ER图和
    你会得到更多的想法。
  3. 如果列是varchar或text,则不要超过15列表列,因为
    那么SQL将不得不将数据存储在不同的页面中。如果列是
    布尔值,那么它可以是大约30.
  4. 根据您的数据正确定义聚簇索引,因为这样可以优化查询。

由于问题的性质没有太多的细节,答案也是通用的,从100英尺的角度来看。

0

再次,请不要这样做。

查看关于数据库的Entity Attribute Value模型。它将帮助您在实体上存储大量稀疏属性,并且不会使数据库流失。

的基本概念如下所示

create table #attributes 
(
    id int identity(1,1), 
    attribute varchar(20), 
    attribute_description varchar(max), 
    attribute_type varchar(20) 
) 

insert into #attributes values ('Column 1','what you want to put in column 1 of 1000','string') 
insert into #attributes values ('Column 2','what you want to put in column 2 of 1000','int') 

create table #entity 
(
    id int identity(1,1), 
    whatever varchar(max) 
) 

insert into #entity values ('Entity1') 
insert into #entity values ('Entity2') 

create table #entity_attribute 
(
    id int identity(1,1), 
    entity_id int, 
    attribute_id int, 
    attribute_value varchar(max) 
) 


insert into #entity_attribute values (1,1,'e1value1') 
insert into #entity_attribute values (1,2,'e1value2') 
insert into #entity_attribute values (2,2,'e2value2') 

select * 
from #entity e 
join #entity_attribute ea on e.id = ea.entity_id 

之间发生的事情在#entity表,并在发生什么的#attribute表是有点依赖于应用程序,但一般情况下会是这样那就是区别从不为空,每次需要实体时都会被访问,我会将其限制为10个左右的项目。

让我猜这是一个医疗应用程序?