Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a large SQL statement and populating a gridview

I have a fairly large SQL statement which has a number of inner joins and cross apply statements to do some calculations which creates new columns for each entry based on the previous query.

I was wondering what the most efficient / best way would be to run this query from C# in code to populate a gridview?

Breaking the SQL up and write some of the calculations in C#?

Creating a stored procedure?

Just passing the sql in as a string and creating a dataset and then binding the dataset to the gridview seems to be taking too long.

like image 545
user1186144 Avatar asked Oct 09 '22 07:10

user1186144


1 Answers

Your core problem is that you database query must run faster. Once you have a solution for that, you can adjust your code accordingly.

The first thing I would look at is to optimize your database indexes to increase your query performance. If you can, use the SQL Profiler and Index Tuning Wizard (I don't think these tools are available in the Express edition of SQL Server). Please note that new indexes can also cause performance degradation with inserts, so if your database needs to support high transaction volume, you should be careful about using this approach.

If you can find a way to increase your query performance by breaking down the query into parts and aggregating the results with procedural code, then it would make sense to move to an ObjectDataSource. This will allow you maximum flexibility in how the data is acquired.

As a starting point, I would prototype your query and/or parts of your query in a SQL editor to that you can focus on just query optimization before you make any decisions about how your ASP.NET code will change.

I can't comment further on exactly how to optimize your data extraction without looking at the specifics of your schema and the queries you're using. But this should get you going in the right direction.

like image 67
Paul Keister Avatar answered Oct 13 '22 11:10

Paul Keister