Mus*_*ver 5 sql-server-2008 temporary-tables
我正在使用 SQL Server 2008 并且我的存储过程确实插入到 #temptable 中。我的过程已经运行了 32 小时(考虑到它必须聚合的数据量,这并不是闻所未闻),我想知道是否在存储过程本身之外,我可以查询临时表并查看临时表中的内容桌子?
目前我能想出的唯一解决方案是停止程序,我不想这样做:(
1) 停止程序并使用全局临时表 ##temptableglobal
2) 停止程序并使用实际表 temptable
编辑
按照下面链接的教程,我的第一页返回以下内容
0XE601000000100
但如果我试着跑
DBCC TRACEON (3404)
DBCC PAGE (tempdb, 1, 173, 3) With table results
Run Code Online (Sandbox Code Playgroud)
数据似乎不是纯文本格式,例如 VALUE 列中的数据显示为 0,而 #temptable 中根本不应该包含任何值为 0 的值。
Let\'s start with the query in Paul White\'s blog post, Viewing Another Session\xe2\x80\x99s Temporary Table (linked provided by @PJMahoney in a comment on the Question), as that gives us a few key pieces of information. Please note that I have added two fields to Paul\'s original query to handle the translation of the hex/binary value in the first_page
field.
SELECT T.name,\n T.[object_id],\n AU.type_desc,\n AU.first_page,\n AU.data_pages,\n P.[rows],\n CONVERT(INT,\n CONVERT(BINARY(2), REVERSE(SUBSTRING(AU.first_page, 5, 2)))) AS [FileIdINT],\n CONVERT(INT,\n CONVERT(BINARY(4), REVERSE(SUBSTRING(AU.first_page, 1, 4)))) AS [FirstPageINT]\nFROM tempdb.sys.tables T\nJOIN tempdb.sys.partitions P\n ON P.[object_id] = T.[object_id]\nJOIN tempdb.sys.system_internals_allocation_units AU\n ON (AU.type_desc = N\'IN_ROW_DATA\' AND AU.container_id = P.partition_id)\n OR (AU.type_desc = N\'ROW_OVERFLOW_DATA\' AND AU.container_id = P.partition_id)\n OR (AU.type_desc = N\'LOB_DATA\' AND AU.container_id = P.hobt_id)\nWHERE T.name LIKE N\'#temptable%\'; -- replace with your temp table name!!\n
Run Code Online (Sandbox Code Playgroud)\n\nYou should get something similar to the following:
\n\nname object_id type_desc first_page data rows File 1st\n pages Id Page\n----------------------- ----------- ----------- -------------- ----- ---- ---- ---\n#bob__...__000000000054 -1221199275 IN_ROW_DATA 0xF00100000100 1 4 1 496\n#bob__...__000000000054 -1221199275 LOB_DATA 0xF20100000100 0 4 1 498\n
Run Code Online (Sandbox Code Playgroud)\n\nBefore even bothering with looking at the datapages directly using DBCC PAGE
, take a look at the "rows" field. Is it 0? Is it much lower than expected? Does your query, if not inserting the results into a temporary table, start returning rows well before it is finished? Or does it wait until somewhere towards the end of the query processing? Depending on what the query is doing, and the execution plan, it could be that sorting and/or parallelism is preventing any results from being released until later in the processing. If this is the case (and for a query that normally takes more than a day to run, it quite likely is the case), then there might not be anything to see here, in which case the "rows" will be 0
but that won\'t imply that anything is being blocked, etc.
But, if there are rows there, then you can view them by using:
\n\nDBCC PAGE(tempdb, {file_id}, {page_id}, 3) WITH TABLERESULTS;\n
Run Code Online (Sandbox Code Playgroud)\n\nJust replace {file_id}
and {page_id}
with the values returned in the FileIdINT
and FirstPageINT
columns respectively.
The rows in a data page are in "slots". So scroll down until you see something along the lines of Slot 0 Offset 0x60 Length 37
in the ParentObject
field. There should be several rows for each "slot", depending on how many columns are in the table and if any are LOB types with enough data that won\'t fit in the row, etc. But in the Field
field you should see the column names of the table, though the LOB types sometimes don\'t display the field name here and instead only show it on the far left of the Object
field as "field name = [BLOB ...". In the VALUE
field for each "field" row, you should see the value. Just keep in mind that:
The first several rows for each "slot" (i.e. the actual row of data), are the actual bytes of the entire row. The Object
field is set to "Memory Dump @0x00..." and the Field
field is empty. The VALUE
field for each of these rows is displayed as "first_byte_offset: hex_values ASCII_characters". Each "Memory Dump" row shows 20 bytes, with the hex values arranged in 5 sets of 4 bytes each (1 byte = 00 - FF), and the 20 ASCII characters corresponding to each of those 20 bytes.
This representation is not translated so don\'t expect to see any actual data here outside of in-row string data. CHAR
/ VARCHAR
data shows up as it would anywhere else: "test" shows up as "test". NCHAR
/ NVARCHAR
is not displayed as Unicode. The bytes are parsed (not translated!) as if they are VARCHAR
(i.e. 8=bit ASCII Extended) characters. So characters that fit in the ASCII Extended range (Code Points 0 - 255) display "normally", just with a trailing "." (the "00" in the hex). Any Code Points above 255 will be incorrectly represented since a Code Point of 0x03F4
will show up as two characters -- 0x03
and 0xF4
-- instead of \xcf\xb4
.
[NULL]
VARCHAR(MAX)
, NVARCHAR(MAX)
, XML
, etc) or not. For example, the string "BoB" shows up as:\n\nVARCHAR
or NVARCHAR
: BoBVARCHAR(MAX)
: 0x426f42NVARCHAR(MAX)
: 0x42006f004200Object
),在字段中显示“RowId” Field
,并在字段中显示 PageID 规范(例如(1:501:0)
)VALUE
。更新:
\n\n上面的信息仅涵盖获取第一个数据页中的值。但是如果您想要更多页面怎么办?顶部有一行指向下一个(FileID:PageID)
(ParentObject
=“PAGE HEADER:”,Object
=“Page @0x000...”,Field
=“m_nextPage”),但是每次获取都有点麻烦,并且不\不完全允许直接进入最后一页。
幸运的是,有两种方法可以获取 PageID 列表:
\n\n未记录的DBCC IND(database_name, object_id, index_id)
功能。该object_id
值是从上面的查询返回的(我们还从中获取了 FileID 和 PageID)。例如:
DBCC IND(tempdb, -1125198933, 0);\n
Run Code Online (Sandbox Code Playgroud)如果使用 SQL Server 2012 或更高版本,您可以使用未记录的sys.dm_db_database_page_allocations(database_id, object_id, index_id, partition_number, mode)
. 的值mode
可以是LIMITED
或DETAILED
。例如:
SELECT *\nFROM sys.dm_db_database_page_allocations(DB_ID(N\'tempdb\'), -1221199275,\n NULL, NULL, \'DETAILED\')\nWHERE page_type = 1;\n
Run Code Online (Sandbox Code Playgroud)\n\nnext_page_file_id
和 的列next_page_page_id
是您所需要的。
归档时间: |
|
查看次数: |
1161 次 |
最近记录: |