简体   繁体   English

亚音速-收集负载与查询

[英]subsonic - collection load versus query

I am setting the data source of a datagridview to a subsonic collection 我正在将datagridview的数据源设置为亚音速集合

        TcolorCollection tc = new TcolorCollection().Load();
        dataGridView1.DataSource = tc;

and I've noticed that the previous code is much (way to much) slower then the following 而且我已经注意到,前面的代码比下面的代码慢很多(很多)

        SubSonic.Query q3 = new SubSonic.Query("tcolor");
        q3.QueryType = SubSonic.QueryType.Select;
        IDataReader reader = q3.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(reader);
        dataGridView1.DataSource = dt;

I would like to use the collection class and it's connectivity, but I need a way for the .Load() to be faster. 我想使用collection类及其连接性,但是我需要一种使.Load()更快的方法。

fyi the table I am using has 8000+ records. 供我使用的表有8000多个记录。 Not small, but not huge either. 不小,但也不大。

Couple of thoughts... 几个想法...

If you have to use a DataSet/DataTable you could just do this: 如果必须使用DataSet / DataTable,则可以执行以下操作:

grid.DataSource=new Select().From("tcolor").ExecuteDataSet(); grid.DataSource = new Select()。From(“ tcolor”)。ExecuteDataSet();

Both of the things you show above use the same core bits - not sure why one is slower than the other. 上面显示的两种方法都使用相同的核心位-不知道为什么一个比另一个慢。

The TcolorCollection().Load() creates 8000 Tcolor objects and add's them to the list. TcolorCollection()。Load()创建8000个Tcolor对象,并将它们添加到列表中。 From my experience it is not a good idea to use a collection if you expect more than 1000 records. 根据我的经验,如果您希望有1000条以上的记录,那么使用集合不是一个好主意。

In this case, as Rob suggested, ExecuteDataSet() is way faster. 在这种情况下,正如Rob所建议的,ExecuteDataSet()更快。

Or you could use ExecuteTypedList() to map the query to your own poco class and decorate it with the SubSonic features you need. 或者,您可以使用ExecuteTypedList()将查询映射到您自己的poco类,并使用所需的SubSonic功能对其进行修饰。

eg 例如

public class TestPoco()
{
    public int Id {get;set;}
    public String Name {get;set;}

    public bool IsNew { get { return Id == 0 } };
    public void Delete()
    {
        // Test is an SubSonic IActiveRecord Object
        Test.Destroy(Id);
    }
}

private void MyTask()
{
    List<TestPoco> list =
          DB.Select("Id", "Name").From<Test>().ExecuteTypedList<TestPoco>();

   foreach (var item in list)
   {
       if (item.Name.Contains("acme"))
           item.Delete();
   }
}

should be pretty close in execution time to native DB access. 执行时间应该与本机数据库访问非常接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM