Listing Objects in an Oracle Tablespace
SQL lists every segment within a specific tablespace in an Oracle database
A detailed explanation of Oracle Database SQL code used to list objects within a tablespace, including purpose, breakdown, key points, and insights.
Sample SQL Command
1set pages 999
2col owner format a15
3col segment_name format a40
4col segment_type format a20
5select owner
6, segment_name
7, segment_type
8from dba_segments
9where lower(tablespace_name) like lower('%&tablespace%')
10order by owner, segment_name
11/
Sample Oracle Output
1OWNER SEGMENT_NAME SEGMENT_TYPE
2--------------- ---------------------------------------- --------------------
3LARRY BIN$Q+k4qsgBLNngRAAQg5ZRWg==$0 INDEX
4LARRY BIN$Q+k4qsgCLNngRAAQg5ZRWg==$0 TABLE
5LARRY BIN$Q+k4qsgELNngRAAQg5ZRWg==$0 INDEX
6LARRY BIN$Q+k4qsgFLNngRAAQg5ZRWg==$0 TABLE
7LARRY BOX TABLE
8LARRY BOX_PK INDEX
9LARRY CD TABLE
10LARRY CD_PK INDEX
11LARRY LOCATION TABLE
12LARRY LOCATION_PK INDEX
13LARRY LOTS_OF_ROWS TABLE
14LARRY POO TABLE
15LARRY SERVERS TABLE
16LARRY SYS_C0023466 INDEX
17LARRY SYS_C0023467 INDEX
18LARRY SYS_C0027234 INDEX
19LARRY TEST_TABLE TABLE
20LARRY TRACK TABLE
21LARRY TRACK_PK INDEX
22
2319 rows selected.
Purpose:
- To identify and display every segment (tables, indexes, partitions and LOBs that have storage allocated) residing in a chosen tablespace.
Breakdown:
set pages 999: This setting sets the number of lines on each page of output to 999, so column headings repeat less often if the tablespace holds a large number of objects.col owner format a15,col segment_name format a40**,col segment_type format a20**: These lines define column formatting in the output. They set specific widths for the "owner", "segment_name", and "segment_type" columns to improve readability.select owner, segment_name, segment_type: This clause selects three specific columns from thedba_segmentssystem view:owner: The schema (user) who owns the object.segment_name: The actual name of the object.segment_type: The type of segment (TABLE, INDEX, TABLE PARTITION, INDEX PARTITION, LOBSEGMENT, etc.).
from dba_segments: This specifies the data source for the query.dba_segmentsholds information about all database segments, including their type, owner, and tablespace association.where lower(tablespace_name) like lower('%&tablespace%'): This clause filters the results based on the tablespace name.lower(tablespace_name): Converts the tablespace name to lowercase for case-insensitive comparison.like lower('%&tablespace%'): Uses a wildcard comparison with%to accept any tablespace name passed as the&tablespacevariable.
order by owner, segment_name: This clause sorts the output first by owner and then by segment name within each owner.
Key Points:
- This code relies on the
dba_segmentssystem view, which requires appropriate privileges to access (typically DBA or SYSDBA). - The
likeclause with wildcards allows for listing objects in any tablespace whose name matches the provided part. - The code filters for all object types (
segment_type). If you only need specific types (e.g., tables), modify thewhereclause accordingly. - Consider additional filtering using other columns from
dba_segmentsfor more granular results. - Empty tables created with deferred segment creation (the default,
DEFERRED_SEGMENT_CREATION = TRUE) have no segment until the first row is inserted, so they do not appear. Querydba_tablesordba_indexesbytablespace_nameto catch them.
Insights and Explanations:
set pagesonly controls how many lines SQL*Plus prints on each page, and so how often it repeats column headings; it has no effect on query cost. Useset pages 0to suppress headings entirely when spooling to a file.- Because the filter uses
'%name%', a partial name can match more than one tablespace (for example, USERS also matches USERS2). Enter the full tablespace name when you want only one. - The code provides a basic listing of segments. To show size, add
bytesorblocksfromdba_segments.dba_segmentshas no creation-date column; for the object creation date, join todba_objects(columncreated) on owner andobject_name = segment_name.
The query lists every segment within a specified tablespace. Empty tables with deferred segment creation are the main gap, so adapt it to your needs.
Posts in this series
- Monitoring Temporary Tablespace Usage in Oracle Database
- Adjusting Segment Growth with MAXEXTENTS in Oracle Database
- Oracle Segments Nearing Max Extents via dba_segments
- Setting a Default Temporary Tablespace in Oracle Database
- Identifying User Tablespaces and Space Usage in Oracle
- Listing Objects in an Oracle Tablespace
- Oracle Tablespaces Needing More Space: SQL Script
- Exploring Datafiles within Oracle Tablespaces
- Understanding Tablespace Usage in Oracle Database
- Understanding User Quotas in Oracle Database
- Creating a Temporary Tablespace in Oracle Database