Loading...

데이터베이스

모든 테이블의 행(row) 갯수를 각각 가져오는 방법

페이지 정보

작성자 DocsArchives
작성일 2025.09.10 12:50
분류 oracle
839 조회

본문

1. sqlplus 의 spool 을 이용한 방법
 

Set heading off
Set feedback off
Set pagesize 0
Set termout off
Set trimout on
Set trimspool on
Set recsep off
Set linesize 100
Column d noprint new_value date_
Column u noprint new_value user_
Spool tmp
Select 'Select '''||table_name||' , ''||count(*) from '||table_name||';',
    to_char(sysdate, 'YYYYMMDDHH24MISS') d, user u
from user_tables
order by table_name
<span>
Spool off
Spool count_&user_._&date_
@tmp.LST
Spool off


위의 내용을 rowcount.sql 이라는 텍스트 파일로 저장하고 sqlplus 에 접속해서 이 파일을 @rowcount.sql 로 실행합니다.
그러면, sqlplus 를 실행시켰던 폴더 위치에 count_<user name>_<system_date>.LST 라는 파일이 생성되어 있습니다.


2. stored procedure 에서 for loop 문을 이용
 

set serveroutput on
declare
    v_cnt number;
begin
    for v_rec in (select table_name from all_tables a where owner='XXX') loop
  execute immediate 'select count(*) from XXX.' || v_rec.table_name into v_cnt;
  dbms_output.put_line('Table XXX.' || v_rec.table_name || ' has ' || v_cnt || ' row(s).');
    end loop;
end;


위에서 XXX 를 원하는 owner 로 바꿔서 실행합니다.
원하는 결과 포맷이 다르면 put_line 에서 원하는 출력 포맷으로 변경하면 되겠죠.


3. xmltable 을 이용

// 아래는 PLS-00201: identifier 'SYS.DBMS_XQUERYINT' must be declared
// 에러를 발생하고 실행 안됩니다.
 

SQL> select table_name, column_value cnt
  from user_tables, xmltable (('count(ora:view("'||table_name||'"))'))


4. xmlgen.getxml 을 이용

// 아래는 실행은 되지만, /ROWSET/ROW/C 의 의미를 몰라서 찜찜...
 

 SELECT table_name , to_number(
  extractvalue (
  xmltype (
  dbms_xmlgen.getxml ('select count(* ) c from '||table_name)),'/ROWSET/ROW/C')) No_of_recs
  FROM user_tables
  ORDER by 1;


5. num_rows 와 user_tables 을 이용

select table_name,sum(num_rows) from user_tables group by table_name order by table_name


6. dba_tables 을 조회하는 방법

// 아래의 방법은 실행 이전에 dba_tables 을 업뎃해줘야 하는 불편이 있다.
// 즉,
// exec dbms_stats.gather_schema_stats(ownname => 'NAME');
// 을 실행하고 아래의 select 를 실행해야 최신(?) 값을 얻는다. 
 

SELECT table_name,
   num_rows
  FROM dba_tables
WHERE TABLE_NAME='NAME'


To update the latest row count value in the DBA_TABLES view execute.

태그
댓글 0
홈으로 전체메뉴
전체 검색
회원가입