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.