Reputation: 1
We want to SUM amounts for all dates in a range provided.
Logic is like - in while loop we are taking sum of amount column for date range and adding that sum in another variable
This is taking 1-3 minutes to executes depending on date range.
And because of this we are getting timeout exception in code side.
Can we make changes in stored procedure to reduce time or is there any other way to handle this in code so that exception will not be thrown.
Please Help...
Upvotes: 0
Views: 1986
Reputation: 39255
If your query really needs that amount of time, you can prevent timeouts by setting the CommandTimeout property of the SqlCommand to an appropriate value.
But first check whether the query can be optimized.
Upvotes: 0
Reputation: 37566
The right way would be to optimize your procedure, but since you didn't post the code i cannot say anything about it, the other way would be to handle the symptom so no Exception get thrown by increasing the Timeout in your Connectionstring like:
"CommandTimeout = try any greater value;"
Upvotes: 0
Reputation: 915
There is no one way to write a query. If you find that a query with an OUTER JOIN is taking a long time, rewrite it using an INNER JOIN. Or perhaps the WHERE clause is written in such a way that it is causing excessive database read operations.
While normalizing tables results in ideal database design, it often leads to poor querying. If you find that frequently-used queries have to span too many normalized tables, consider some duplication of data.
Lack of well-defined indexes is a frequent cause of queries taking a long time to execute. Creating indexes will cause a query to lookup the index rather than the table.
Start with activating Query Execution Time in your stored procedure to see what takes time
SET STATISTICS TIME ON Select * from ......
You could also view graphical execution plan of your query by: Click Query > Include Actual Execution Plan.
You can read more about Query performance tuning on: Pinal Dave's Blog, he is the best when it comes to this.
Upvotes: 3