【发布时间】:2011-05-05 15:56:30
【问题描述】:
我有一个包含大量重复字符串数据的大表。为了节省空间,我将字符串数据移到了单独的表中。我的表格现在看起来像这样:
MyRecords
RecordId (int) | FieldA (int) | FieldB (datetime) | FieldC (...) | MyString1Id (int) | MyString2Id (int) | MyString3Id (int) | ...
MyStrings
StringId (int) | StringValue (varchar)
MyRecords 表有大约 10 个指向字符串表的外键。我有一个存储过程GetMyRecords,它检索具有实际字符串值的记录列表。对于每个字符串关系,这个 sp 现在有 10 个到字符串表的连接:
SELECT [Field1], [Field2], [Field3], ..., [Strings1].[StringValue], [Strings2].[StringValue], ...
FROM MyRecords INNER JOIN
MyStrings AS Strings1 ON MyRecords.MyString1Id = Strings1.StringId INNER JOIN
MyStrings AS Strings2 ON MyRecords.MyString2Id = Strings2.StringId INNER JOIN
MyStrings AS Strings3 ON MyRecords.MyString3Id = Strings3.StringId INNER JOIN
(more joins)
WHERE [Field1] = @Field1 AND [Field2] = @Field2
GetMyRecords 比我想要的慢得多,因为所有的连接。我怎样才能提高这个 sp 的性能?我能不能把它变成一个单一的连接?
strings 表在StringId 上有一个聚集主键,所有 where 字段都在MyRecords 表的非聚集索引中。
【问题讨论】:
-
你真的应该规范你的结构……这不是真正可扩展的,维护起来很残酷。
标签: sql-server sql-server-2008 stored-procedures join