Question

I am looking for the fastest way to get a unique record set from 4 tables. They are joined together with performance joins using indexs. Also there are duplicates in the results after the select from the join. Would it be faster to pull the records using the joins into a CTE (Common Table Expression/WITH Statement) and then select from the CTE with a group by or would it be faster to use the group by on the original data pull?

i.e.

SELECT     
account 
,customer
,class
,CURRENT DATE
,contract_date
FROM ACCOUNT acct
INNER JOIN measure msr 
ON acct.CUST_ID = msr.CUST_ID 
INNER JOIN products prod 
ON msr.PROD_ID = prod.PROD_ID 
INNER JOIN customer x 
ON acct.CUST_ID = x.CUST_ID 
WITH UR

There are a bunch of dups in here so would it be faster to just group by the 5 columns in the select or add a distinct or put this in a CTE/WITH statement and then select with group from the CTE?

Was it helpful?

Solution

It depends on your data pattern and size of data. There are no thumb rules. I would say try with all possible option and check execution plan, should give you the best option.

~ Santosh

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top