Ad hoc query running crazy slow.



Hi,
I have a job that execute a stored proc. It was running fine like 3
minutes to finish but a week ago it started to take 4 hours. Its using
ad hoc quries in that stored procedure. I reindex all the tables that
it was using. I check execution plan there was no tabloe scans. I
recompile the stored proc but no use. its sql 2000 with service pack 3
installed on it. Here is the query anyone has any clue what went wrong
suddenly. Link server is working fine.

i am running it like this

exec usp_MO_ActivityTradesBPS 'CORP'


CREATE PROCEDURE dbo.usp_MO_PositionsBPS


@Load varchar(25) = null,
@AsOfDate datetime

AS

Set @Load = '%'

If @Load is null or @Load = ''

begin

Set @Load = '%'

end

If IsNull(@AsOfDate,'') = ''
Set @AsOfDate = dbo.fn_BusinessDate(GetDate()-1)

If @Load = 'SANFRAN'

begin

Set @Load = '%'

SELECT RTRIM(T.BRANCHCD) + '-' + RTRIM(T.ACCOUNTNR) + '-' +
T.ACCOUNTTYPECD + T.CUSIPNR [key],
RTRIM(T.BRANCHCD) + '-' + RTRIM(T.ACCOUNTNR) + '-' + T.ACCOUNTTYPECD
BRANCHCD, T.TICKERSYMBOL, T.ADPSECURITYNR, T.CUSIPNR,
T.ADPSECURITY1DS, T.TDQUANTITY, T.SECURITYPRICEAM, T.LEDGERBALANCEAM,
T.TDDIVINTAM, T.TDGRCREDITMTDOP7,
T.TDMTDSECFEE, T.MTDREALPLTDOP7, T.TDCUMPL, T.MTDPL, T.MTDNETPL,
T.DAILYNETPL, T.CURRENCYISOCD
FROM BPS_Positions T
WHERE T.BRANCHCD = '001'
and T.BRANCHCD + '-' + T.ACCOUNTNR not in (Select Distinct
SM.Bps_AcctNr
From MO_InvStrategyMap SM
Where SM.Bps_AcctNr is not null)
and (T.TDMTDSECFEE <> 0
or T.TDCOmsnAm <> 0
or T.TDQUANTITY <> 0
or T.MarketValAm <> 0
or T.MTDNETPL <> 0
or T.MTDPL <> 0
or T.LEDGERBAlanceAm <> 0
or T.DAILYNETPL <> 0
or T.TDDivIntAm <> 0
or T.TDGRCREDITMTDOP7 <> 0)
ORDER BY T.BRANCHCD, T.ACCOUNTNR, T.ACCOUNTTYPECD, T.ADPSECURITYNR,
T.CUSIPNR, T.CURRENCYISOCD

end

Else

begin

SELECT AM.Inventory + T.CUSIPNR [Key], rtrim(T.BRANCHCD) + '-' +
rtrim(T.ACCOUNTNR) BRANCHCD, T.TICKERSYMBOL, T.ADPSECURITYNR,
T.CUSIPNR,
T.ADPSECURITY1DS, T.TDQUANTITY, case when T.TDQuantity <> 0
then T.MarketValAm/T.TDQuantity
else T.SecurityPriceAm end as SecurityPriceAm,
T.LEDGERBALANCEAM, T.TDDIVINTAM, T.TDGRCREDITMTDOP7,
T.TDMTDSECFEE, T.MTDREALPLTDOP7, T.TDCUMPL, T.MTDPL, T.MTDNETPL,
T.DAILYNETPL, T.CURRENCYISOCD
FROM BPS_PositionsHistorical T
JOIN MO_InvAccountMap AM
ON T.BRANCHCD + '-' + T.ACCOUNTNR = AM.AccountNr
JOIN (Select Distinct TraderCd
From MO_InvStrategyMap
Where PrimaryRecFl = 1
and (Desk like @Load
or Category like @Load
or SubCategory like @Load
or TraderName like @Load
or TraderCd like @Load)) SM
ON SM.TraderCd = AM.Inventory
WHERE T.BusinessDate = @AsOfDate
and AM.DataSource like 'BPS%'
and (T.TDMTDSECFEE <> 0
or T.TDCOmsnAm <> 0
or T.TDQUANTITY <> 0
or T.MarketValAm <> 0
or T.MTDNETPL <> 0
or T.MTDPL <> 0
or T.LEDGERBAlanceAm <> 0
or T.DAILYNETPL <> 0
or T.TDDivIntAm <> 0
or T.TDGRCREDITMTDOP7 <> 0)
ORDER BY AM.Inventory, T.BRANCHCD, T.ACCOUNTNR, T.ACCOUNTTYPECD,
T.ADPSECURITYNR, T.CUSIPNR, T.CURRENCYISOCD

end
GO

.



Relevant Pages

  • Re: "Script timed out" error
    ... server won't be on its knees everytime a dozen clients hit this stored proc ... In any case there is no mystery here, default timeout for ASP script is 90 ... many rows to the recordset and/or browser, ... rare for a user to wait much more than a minute for a page to load. ...
    (microsoft.public.inetserver.asp.db)
  • Re: emailing in batch... should i?
    ... SQL Mail or VB.NET? ... It really depends on which server is under load, ... loop, they are optimized for this type of delivery. ... loop through it all via stored proc or trigger and send the mail using ...
    (microsoft.public.dotnet.framework.aspnet)
  • Filter Datagrid?
    ... I have 3 datagrids on my page that uses data from one stored proc. ... If category = 1 then load that data into grid 1 ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Re: Ad hoc query running crazy slow.
    ... Looking at the query plan, where are you spending most of your time? ... I have a job that execute a stored proc. ... Set @Load = '%' ... JOIN (Select Distinct TraderCd ...
    (microsoft.public.sqlserver.clients)
  • Re: To clear a listbox
    ... >its value property during the load. ... I have no code in my Form_Load to fire the option button. ... If there are multiple option buttons and all are originally False, ... of the buttons will change to True and its OptionX_Click will execute ...
    (microsoft.public.vb.controls)