• Shot in the dark...

    Could be a stale statistics issue. If the query runs fine for a couple of days and then start misbehaving, you statistics might be outdated due to updates to the underlying tables.

    I would compare the execution plan of the ad-hoc query in SSMS and the execution plan of the statement executed by the app while it's running. You can capture it from sys.dm_exec_query_plan(plan_handle) passing the plan_handle found in sys.dm_exec_requests.

    If the plans are different (they likely are), check if the two statements are executed in the exact same way (literal values/parameters, direct execution/sp_executesql). I would check for outdated statistics and parameter sniffing.

    Hope this helps

    Gianluca

    -- Gianluca Sartori