具有以下表结构:
表 1:客户
CustID(primary key) | CustName(indexed)
----------------------------------
C1 Cust1
C2 Cust2
. Cust3
. Cust.
. Cust.
C10000 Cust10000
表 2:自定义字段
FieldID (primary key) | ID (indexed) | FieldValue
---------------------------------------------------------------------
1 C1 Test
2 C2 Test
3 C3 Test
4 C4 Test
. . Test
. . Test
few millions Z1 Test
"ID" column is indexed.
尝试输出以下内容;
CustID | Field 1 | Field 2 | Field 3 | .... | Field N
----------------------------------------------------------
我试着写一个像
Select
CustID, A1.FieldValue as [Field 1], A2.FieldValue as [Field 2]
from
Customers
left outer join
CustomFields A1 on Customers.custID = A1.ID
left outer join
CustomFields A2 on Customers.custID = A2.ID
left outer join
CustomFields An on Customers.custID = An.ID
where
custName like 'C%'
由于该CustomFields
表包含几百万条记录,因此上述查询表现不佳。现在大约需要 10-12 秒(对于 500 个客户和 6 个字段)
我认为左外连接在这里增加了时间。解决问题的任何想法都会真正有帮助吗?
平台:SQL Server 2005
更新 :
CustomFields
table 是一个通用表,它可以包含任何其他实体(供应商、项目等)的字段。