久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

重用表變量?

Reuse a table variable?(重用表變量?)
本文介紹了重用表變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在某個日期時間段(即 05/01/2012 到 05/31/2012)對數據庫運行復雜查詢列表.然后我需要為 06/01/2012 到 06/30/2012 運行相同的查詢.然后加入結果用于報告目的.

I'm running a list of complex query on a database for certain datetime period (i.e. 05/01/2012 to 05/31/2012). Then I need run the same queries for 06/01/2012 to 06/30/2012. then join the results for report purpose.

在查詢中,我使用了幾個表變量來保存臨時數據.因為數據庫很大,表變量也很大.有沒有辦法重用這些表變量?

in the queries, I used several table variables to hold temporary data. Because the database is large, the table variable size is large too. Is there a way that I can reuse these table variables?

DROP,截斷將不起作用.我是否必須從@table 中刪除所有數據?如果是這樣,它會很慢嗎?因為@table 有大量數據,我是否必須對它進行批量刪除?

DROP, Truncate will not work. Do I have to delete all data from @table? If so, will it be slow? DO I have to do batch delete for the @table since it has lots of data?

順便說一句,我必須將所有查詢放在一個 SP 文件中,由于系統的設計方式,無法調用函數或其他 SP.

BTW, I have to put all queries in one SP file, can not call function or other SP because of the way the system was designed.

謝謝

==============================

=============================

查詢中沒有循環.它的工作原理如下:

there is no loop in the queries. it works like:

選擇.....選擇.....更新.....加入......

select ..... select ..... update ..... join ......

對日期 05/01/2012 至 05/31/2012 執行一組查詢.然后需要對 06/01/2012 到 06/30/2012 的同一組查詢.

do a set of queries for date 05/01/2012 to 05/31/2012. then need to the same set of queries for 06/01/2012 to 06/30/2012.

在查詢中,里面有很多邏輯,所以我們不能將這兩個合并為一組查詢.由于系統設計,我們不能調用函數或SP進行查詢.必須先做第一組查詢,然后按順序做第二組查詢.

in the queries, there are lots of logical inside, so we can not combine these 2 into one set of queries. because of system designed, we can not call function or SP for the query. have to do the 1st set of query, then the 2nd set of query in sequence.

問題是數據太多,@table太大了.如果我們可以重用@table,就可以解決問題.

the problem is there is too much data, the @table is too large. if we can reuse @table, it will solve the problem.

謝謝

================================

===============================

是的,現在,相同的代碼,在 2 個不同的日期時間間隔內重復兩次.但是,在代碼中,它有一些邏輯內部,根據日期時間之間的差異進行不同的處理.抱歉,我無法發布實際代碼.但是,這個過程就像一個以不同日期時間段為參數的SP.

yes, right now, the same codes, repeat twice for 2 different datetime interval. however, in the code, it has some logical inside, different process based on the difference between the datetime. Sorry, I can not post the actual code. But, the process is like a SP with different datetime period as parameters.

但是,在這種情況下我不能使用 SP/function,因此必須對相同的代碼進行兩次硬編碼.理想情況下,每次重復代碼時都需要使用不同的@table(現在,我需要重復 3 次),但是由于數據大小,如果我重復 3 次,@table 就太大了(每個都需要多個 @table做邏輯部分).

However, I can not use SP/function in this case, so have to hard code the same code twice. ideally, will need use different @table for each time I repeat the code (right now, I need repeat 3 times), but because of the data size, @table is too large if I repeat 3 times (needs multiple @table in each to do the logical part).

也許我最好使用臨時表?所以我可以在開始新的重復"時刪除它?

maybe I had better use temporary table? so I can drop it when start a new 'repeat'?

謝謝

推薦答案

表變量未記錄在當前數據庫中,并且不受事務的約束.為什么你認為截斷或刪除會比刪除快?你試過這個嗎?

A table variable isn't logged in the current database, and isn't subject to transactions. Why do you think a truncate or drop would be any faster than a delete? Have you tried this?

DECLARE @f TABLE(id INT);

INSERT @f SELECT 1;

BEGIN TRANSACTION;
DELETE @f WHERE id = 1;
ROLLBACK TRANSACTION;

SELECT id FROM @f;

沒有結果.現在,如果刪除完全記錄在當前數據庫中(這使得 DELETE 比普通用戶表的 TRUNCATE 慢),您會期望 DELETE 已回滾,并且 SELECT 應該已返回數據.但是不,刪除不是事務的一部分.那么,您可以從邏輯上得出結論,如果允許后者,DELETETRUNCATE 將非常相似,如果不完全相同.

No results. Now, if the delete were fully logged in the current database (which is what makes DELETE slower than TRUNCATE for a normal user table), you would expect the DELETE to have been rolled back, and the SELECT should have returned data. But no, the delete is not part of the transaction. You could logically conclude, then, that DELETE and TRUNCATE would be quite similar, if not identical, were the latter allowed.

如果必須使用表變量,只需使用刪除.如果您發現它很慢,那可能不是因為刪除,而可能是因為您在循環中重新使用表變量,而不是使用基于集合的操作.但是,當然,您比我們中的任何人都處于更好的位置,以測試如果您使用兩個不同的 @table 變量與重新使用單個表變量并在兩者之間發出刪除,您的代碼會慢多少.但我仍然認為你的整個過程需要重新調查,因為它在很多層面上對我來說都不是最佳選擇.

If you must use a table variable, just use a delete. If you find it slow, it's probably not because of the delete, it's probably because you're re-using a table variable in a loop, rather than using a set-based operation. But of course you are in a better position than any of us to test how much slower your code would be if you use two different @table variables vs. re-using a single table variable and issuing a delete in between. But I still think your whole process needs to be re-investigated because it sounds sub-optimal to me on many levels.

這篇關于重用表變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 综合婷婷 | www.97zyz.com| 91精品国产综合久久精品图片 | 亚洲精品国产电影 | 国产精品1区2区3区 中文字幕一区二区三区四区 | 五月综合色啪 | 国内精品久久久久久影视8 最新黄色在线观看 | 国产91综合一区在线观看 | 亚洲高清视频一区二区 | 成人水多啪啪片 | 免费看爱爱视频 | 国产原创在线观看 | 日韩av在线一区二区 | 亚洲国产一区在线 | 日韩欧美三区 | 精品国产一区二区三区在线观看 | 国产精品美女视频 | 日韩高清在线 | 九九av| 91久久精品国产91久久性色tv | 国产成人综合一区二区三区 | 国产精品成人69xxx免费视频 | 欧美美女爱爱视频 | 在线看亚洲 | 亚洲精品一区在线观看 | 九九九视频 | 久久国产精品一区二区 | av黄色在线播放 | 黄色片a级 | 国产有码 | 久久大全 | 成人在线视频网站 | 91精品国产91久久久久久 | 99国内精品久久久久久久 | 日本久久精 | av中文网 | 日韩精品在线观看一区二区三区 | 国产精品视频一二三区 | 国产1区| 欧美国产中文字幕 | 国产精品18hdxxxⅹ在线 |