在经典 ASP 中,打开和关闭多个连接是否会对性能产生重大影响?

发布于 2024-07-23 12:50:42 字数 525 浏览 6 评论 0原文

我有一个类似于下面的代码片段,我想将其重构为两个不同的函数,每个函数都有自己的连接(为了更好的维护):

Dim Conn, Sql, RS

Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open ConnString

Sql = SELECT * FROM CLIENTS

Set RS = Conn.Execute(sql)
//'Do something with Clients

Set RS = Nothing

Sql = SELECT * FROM DEALERS

Set RS = Conn.Execute(sql)
//'Do something with Dealers

Set RS = Nothing

Conn.Close
Set Conn = Nothing

将有两个函数(例如 GetClients 和 GetDealers),每个函数打开和关闭自己的连接都有一个主要性能受到影响,相反仅打开和关闭单个连接,如上所示? 如果是这样,您将如何重构代码?

I have a code snippet similar to the one below that I would like to refactor into two different functions each with their own connection (for better maintenance):

Dim Conn, Sql, RS

Set Conn = Server.CreateObject("ADODB.Connection")
    Conn.Open ConnString

Sql = SELECT * FROM CLIENTS

Set RS = Conn.Execute(sql)
//'Do something with Clients

Set RS = Nothing

Sql = SELECT * FROM DEALERS

Set RS = Conn.Execute(sql)
//'Do something with Dealers

Set RS = Nothing

Conn.Close
Set Conn = Nothing

Will having two functions (e.g. GetClients and GetDealers) each opening and closing their own connections have a major performance hit, opposite only opening and closing a single connection as illustrated above? If so, how would you refactor the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猥︴琐丶欲为 2024-07-30 12:50:42

如果我没有记错的话,连接是池化的,因此打开和关闭每个函数的连接几乎不需要任何成本。

If I am not mistaken the connections are pooled so it should cost next to nothing to have each function open and close the connections.

微暖i 2024-07-30 12:50:42
  1. 不要使用 select * 但指定您需要的列。
  2. 使用 getrow
  3. 指定您想要从数据库中获得的内容(使用联接和 where 子句)。

当你完成所有这些后,你的代码将是最佳的。

  1. Don't use select * but specify columns you need.
  2. Use getrows.
  3. Specify what you want to have from the database (Use joins and where clause).

When you do all this your code will be optimal.

我还不会笑 2024-07-30 12:50:42

在这种情况下,您不应该遇到任何性能问题,但如果数据源相同,则最好使用单个连接。

更好的方法是将单个打开的连接对象传递到每个 GetClients 和 GetDealers 函数中。

You shouldn't have any performance problems in this case however it is good practice to use a single connection if the data source is the same.

A better way of doing it would be to pass a single opened connection object into each of the GetClients and GetDealers functions.

黑白记忆 2024-07-30 12:50:42

连接会自动池化,因此成本非常小。

然而它并不为零。 每次从池中拉出连接并重新打开它时,都会向 SQL Server 发送 sp_reset_connection 命令。 这是非常便宜的,所以我不会担心。

旁注:单个 ADODB.Connection 可能代表多个数据库连接。 如果您尝试使用同一个记录集打开第二个记录集,同时仍在读取第一个记录集,则它有可能在后台创建一个新记录集。 再次强调,这并不是真正的问题,只是您应该知道的事情。

Connections are pooled automatically, so the cost is very small.

It isn't zero however. Every time you pull a connection from the pool and reopen it, you send a sp_reset_connection command to SQL Server. This is incredibly cheap, so I wouldn't worry about it.

Side note: A single ADODB.Connection may represent multiple database connections. If you try to use the same one to open a second recordset while still reading from the first one, there is a chance it will create a new one in the background. Again, this isn't a real concern just something you should know.

牵你手 2024-07-30 12:50:42

连接池可用于经典 ASP。 我将重构该代码以使用接受连接字符串的方法,并在该方法中尽快打开和关闭连接。

至少与连接使用情况一样令人担忧的是,您没有使用断开连接的记录集(ADO.NET 默认实现)。 断开连接的记录集允许您在完成查询后立即关闭连接并将其返回到池中,而不必等到迭代记录集。

以下是在 JScript 中执行此操作的方法; 移植到 VBScript 应该很简单:

var sql = "select * from MyTable";
var cn = new ActiveXObject("ADODB.Connection");
var rs = new ActiveXObject("ADODB.Recordset");
var nothing = rs.ActiveConnection;
cn.Open(connectString);
rs.CursorLocation = 3; //adUseClient
rs.Open(sql, cn, 3, 1);
rs.ActiveConnection = nothing;
cn.Close();
//now do something with disconnected rs

Connection pooling is available with classic ASP. I would refactor that code to use a method that accepts a connection string, and open and close the connection as quickly as possible within that method.

At least as concerning as the connection usage is the fact that you are not using disconnected recordsets (which ADO.NET implements by default). Disconnected recordsets let you close the connection and give it back to the pool as soon as you have done your query, rather than having to wait until you have iterated through the recordset.

Here is how you would do that in JScript; it should be simple to port to VBScript:

var sql = "select * from MyTable";
var cn = new ActiveXObject("ADODB.Connection");
var rs = new ActiveXObject("ADODB.Recordset");
var nothing = rs.ActiveConnection;
cn.Open(connectString);
rs.CursorLocation = 3; //adUseClient
rs.Open(sql, cn, 3, 1);
rs.ActiveConnection = nothing;
cn.Close();
//now do something with disconnected rs
天冷不及心凉 2024-07-30 12:50:42

连接应该被集中

connections should be pooled

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文