提高運行時間超過3分鍾的asp腳本性能

[英]Improve asp script performance that takes 3+ minutes to run


I use an SQL statement to remove records that exist on another database but this takes a very long time.

我使用SQL語句刪除另一個數據庫中存在的記錄,但這需要很長時間。

Is there any other alternative to the code below that can be faster? Database is Access.

下面的代碼還有其他替代方案可以更快嗎?數據庫是Access。

email_DB.mdb is from where I want to remove the email addresses that exist on the other database (table Newsletter_Subscribers) customers.mdb is the other database (table Customers)

email_DB.mdb是我想從中刪除其他數據庫中存在的電子郵件地址(表Newsletter_Subscribers)的customers.mdb是其他數據庫(表Customers)

SQLRemoveDupes = "DELETE FROM Newsletter_Subscribers WHERE EXISTS (select * from [" & strDBPath & "Customers].Customers " _
      & "where Subscriber_Email = Email or Subscriber_Email = EmailO)"

NewsletterConn = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & strDBPath & "email_DB.mdb"

Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = NewsletterConn
MM_editCmd.CommandText = SQLRemoveDupes
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
Set MM_editCmd = Nothing

EDIT: Tried the SQL below from one of the answers but I keep getting an error when running it:

編輯:從其中一個答案嘗試下面的SQL但運行時我一直收到錯誤:

SQL: DELETE FROM Newsletter_Subscribers WHERE CustID IN (select CustID from [" & strDBPath & "Customers].Customers where Subscriber_Email = Email or Subscriber_Email = EmailO)

SQL:DELETE FROM Newsletter_Subscribers WHERE CustID IN(從[“&strDBPath&”Customers]中選擇CustID。訂閱者,其中Subscriber_Email = Email或Subscriber_Email = EmailO)

I get a "Too few parameters. Expected 1." error message on the Execute line.

我得到“參數太少。預計1.”執行行上的錯誤消息。

5 个解决方案

#1


0  

I would use WHERE Subscriber_Email IN (Email, Email0) as the WHERE clause

我會使用WHERE Subscriber_Email IN(Email,Email0)作為WHERE子句

SQLRemoveDupes = "DELETE FROM Newsletter_Subscribers WHERE EXISTS " & _ 
(select * from [" & strDBPath & "Customers].Customers where Subscriber_Email IN (Email, EmailO)"

I have found from experience that using an OR predicate in a WHERE clause can be detrimental in terms of performance because SQL will have to evaluate each clause separately, and it might decide to ignore indexes and use a table scan. Sometime it can be better to split it into two separate statements. (I have to admit I am thinking in terms of SQL Server here, but the same may apply to Access)

我從經驗中發現,在WHERE子句中使用OR謂詞在性能方面可能是有害的,因為SQL必須單獨評估每個子句,並且它可能決定忽略索引並使用表掃描。有時將它分成兩個單獨的語句會更好。 (我不得不承認我在這里考慮SQL Server,但同樣適用於Access)

"DELETE FROM Newsletter_Subscribers WHERE EXISTS " & _ 
    (select * from [" & strDBPath & "Customers].Customers where Subscriber_Email = Email)"

"DELETE FROM Newsletter_Subscribers WHERE EXISTS " & _ 
    (select * from [" & strDBPath & "Customers].Customers where Subscriber_Email = EmailO)"

#2


0  

Assuming there's an ID-column present in the Customers table, the following change in SQL should give better performance:

假設Customers表中存在ID列,SQL中的以下更改應該會提供更好的性能:

"DELETE FROM Newsletter_Subscribers WHERE ID IN (select ID from [" & strDBPath & "Customers].Customers where Subscriber_Email = Email or Subscriber_Email = EmailO)"

“DELETE FROM Newsletter_Subscribers WHERE ID IN(從[”&strDBPath&“Customers]中選擇ID。訂閱者,其中Subscriber_Email = Email或Subscriber_Email = EmailO)”

PS. The ideal solution (judging from the column names) would be to redesign the tables and code logic of inserting emails in the first place. DS

PS。理想的解決方案(從列名稱判斷)將是重新設計表格和首先插入電子郵件的代碼邏輯。 DS

#3


0  

Try adding an Access Querydef and calling that.

嘗試添加Access Querydef並調用它。

#4


0  

It sounds like you do not have an index on the subscriber_enail field. This forces a table scan ( or several). Add an index on this field and you should see significant improvement.

聽起來您在subscriber_enail字段上沒有索引。這會強制進行表掃描(或幾次)。在此字段上添加索引,您應該看到顯着的改進。

I would have coded the query

我會編碼查詢

DELETE FROM Newsletter_Subscribers where (Subscriber_Email = Email or Subscriber_Email = EMail0)

#5


0  

I would try splitting this into two separate statements with separate database connections.

我會嘗試將其拆分為兩個單獨的語句,並使用單獨的數據庫連接。

First, fetch the list of email addresses or IDs in the first database (as a string).

首先,獲取第一個數據庫中的電子郵件地址或ID列表(作為字符串)。

Second, construct a WHERE NOT IN statement and run it on the second database.

其次,構造一個WHERE NOT IN語句並在第二個數據庫上運行它。

I would imagine this would be much faster as it does not have to interoperate between the two databases. The only possible issue would be if there are thousands of records in the first database and you hit the maximum length of a sql query string (whatever that is).

我想這會快得多,因為它不需要在兩個數據庫之間進行互操作。唯一可能的問題是,如果第一個數據庫中有數千條記錄,並且您達到了sql查詢字符串的最大長度(無論是什么)。

Here are some useful functions for this:

以下是一些有用的功能:

function GetDelimitedRecordString(sql, recordDelimiter)
    dim rs, str
    set rs = db.execute(sql)
    if rs.eof then
        str = ""
  else
        str = rs.GetString(,,,recordDelimiter)
      str = mid(str, 1, len(str)-len(recordDelimiter))
    end if
    rs.close
    set rs = nothing
    GetDelimitedRecordString = str
end function

function FmtSqlList(commaDelimitedStringOrArray)
    ' converts a string of the format "red, yellow, blue" to "'red', 'yellow', 'blue'"
    ' useful for taking input from an html form post (eg a multi-select box or checkbox group) and using it in a SQL WHERE IN clause
    ' prevents sql injection
    dim result:result = ""
    dim arr, str
    if isArray(commaDelimitedStringOrArray) then
        arr = commaDelimitedStringOrArray
    else
        arr = split(commaDelimitedStringOrArray, ",")
    end if
    for each str in arr
        if result<>"" then result = result & ", "
        result = result & "'" & trim(replace(str&"","'","''")) & "'"
    next
    FmtSqlList = result
end function

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/11/28/251bc7ed592df969f07399d3cc96403d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com