Professional Documents
Culture Documents
NET
Applications
Introduction
Developing performance-oriented .NET applications is not easy. The .NET
standard includes only basic guidelines and interface definitions to help
programmers develop .NET applications. In addition, .NET data providers do
not throw exceptions to say that your code is running too slowly.
Designing a .NET application is a complex process, in part because the code
can be very data provider-specific. If you are working with several databases,
you will find differences in the programming concepts for the data providers.
You will need much more database knowledge to design your application
efficiently.
This technical brief contains guidelines compiled by examining the .NET
implementations of shipping .NET applications and analyzing the common
performance mistakes that we found. The guidelines discuss selecting .NET
objects and methods, designing .NET applications, retrieving data, and
updating data.
Following these general rules will help you solve some common .NET
system performance problems, such as those listed in the following table:
Once created, connection pools are not destroyed until the active process
ends or the connection lifetime is exceeded. Maintenance of inactive or
empty pools involves minimal system overhead.
2 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
If you are using connection pooling, opening and closing connections is not
an expensive operation. Using the Close() method of the data provider’s
Connection object adds or returns the connection to the connection pool.
Remember, however, that closing a connection automatically closes all
DataReader objects associated with the connection.
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 3 OF 13
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
// Start a transaction
OracleTransaction TransId = MyConn.BeginTransaction();
4 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
try
{
int count = (int)salCmd.ExecuteScalar();
}
catch (Exception ex)
{
// Display any exceptions in a messagebox
MessageBox.Show (ex.Message);
}
// Close the connection
Conn.Close();
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 5 OF 13
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
6 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
not a 100% managed data access solution. Only a very few vendors produce
true managed code providers that implement their entire stack as a managed
component.
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 7 OF 13
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
The end-user can often write much more efficient UPDATE and DELETE
statements than those that the CommandBuilder generates. A programmer
who knows the underlying database schema and that the EMPNO column of
the EMPLOYEES table is the primary key for the table, can code the same
UPDATE statement as follows:
UPDATE EMPLOYEES SET EMPNO = ?, ENAME = ?, JOB = ?, MGR = ?, HIREDATE = ?, SAL = ?,
COMM = ?, DEPT = ? WHERE EMPNO = ?
The latter statement will run much more efficiently on the database server
than the statement generated by the CommandBuilder.
Another drawback implicit in the design of the CommandBuilder object is that
the CommandBuilder must generate statements at runtime. Each time a
DataAdapter.Update method is called, the CommandBuilder must analyze
the contents of the result set and generate UPDATE, INSERT, and DELETE
statements for the DataAdapter. When the programmer explicitly specifies
the UPDATE, INSERT, and DELETE statements for the DataAdapter, this
extra processing time is avoided.
8 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
Retrieving Data
To retrieve data efficiently, return only the data that you need, and choose
the most efficient method of doing so. The guidelines in this section will help
you to optimize system performance when retrieving data with .NET
applications.
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 9 OF 13
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
possible, the developer should attempt to implement a method that limits the
number of columns that are retrieved.
Most users don’t want to see long data. If the user does want to process
these result items, then the application can query the database again,
specifying only the long columns in the select list. This method allows the
average user to retrieve the result set without having to pay a high
performance penalty for network traffic.
Consider a query such as
SELECT * FROM EMPLOYEES WHERE SSID = '999-99-2222'
An application might only want to retrieve this employee’s name and
address. But, remember that a .NET data provider has no idea which result
columns an application might be trying to retrieve when the query is
executed. A data provider only knows that an application can request any of
the result columns. When the .NET data provider processes the fetch
request, it will most likely return at least one, if not more, result rows across
the network from the database server. In this case, a result row will contain
all the column values for each row — including an employee picture if the
EMPLOYEES table contains such a column. Limiting the select list to contain
only the name and address columns results in decreased network traffic and
a query that performs faster at runtime.
10 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
Updating Data
This section provides guidelines to help you to optimize system performance
when updating data in databases with a .NET data provider.
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 11 OF 13
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
The following example shows the application flow for updating DataSets,
using Oracle’s Rowid as the update mechanism.
// Create the DataAdapter and DataSets
OracleCommand DbCmd = new OracleCommand
("SELECT rowid, deptid, deptname FROM department”, DBConn);
// Bind parameters
myDataAdapter.UpdateCommand.Parameters.Add
("param1", OracleDbType.VarChar,100,"deptname");
"myDataAdapter.UpdateCommand.Parameters.Add("param2",
OracleDbType.Number,4,"deptid";
myDataAdapter.UpdateCommand.Parameters.Add("param3",
OracleDbType.Number, 4, "rowid");
Conclusion
With thoughtful design and implementation, the performance of .NET
applications can be improved. By retrieving only required data, selecting
functions that optimize performance, and properly managing connections
and updates, your applications can run more efficiently and generate less
network traffic.
We welcome your feedback! Please send any comments concerning documentation, including
suggestions for other topics that you would like to see, to:
docgroup@datadirect.com
12 OF 13 D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04
T D E S I G N I N G P E R F O R M A N C E – O P T I M I Z E D . N E T A P P L I C A T I O N S
800-876-3101
DataDirect Technologies is focused on data access, enabling
info@datadirect.com software developers at both packaged software vendors and in
corporate IT departments to create better applications faster.
Worldwide Sales DataDirect Technologies offers the most comprehensive, proven
line of data connectivity components available anywhere.
Belgium (French) ..............0800 12 045
Belgium (Dutch)................0800 12 046 Developers worldwide depend on DataDirect Technologies to
France .............................0800 911 454 connect their applications to an unparalleled range of data
Germany .....................0800 181 78 76
Japan ..............................0120.20.9613 sources using standards-based interfaces such as ODBC, JDBC
Netherlands ..................0800 022 0524 and ADO.NET, as well as cutting-edge XML query technologies.
United Kingdom ..........0800 169 19 07 More than 250 leading independent software vendors and
United States..................800 876 3101
thousands of enterprises rely on DataDirect Technologies to
simplify and streamline data connectivity. DataDirect
Technologies is an operating company of Progress Software
Corporation (Nasdaq: PRGS).
www.datadirect.com
D A T A D I R E C T T E C H N O L O G I E S OCTOBER 04 13 OF 13