如何组织数据集查询以提高性能

时间:2022-10-03 03:56:24

I don't know when to add to a dataset a tableadapter or a query from toolbox. Does it make any difference?

我不知道何时向数据集添加tableadapter或来自工具箱的查询。这有什么不同吗?

I also dont know where to create instances of the adapters.

我也不知道在哪里创建适配器的实例。

  • Should I do it in the Page_Load?
  • 我应该在Page_Load中进行吗?

  • Should I just do it when I'm going to use it?
  • 我打算在使用时这样做吗?

  • Am I opening a new connection when I create a new instance?
  • 我在创建新实例时是否打开了新连接?

This doesn't seem very important, but every time I create a query a little voice on my brain asks me these questions.

这似乎不是很重要,但每次我创建一个查询时,我脑子里的一个小声音都会问我这些问题。

1 个解决方案

#1


2  

Should I just do it when I'm going to use it?

我打算在使用时这样做吗?

I would recommend that you only retrieve the data when you are going to use it. If you are not going to need it, there is no reason to waste resources by retrieving it in Page_Load. If you are going to need it multiple times throughout the page load, consider saving the query results to a private variable or collection so that the same data can be reused multiple times throughout the page load.

我建议您只在使用它时检索数据。如果您不需要它,则没有理由通过在Page_Load中检索它来浪费资源。如果在整个页面加载过程中多次需要它,请考虑将查询结果保存到私有变量或集合中,以便在整个页面加载过程中可以多次重用相同的数据。

Am I opening a new connection when I create a new instance?

我在创建新实例时是否打开了新连接?

Asp.net handles connection pooling, and opens and closes connections in an efficient way. You shouldn't have to worry about this.

Asp.net处理连接池,并以有效的方式打开和关闭连接。你不应该担心这个。

One other thing to consider from a performance perspective is to avoid using Datasets and TableAdapters. In many cases, they add extra overhead into data retrieval that does not exist when using Linq to Sql, Stored Procedures or DataReaders.

从性能角度考虑的另一件事是避免使用数据集和TableAdapter。在许多情况下,它们会在使用Linq to Sql,Stored Procedures或DataReader时不存在的数据检索中增加额外开销。

#1


2  

Should I just do it when I'm going to use it?

我打算在使用时这样做吗?

I would recommend that you only retrieve the data when you are going to use it. If you are not going to need it, there is no reason to waste resources by retrieving it in Page_Load. If you are going to need it multiple times throughout the page load, consider saving the query results to a private variable or collection so that the same data can be reused multiple times throughout the page load.

我建议您只在使用它时检索数据。如果您不需要它,则没有理由通过在Page_Load中检索它来浪费资源。如果在整个页面加载过程中多次需要它,请考虑将查询结果保存到私有变量或集合中,以便在整个页面加载过程中可以多次重用相同的数据。

Am I opening a new connection when I create a new instance?

我在创建新实例时是否打开了新连接?

Asp.net handles connection pooling, and opens and closes connections in an efficient way. You shouldn't have to worry about this.

Asp.net处理连接池,并以有效的方式打开和关闭连接。你不应该担心这个。

One other thing to consider from a performance perspective is to avoid using Datasets and TableAdapters. In many cases, they add extra overhead into data retrieval that does not exist when using Linq to Sql, Stored Procedures or DataReaders.

从性能角度考虑的另一件事是避免使用数据集和TableAdapter。在许多情况下,它们会在使用Linq to Sql,Stored Procedures或DataReader时不存在的数据检索中增加额外开销。