![](/img/trans.png)
[英]SQL Server 2008 R2: Return result from stored procedure and insert into temp table
[英]Performance issue of stored procedure with several select and insert operations in SQL Server 2008 R2
我的應用程序和數據庫設計可以簡化為簡單的報價系統。
以下是詳細說明:
數據庫中的三個表:
表格:項目:
表:收件人:
表:訂單:
我具有以下存儲過程來處理選擇/業務邏輯:
CREATE procedere spInsert
--list of parameters
set @tmpItemId = (select itemId from item
where name = @name
and category = @category
and description = @description)
if @tmpItemId is NULL
begin
insert to item values(@name, @category, @description)
set @tmpItemId = @@IDENTITY
end
set @tmpRecipentId = (select recipentId from recipient
where firstname = @firstname
and lastname = @lastname
and address = @address and email = @email )
if @tmpRecipentId is NULL
begin
insert to recipient values(@firstname, @lastname, @address, @email)
set @tmpRecipentId = @@IDENTITY
end
insert into order values(@price, @date, @tmpItemId, @tmpRecipientId )
因此,基本上,邏輯是檢查Item
和Recipient
的存在,然后插入。
在實際數據庫中,“ Item
和“ Recipient
的列號分別為20,20。 我正在嘗試通過在文本文件中讀取大量原始信息(將項目信息和收件人信息以及訂單信息混合在一起)來進行壓力測試,然后執行spInsert
:
JdbcTemplate jt = getJdbcTemplate();
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jt);
simpleJdbcCall.withProcedureName("spInsert").execute(paraMap);
但是速度並不令人滿意。 根據我的粗略計算,每100行原始信息插入需要3-5秒。
我想知道是否還有改進的空間? 包括更改數據庫的設計?
PS,我正在考慮將自動增加的Item
和Recipient
索引ID更改為UUID,以便讓程序知道新的ID Item
和Recipient
並避免在存儲過程中進行select
。 這是可行的方法嗎?
這是執行計划:
我懷疑您需要以下性能指標:
item(name, category, description)
recipient(firstname, lastname, address, email)
您可能還應該創建這些唯一索引,從而避免出現“子查詢返回太多行”的問題。
這只是一個簡化的版本,您實際上是在比較〜20列以查找是否在每次插入Item表之前是否存在匹配項,而對於Recipient表是否存在匹配項? 是否可以重新考慮設計? 也許單獨從收件人和訂單中插入項目?
要檢查實際情況,請檢查執行計划和SET STATISTICS IO ON的輸出。 我的猜測是搜索部分是問題所在。
我認為,這些表聚集在標識列上。 在一個或兩個選擇性最強的列上創建非聚集索引,以加快搜索速度。 不要使索引太寬,否則會降低索引速度。 您將無法使其唯一,但是請使用SELECT TOP 1保護子查詢。
永遠不要使用@@ IDENTITY,而要使用scope_identity()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.