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

标签 c# .net ado.net

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

当通过 UniqueIDUserID 在 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, RowFilter, or RowStateFilter properties afterwards, the index is built at least two times.

关于c# - ADO.NET DataTable 约束如何影响性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1012726/

相关文章:

c# - 使用 Entity Framework 的 SqlBulkCopy(和更新)

c# - 通过 ado.net 插入 DBGeography 类型的正确方法是什么

c# - 尚未为此应用程序配置 session 或使用 IHttpContextAccessor 时请求错误

c# - UWP 从异步工作线程更新 UI

c# - C# 中的 .NET Unreal Tournament 机器人 - 无法构建 Codeplex 代码

.net - NHibernate 和 ADO.NET 连接池

c# - 垃圾收集,并处理问题。感谢一些聪明的帮助

c# - 自定义随机数生成器

c# - 闭包是如何在幕后运作的? (C#)

c# - 使用 Assert 测试异常以确保它们将被抛出的最佳方法