I have a stored procedure that is not only huge, but very memory intensive. This first stored procedure is created for a detail report. The second stored procedure calls the first stored procedure, places the information into a Temporary table and then selects the information from that temporary table and summarizes it.
Now, the second stored procedure doesn’t need all of the information in the detail stored procedure, it only needs some of it.
Our DBA has called this approach reckless and has made other suggestions, such as “persist this to a real table so that it is run one-time per day”. This actually isn’t going to happen because numerous users can run either report numerous times per day and for different date ranges. His other suggestion was to SUM without the temp table, but I’m not sure how you select from one stored procedure only certain fields.
I have two questions: How would I follow his second suggestion of SUM without the temp table and if that can’t be done, does anyone have any suggestions on the best approach to use to summarize the needed data for the Summary report?
Both of these stored procedures are for separate reports. One is for the detail and the other report is the summary. The end users can run them for different stores and different time periods.
Why not put the data in a view and hit the view from both stored procs? This would reduce the complexity in both procs (but move it to the view).
Depending on the structure of your query you could possibly even create an indexed view which would create a persisted form of the data and allow you to make use of indexes.