{
  "date": "2020-12-15T08:15:18",
  "slug": "efficiently-query-dba_extents-for-file_id-block_id",
  "link": "https://www.dbi-services.com/blog/efficiently-query-dba_extents-for-file_id-block_id/",
  "title": {
    "rendered": "Efficiently query DBA_EXTENTS for FILE_ID / BLOCK_ID"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\n<i>This was initially posted to CERN Database blog on Thursday, 27 September 2018 where it seems to be lost. Here is a copy thanks to <a href=\"https://web.archive.org/web/20191229201611/https://db-blog.web.cern.ch/blog/franck-pachot/2018-09-efficiently-query-dbaextents-fileid-blockid\" target=\"_blank\" rel=\"noopener noreferrer\">web.archive.org</a></i></p>\n<p>Did you ever try to query DBA_EXTENTS on a very large database with LMT tablespaces? I had to, in the past, in order to find which segment a corrupt block belonged to. The information about extent allocation is stored in the datafiles headers, visible though X$KTFBUE, and queries on it can be very expensive. In addition to that, the optimizer tends to start with the segments and get to this X$KTFBUE for each of them. At this time, I had quickly created a view on the internal dictionary tables, forcing to start by X$KTFBUE with materialized CTE, to replace DBA_EXTENTS. I published this on <a href=\"https://www.dba-village.com/village/dvp_scripts.ScriptDetails?ScriptIdA=2543\" target=\"_blank\" rel=\"noopener noreferrer\">dba-village</a> in 2006.</p>\n<p>I recently wanted to know the segment/extend for a hot block, identified by its file_id and block_id on a 900TB database with 7000 datafiles and 90000 extents, so I went back to this old query and I got my result in 1 second. The idea is to be sure that we start with the file (X$KCCFE) and then get to the extent allocation (X$KTFBUE) before going to the segments:</p>\n<p><a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CaptureExtentMap.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46158\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CaptureExtentMap.png\" alt=\"\" width=\"1262\" height=\"730\" /></a></p>\n<p>So here is the query:</p>\n<pre><code>\ncolumn owner format a6\ncolumn segment_type format a20\ncolumn segment_name format a15\ncolumn partition_name format a15\nset linesize 200\nset timing on time on echo on autotrace on stat\nWITH\n l AS ( /* LMT extents indexed on ktfbuesegtsn,ktfbuesegfno,ktfbuesegbno */\n  SELECT ktfbuesegtsn segtsn,ktfbuesegfno segrfn,ktfbuesegbno segbid, ktfbuefno extrfn,\n         ktfbuebno fstbid,ktfbuebno + ktfbueblks - 1 lstbid,ktfbueblks extblks,ktfbueextno extno\n  FROM sys.x$ktfbue\n ),\n d AS ( /* DMT extents ts#, segfile#, segblock# */\n  SELECT ts# segtsn,segfile# segrfn,segblock# segbid, file# extrfn,\n         block# fstbid,block# + length - 1 lstbid,length extblks, ext# extno\n  FROM sys.uet$\n ),\n s AS ( /* segment information for the tablespace that contains afn file */\n  SELECT /*+ materialized */\n  f1.fenum afn,f1.ferfn rfn,s.ts# segtsn,s.FILE# segrfn,s.BLOCK# segbid ,s.TYPE# segtype,f2.fenum segafn,t.name tsname,blocksize\n  FROM sys.seg$ s, sys.ts$ t, sys.x$kccfe f1,sys.x$kccfe f2 \n  WHERE s.ts#=t.ts# AND t.ts#=f1.fetsn AND s.FILE#=f2.ferfn AND s.ts#=f2.fetsn\n ),\n m AS ( /* extent mapping for the tablespace that contains afn file */\nSELECT /*+ use_nl(e) ordered */\n s.afn,s.segtsn,s.segrfn,s.segbid,extrfn,fstbid,lstbid,extblks,extno, segtype,s.rfn, tsname,blocksize\n FROM s,l e\n WHERE e.segtsn=s.segtsn AND e.segrfn=s.segrfn AND e.segbid=s.segbid\n UNION ALL\n SELECT /*+ use_nl(e) ordered */ \n s.afn,s.segtsn,s.segrfn,s.segbid,extrfn,fstbid,lstbid,extblks,extno, segtype,s.rfn, tsname,blocksize\n FROM s,d e\n  WHERE e.segtsn=s.segtsn AND e.segrfn=s.segrfn AND e.segbid=s.segbid\n UNION ALL\n SELECT /*+ use_nl(e) use_nl(t) ordered */\n f.fenum afn,null segtsn,null segrfn,null segbid,f.ferfn extrfn,e.ktfbfebno fstbid,e.ktfbfebno+e.ktfbfeblks-1 lstbid,e.ktfbfeblks extblks,null extno, null segtype,f.ferfn rfn,name tsname,blocksize\n FROM sys.x$kccfe f,sys.x$ktfbfe e,sys.ts$ t\n WHERE t.ts#=f.fetsn and e.ktfbfetsn=f.fetsn and e.ktfbfefno=f.ferfn\n UNION ALL\n SELECT /*+ use_nl(e) use_nl(t) ordered */\n f.fenum afn,null segtsn,null segrfn,null segbid,f.ferfn extrfn,e.block# fstbid,e.block#+e.length-1 lstbid,e.length extblks,null extno, null segtype,f.ferfn rfn,name tsname,blocksize\n FROM sys.x$kccfe f,sys.fet$ e,sys.ts$ t\n WHERE t.ts#=f.fetsn and e.ts#=f.fetsn and e.file#=f.ferfn\n ),\n o AS (\n  SELECT s.tablespace_id segtsn,s.relative_fno segrfn,s.header_block   segbid,s.segment_type,s.owner,s.segment_name,s.partition_name\n  FROM SYS_DBA_SEGS s\n ),\ndatafile_map as (\nSELECT\n afn file_id,fstbid block_id,extblks blocks,nvl(segment_type,decode(segtype,null,'free space','type='||segtype)) segment_type,\n owner,segment_name,partition_name,extno extent_id,extblks*blocksize bytes,\n tsname tablespace_name,rfn relative_fno,m.segtsn,m.segrfn,m.segbid\n FROM m,o WHERE extrfn=rfn and m.segtsn=o.segtsn(+) AND m.segrfn=o.segrfn(+) AND m.segbid=o.segbid(+)\nUNION ALL\nSELECT\n file_id+(select to_number(value) from v$parameter WHERE name='db_files') file_id,\n 1 block_id,blocks,'tempfile' segment_type,\n '' owner,file_name segment_name,'' partition_name,0 extent_id,bytes,\n  tablespace_name,relative_fno,0 segtsn,0 segrfn,0 segbid\n FROM dba_temp_files\n)\nselect * from datafile_map where file_id=5495 and 11970455 between block_id and block_id+blocks\n</code></pre>\n<p>And here is the result, with execution statistics:</p>\n<pre><code>\n\n   FILE_ID   BLOCK_ID     BLOCKS SEGMENT_TYPE         OWNER  SEGMENT_NAME    PARTITION_NAME    EXTENT_ID      BYTES TABLESPACE_NAME      RELATIVE_FNO     SEGTSN     SEGRFN    SEGBID\n---------- ---------- ---------- -------------------- ------ --------------- ---------------- ---------- ---------- -------------------- ------------ ---------- ---------- ----------\n      5495   11964544            8192 INDEX PARTITION LHCLOG DN_PK           PART_DN_20161022 1342         67108864 LOG_DATA_20161022            1024       6364       1024        162\n\nElapsed: 00:00:01.25\n\nStatistics\n----------------------------------------------------------\n        103  recursive calls\n       1071  db block gets\n      21685  consistent gets\n        782  physical reads\n        840  redo size\n       1548  bytes sent via SQL*Net to client\n        520  bytes received via SQL*Net from client\n          2  SQL*Net roundtrips to/from client\n          0  sorts (memory)\n          0  sorts (disk)\n          1  rows processed\n</code></pre>\n<p>Knowing the segment from the block address is important in performance tuning, when we get the file_id/block_id from wait event parameters. It is even more important when a block corrution is detected ans having a fast query may help.</p>\n",
    "protected": false
  }
}
