views:

1095

answers:

6

I decided not to use an orm and going to use straight ADO.NET for my project. I know I know its gonna take longer to program but I just want pages to load at high speeds even at peak time.

+3  A: 

Have a look at the Improving .NET Application Performance and Scalability book (available online for free, in MSDN). There's a whole chapter about improving ADO.NET performance.

M4N
Excellent links. The only thing I'd disagree with is their emphasis on using stored procedures for performance. As I understand it, modern versions of SQL Server cache the execution plans for ad hoc SQL queries, so after an ad hoc query is run once it should be as fast as a stored procedure. However, the linked article is actually from Microsoft, so I'm not going to claim that I know better than them. In all the benchmarking I've done lately, however, the performance of stored procedures and ad hoc queries is virtually identical.
MusiGenesis
A very good book indeed. I have a printed version of it. Unfortunately it hasn't been updated since 2004 and lots of stuff has changed. The DataRow’s backing store for instance was totally rewritten in .Net 2.0 which dramatically changed the performance profile of using DataSets between versions 1.x and 2.0.
Alfred Myers
+1  A: 

Be very smart about connection management. Opening a DB connection can be very expensive so try and keep that in mind when writing the database access layer.

ghills
This shouldn't really be an issue so long as connection pooling is enabled. Instantiating a connection object and calling its `Open` method will usually just grab an available connection from the pool, rather than establishing a completely new connection to the database.
LukeH
Agreed with Luke. Connections haven't been worth worrying about (much) for more than 10 years.
MusiGenesis
I got a legacy code written in VB.Net that has everythig mixed like if it were written in VB6: no layers, no classes, all the code inside the form and the bas modules...
yelinna
A: 

If you're not going to use an ORM, are you also not going to cache your data? That's a big advantage of using an ORM. If there's no data cache, you'll need to look at ways to cache the HTML/JavaScript. This can be accomplished using OutputCache directive and SqlDependency. Or by publishing out static HTML and JavaScript files. Either way, you will be able to handle higher load if you're not constantly hitting the database on every request.

Some links:

ASP.NET Web Site Performance Improvement http://www.codeproject.com/KB/aspnet/aspnetPerformance.aspx

10 ASP.NET Performance and Scalability Secrets http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx

Omar AL Zabir blog on ASP.NET Ajax and .NET 3.5 http://msmvps.com/blogs/omar/archive/tags/performance/default.aspx

Axl
A: 

The article Martin links to is excellent. I would just add that you definitely want to use a DataReader instead of a DataSet (I love DataSets, but not for performance reasons).

MusiGenesis
Unless you have to go back to previous records in which case using a DataSet can be a good idea.
Alfred Myers
@Alfred: he said he "want[s] pages to load at high speed", so I assume he's talking about ASP.Net. In this case letting a user go back to previous records in a DataSet would mean caching the DataSet, which probably wouldn't be a good idea.
MusiGenesis
@MusiGenesis: I'm not disagreeing with you. You said you love DataSets and I’m giving an example of when they’re suitable.Even on a *super duper* high speed webpage, someone could have logic requiring access to previous records for whatever business reason. Instead of going back to the database, use a DataSet. No DataSets on cache – no nothing. Just a DataSet being used during the lifetime of the request.
Alfred Myers
@Alfred: I'm not disagreeing with you, either. :)
MusiGenesis
+1  A: 

One error I see repeated over and over again:

Instantiating and setting up everything (DbConnection, DbCommand, DbParameters) inside a method which is called repeatedly in a tight loop.

Most of the time you can refactor those local variables as class members instantiating them only once and keeping only the updates to DbParameters inside the method.


UPDATED to include sample code asked for in the comments

Disclaimer: This is a quick assembled sample for the sole intent of demonstrating the point about moving repetitive stuff out of the loop. Other better practices aren't necessarily implemented.


        public static void Show() {
            List people = new List();

            //Everything is done inside the loop
            PersonDal personDal = new PersonDal();
            foreach (Person person in people) {
                personDal.Insert(person);
            }

            //Things are setup once outside the loop.
            using (DbConnection connection = SqlClientFactory.Instance.CreateConnection()) {
                // setup the connection
                BetterPersonDal betterPersonDal = new BetterPersonDal(connection);
                connection.Open();
                foreach (Person person in people) {
                    betterPersonDal.Insert(person);
                }
            }
        }
    }

    class Person {
        public int Id { get; set; }
        public string Name { get; set; }
    }

On this first implementation, every thing is set up every time the method is called:


class PersonDal {
    public int Insert(Person person) {
        DbProviderFactory factory = SqlClientFactory.Instance;

        using (DbConnection connection = factory.CreateConnection()) {
            connection.Open();

            connection.ConnectionString = "Whatever";

            using (DbCommand command = connection.CreateCommand()) {
                command.CommandText = "Whatever";
                command.CommandType = CommandType.StoredProcedure;

                DbParameter id = command.CreateParameter();
                id.ParameterName = "@Id";
                id.DbType = DbType.Int32;
                id.Value = person.Id;

                DbParameter name = command.CreateParameter();
                name.ParameterName = "@Name";
                name.DbType = DbType.String;
                name.Size = 50;
                name.Value = person.Name;

                command.Parameters.AddRange(new DbParameter[] { id, name });

                return (int)command.ExecuteScalar();
            }
        }
    }
}

Now we move the setup to the objects construction leaving it out of the loop:


class BetterPersonDal {
    private DbProviderFactory factory;
    private DbConnection connection;
    private DbCommand command;
    private DbParameter id;
    private DbParameter name;

    public BetterPersonDal(DbConnection connection) {
        this.command = connection.CreateCommand();
        this.command.CommandText = "Whatever";
        this.command.CommandType = CommandType.StoredProcedure;

        this.id = command.CreateParameter();
        this.id.ParameterName = "@Id";
        this.id.DbType = DbType.Int32;

        this.name = command.CreateParameter();
        this.name.ParameterName = "@Name";
        this.name.DbType = DbType.String;
        this.name.Size = 50;

        this.command.Parameters.AddRange(new DbParameter[] { id, name });
    }

    public int Insert(Person person) {
        this.id.Value = person.Id;
        this.name.Value = person.Name;

        return (int)command.ExecuteScalar();
    }
}

Alfred Myers
I think I know what you are talking about but can you give an example?
Included example
Alfred Myers