如何ADO.NET DataTable的约束影响性能? [英] How do ADO.NET DataTable Constraints affect performance?

查看:165
本文介绍了如何ADO.NET DataTable的约束影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在数据表(如PrimaryKey的&放大器; UniqueContraint)限制使得选择以同样的方式,他们将在SQL Server中更有效率?或者是执行规则上的数据他们唯一的目的是什么?

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"]
}));

会查找DataTable中的数据通过时,这些例子可能会有更好的表现的UniqueID 用户名

推荐答案

我想你混淆了使用的索引的使用(性能)主键和约束(业务领域模型)的。

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.

在SQL Server的世界,一个主键是经常混淆一个聚集索引,因为往往不是一个代理键(想想自动增量标识列)被choosen作为主键和聚集索引。

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.

这篇文章可能会感兴趣: DataSet和DataTable在ADO.NET 2.0

This article may be of interest: 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.

该数据视图对象公开查找   和FindRows方法,这样就可以   查询数据中的底层   数据表。如果你只执行   一个查询,那是该处理   创建索引减少了所需   由获得的性能   使用索引。

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.

当你创建一个DataView对象,使用   数据视图构造函数   排序,的RowFilter和   RowStateFilter值构造   沿着与底层参数   数据表。使用数据视图   构造确保指数是   建一次。如果你创建一个空   数据视图,并设置排序,的RowFilter,   或RowStateFilter性能   之后,该指数是建立在   至少两次。

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.

这篇关于如何ADO.NET DataTable的约束影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆