加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:42:29 所属栏目:百科 来源:网络整理
导读:对DataTable(例如,PrimaryKey和UniqueContraint)的约束是否使得选择效率与SQL Server中的相同?或者他们唯一的目的是对数据实施规则? myDT.Constraints.Add("PK",myDT.Columns["UniqueID"],true); //add a primary keymyDT.Constrinats.Add(new UniqueConst
对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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读