Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do ADO.NET DataTable Constraints affect performance?

Tags:

c#

.net

ado.net

Do constraints on a DataTable (e.g. PrimaryKey & UniqueContraint) make Selects more efficient in the same way that they would in SQL Server? Or is their only purpose to enforce rules on the data?

myDT.Constraints.Add("PK", myDT.Columns["UniqueID"], true); //add a primary key
myDT.Constrinats.Add(new UniqueConstraint(new DataColumn[] { //add a unique constraint for UserID
    myDT.Columns["UserID"], myDT.Columns["UniqueID"]
})); 

Would these examples potentially have better performance when looking up data in the DataTable by UniqueID or UserID?

like image 590
Seibar Avatar asked Jun 18 '09 13:06

Seibar


People also ask

Which object of ADO.NET has best performance?

A DataReader object has faster access to data.

Which .NET data provider offers best performance when connected to SQL Server database?

ANSWER: The SqlClient data provider. The SqlClient data provider gives the maximum performance from a connection to SQL Server.

Which of the following is not true for ADO.NET DataSet?

- DataSet provides a disconnected view of a data source. - We can create relationship between the tables in a DataSet. 'not true for ADO.NET DataSet' but it is true statements... that we can set a relationship between tables in dataset.

Which ADO.NET object acts as a bridge between DataSet and database?

A DataAdapter object is used as a bridge between a database and a DataSet. It can update both the DataSet object and the original data-source to match the changes in the other, as required.


1 Answers

I think you are confusing the use of primary keys and constraints (business domain model) with the use of indexes (performance).

A Foreign Key can influence an optimiser, and it is common to create an index on Foreign keys.

In the SQL Server world, a Primary key is often confused with a Clustered index, because more often than than a surrogate key (think auto-increment identity column) is choosen as the Primary Key and Clustered Index.

This article may be of interest: DataSet and DataTable in ADO.NET 2.0.

In response to your comment:

Use a DataView for Repetitive Non-Primary Key Searches If you need to repetitively search by using non-primary key data, create a DataView that has a sort order. This creates an index that can be used to perform the search. This is best suited to repetitive searches because there is some cost to creating the index.

The DataView object exposes the Find and FindRows methods so that you can query the data in the underlying DataTable. If you are only performing a single query, the processing that is required to create the index reduces the performance that is gained by using the index.

When you create a DataView object, use the DataView constructor that takes the Sort, RowFilter, and RowStateFilter values as constructor arguments along with the underlying DataTable. Using the DataView constructor ensures that the index is built once. If you create an empty DataView and set the Sort, RowFilter, or RowStateFilter properties afterwards, the index is built at least two times.

like image 135
Mitch Wheat Avatar answered Sep 19 '22 05:09

Mitch Wheat