With the following table structure:
Table 1: Customers
CustID(primary key) | CustName(indexed)
----------------------------------
C1 Cust1
C2 Cust2
. Cust3
. Cust.
. Cust.
C10000 Cust10000
Table 2: CustomFields
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.
Trying to output the following;
CustID | Field 1 | Field 2 | Field 3 | .... | Field N
----------------------------------------------------------
I tried writing a query like
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%'
Since the CustomFields
table holds a few million records, the above query does not perform well. Now it takes about 10-12 seconds (for 500 customers and 6 fields)
I think the left outer joins are adding time here. Any thoughts to solve the problem would be really helpful?
Platform : SQL Server 2005
Updated :
CustomFields
table is a generic table and it can contain fields of any other entities (vendors, items etc..).