提高數據庫性能的兩種方式

    數據庫訪問性能的兩種方式:使用存儲過程和緩衝池POOLING。

使用存儲過程

在ASP.NET中,你可以使用兩種方式執行SQL語句直接在頁面執行代碼,或者封裝SQL語句然後再執行。使用存儲過程可以提高應用程序的性能和可維護性;封裝多個SQL語句,然後成組執行,比如,你可以創建一個存儲過程包含多個SQL UPDATE語句,一次執行修改多個記錄;TSQL支持參數、條件、循環和函數,使用這些功能,你能在一個存儲過程中創建非常複雜的小程序;存儲過程能將應用程序和數據庫的實現分離,如果數據庫表改變了,你可以只改變存儲過程而無須改變你的ASP.NET頁面。

使用緩衝池

To take advantage of connection pooling, you must be careful to do two things in your ASP.NET pages. First, you must be careful to use the same exact connection string whenever you open a database connection. Only those connections opened with the same connection string can be placed in the same connection pool.

Realize that even very small differences in the connection string can thwart connection pooling. Connections are pooled only when they are opened with connection strings that exactly match character by character. For this reason, it is wise to create your connection string in one place and use the same connection string within all your ASP.NET pages. For example, you can place your connection string in the web.config file and retrieve it from this file whenever you need to open a connection. Another option is to place the connection string in Application state within the Global.asax file.

When using SQL connection pooling, you can place additional options in a connection string to modify how connection pooling works. For example, you can specify the minimum and maximum size of the connection pool or even completely disable connection pooling.

Here's a list of the connection pooling options that you can add to the SQL Server connection string:

  • Connection Lifetime— Destroys a connection after a certain number of seconds. The default value is 0, which indicates that connections should never be destroyed.

  • Connection Reset— Indicates whether connections should be reset when they are returned to the pool. The default value is true.

  • Enlist— Indicates whether a connection should be automatically enlisted in the current transaction context. The default value is true.

  • Max Pool Size— The maximum number of connections allowed in a single connection pool. The default value is 100.

  • Min Pool Size— The minimum number of connections allowed in a single connection pool. The default value is 0.

  • Pooling— Determines whether connection pooling is enabled or disabled. The default value is true.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章