zoukankan      html  css  js  c++  java
  • DataTable 的DataView.RowFilter DataTable.Rows.Find 和 DataTable.Select

    http://stackoverflow.com/questions/2832304/dataview-rowfilter-vs-datatable-select-vs-datatable-rows-find
    http://www.dotblogs.com.tw/jeff-yeh/archive/2010/09/28/17972.aspx
     比较


    Moonground's post sums it up nicely:

    • DataView.RowFilter is for binding.
    • DataTable.Rows.Find is for searching by primary key only.
    • DataTable.Select is for searching by multiple columns and also for specifying an order.

    Avoid creating many DataViews in a loop and using their RowFilters to search for records. This will drastically reduce performance.

    I wanted to add that DataTable.Select can take advantage of indexes. You can create an index on a DataTable by creating a DataView and specifying a sort order:

    DataView dv = new DataView(dt, null, "Col1, Col2", DataViewRowState.CurrentRows);

    Then, when you call DataTable.Select(), it can use this index when running the query. We have used this technique to seriously improve performance in places where we use the same query many, many times. (Note that this was before Linq existed.)

    The trick is to define the sort order correctly for the Select statement. So if your query is "Col1 = 1 and Col2 = 4", then you'll want "Col1, Col2" like in the example above.

    Aseem,

    You are looking for the "best approach on finding rows in a datatable", so I first have to ask: "best" for what? I think, any technique has scenarios where it might fit better then the others.

    First, let's look at DataView.RowFilter: A DataView has some advantages in Data Binding. Its very view-oriented so it has powerful sorting, filtering or searching features, but creates some overhead and is not optimized for performance. I would choose the DataView.RowFilter for smaller recordsets and/or where you take advantage of the other features (like, a direct data binding to the view).

    Most facts about the DataView, which you can read in older posts, still apply.

    Second, you should prefer DataTable.Rows.Find over DataTable.Select if you want just a single hit. Why? DataTable.Rows.Find returns only a single row. Essentially, when you specify the primary key, a binary tree is created. This has some overhead associated with it, but tremendously speeds up the retrieval.

    DataTable.Select is slower, but can come very handy if you have multiple criteria and don't care about indexed or unindexed rows: It can find basically everything but is not optimized for performance. Essentially, DataTable.Select has to walk the entire table and compare every record to the criteria that you passed in.

    I hope you find this little overview helpful.

    I'd suggest to take a look at this article, it was helpful for me regarding performance questions. This post contains some quotes from it.

    A little UPDATE: By the way, this might seem a little out of scope of your question, but its nearly always the fastest solution to do the filtering and searching on the backend. If you want the simplicity and have an SQL Server as backend and .NET3+ on client, go for LINQ-to-SQL. Searching Linq objects is very comfortable and creates queries which are performed on server side. While LINQ-to-Objects is also a very comfortable but also slower technique. In case you didn't know already....

    best regards, thomas

     
  • 相关阅读:
    聊聊CMDB的前世今生
    我是如何走上运维岗位的?谈谈新人入职运维发展的注意事项
    如何从生命周期的视角看待应用运维体系建设?
    标准化体系建设(下):如何建立基础架构标准化及服务化体系?
    标准化体系建设(上):如何建立应用标准化体系和模型?
    微服务架构时代,运维体系建设为什么要以“应用”为核心?
    Kubernetes容器化工具Kind实践部署Kubernetes v1.18.x 版本, 发布WordPress和MySQL
    Etcd常用运维命令
    Logstash生产环境实践手册(含grok规则示例和ELKF应用场景)
    Netflix业务运维分析和总结
  • 原文地址:https://www.cnblogs.com/sandy_liao/p/2445623.html
Copyright © 2011-2022 走看看