Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pmotewar
Yak Posting Veteran
62 Posts |
Posted - 2009-05-15 : 09:22:38
|
Hi all,Following is my query taking 2.52 min for retrieving data. kindly suggest me solutionSELECT SCH.Scheme_Id As SchID,SCH.Scheme_Name As SchName,MAX(CP.Port_Date) As [Date],CM.Company_Name As CompName,CN.Nature_Name As NatureName,SM.Sector_Name As SectName,I.Instrument_Name As InstName,R.Rating_Name As RatName,Sum(CP2.Mkt_Value) As MktVal,Sum(CP2.Mkt_Value) AS NetAsset,Sum(ISNULL(CP2.No_Of_Shares,0)) As SH FROM T_COM_POT CP INNER JOIN T_COM_POT_DTLS1 CP1 ON CP.Port_ID = CP1.Port_ID INNER JOIN T_FUND_MASTER FM ON CP.Fund_ID = FM.Fund_Id INNER JOIN T_COMPANY_MASTER CM ON CP1.Company_ID = CM.Company_IdINNER JOIN T_MF_MASTER MF ON FM.Mutualfund_Id = MF.MutualFund_ID INNER JOIN T_COMPANY_NATURE CN ON CP1.Com_Nature_ID = CN.Com_Nature_IDLEFT JOIN T_INSTRUMENT I ON CP1.Instrument_ID = I.Instrument_IDLEFT JOIN T_RATING R ON CP1.Rating_ID = R.Rating_IDLEFT JOIN T_SECTOR_MASTER SM ON CM.Sector_Id = SM.Sector_IDINNER JOIN T_SCHEMES_MASTER SCH ON SCH.Fund_Id = FM.Fund_IdINNER JOIN T_COM_POT_DTLS2 CP2 ON CP1.Com_Pot_N_DTLS1_ID = CP2.Com_Pot_N_DTLS1_IDGROUP BY SCH.Scheme_ID,SCH.Scheme_Name,CM.Company_Name,CN.Nature_Name,SM.Sector_Name,I.Instrument_Name,R.Rating_Namealso i am giving the record count for tablesT_COM_POT: 45853T_FUND_MASTER 3219T_COMPANY_MASTER 5549T_MF_MASTER 55T_COMPANY_NATURE 6T_INSTRUMENT 64T_RATING 78T_SECTOR_MASTER 56T_SCHEMES_MASTER 10264T_COM_POT_DTLS2 1287949T_COM_POT_DTLS1 1287949and each column used in where clause are primary keys in their respective tables. please suggest me solution. this query is used in while loop this query returns output for many times. Pankaj |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-15 : 09:54:35
|
If possible, rewrite query to query the tables in the order of records.Start with t_company_nature and add an OPTION (FORCE ORDER) at the end of query. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2009-05-15 : 10:01:13
|
| First, I don't see a where clause so it is processing each row in T_COM_POT that meets all the "ON" criteria. Second, why would you be using this in a while loop, for what purpose? You're not passing any variables so it returns more or less the same rowset each time it's called - just seems kind of pointless. Lastly, please provide DDL for the tables, sample input and expected output. Row counts are kind of useless as none are large by any means.Terry-- Procrastinate now! |
 |
|
|
pmotewar
Yak Posting Veteran
62 Posts |
Posted - 2009-05-16 : 02:36:10
|
Hi Thanks for reply,actually this query returns result based on scheme id's and datefollowing is my where clause where date is changedWhere Month(CP.Port_Date) = Month('02-01-2009') AND Year(CP.Port_Date) = Year('02-01-2009') AND SCH.Parent_Scheme_Flag = 1 And SCH.Scheme_Id IN ('1','34','654','345','359','223') GROUP BY SCH.Scheme_ID,SCH.Scheme_Name,I.Instrument_Name ORDER BY 3 Desc,1 Ascquote: Originally posted by tosscrosby First, I don't see a where clause so it is processing each row in T_COM_POT that meets all the "ON" criteria. Second, why would you be using this in a while loop, for what purpose? You're not passing any variables so it returns more or less the same rowset each time it's called - just seems kind of pointless. Lastly, please provide DDL for the tables, sample input and expected output. Row counts are kind of useless as none are large by any means.Terry-- Procrastinate now!
Pankaj |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-16 : 02:41:29
|
[code]Where Month(CP.Port_Date) = Month('02-01-2009') AND Year(CP.Port_Date) = Year('02-01-2009') [/code]applying function on the column will result in not able to use any index you have the column.try changing to[code]where CP.Port_Date >= '2009-01-01' -- greater and equal to 1st of the monthand CP.Port_Date <= '2009-01-31' -- lesser and equal to last day of the month[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-16 : 02:42:10
|
Try this. This rewrite will allow you to utilize an index over port_date columnWhere CP.Port_Date >= '20090201'-- Feb 1 2009 AND CP.Port_Date < '20090301'-- March 1 2009 AND SCH.Parent_Scheme_Flag = 1 And SCH.Scheme_Id IN ('1','34','654','345','359','223')GROUP BY SCH.Scheme_ID, SCH.Scheme_Name, I.Instrument_NameORDER BY 3 Desc, 1 Asc E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|