c# – ADO.NET DataTable约束如何影响性能?

前端之家收集整理的这篇文章主要介绍了c# – ADO.NET DataTable约束如何影响性能?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对DataTable(例如,PrimaryKey和UniqueContraint)的约束是否使得选择效率与sql Server中的相同?或者他们唯一的目的是对数据实施规则?
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"]
}));

在通过UniqueID或UserID查找DataTable中的数据时,这些示例是否可能具有更好的性能

解决方法

我认为您使用索引(性能)会混淆主键和约束(业务域模型)的使用.

外键可以影响优化器,并且通常在外键上创建索引.

sql Server世界中,主键通常与聚簇索引混淆,因为选择代理键(认为自动增量标识列)的次数通常是主键和聚簇索引.

这篇文章可能是有趣的:DataSet and DataTable in ADO.NET 2.0.

回应你的评论

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,or RowStateFilter properties afterwards,the index is built at least two times.

猜你在找的C#相关文章