数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

Oracle维护常用SQL语句汇总


发布日期:2022年01月18日
 
Oracle维护常用SQL语句汇总

如何远程判断Oracle数据库的安装平台

select * from v$version;

查看表空间的使用情况

select sum(bytes)/(*) as free_spacetablespace_name

from dba_free_space

group by tablespace_name;

SELECT ATABLESPACE_NAMEABYTES TOTALBBYTES USED CBYTES FREE

(BBYTES*)/ABYTES % USED(CBYTES*)/ABYTES % FREE

FROM SYSSM$TS_AVAIL ASYSSM$TS_USED BSYSSM$TS_FREE C

WHERE ATABLESPACE_NAME=BTABLESPACE_NAME AND ATABLESPACE_NAME=CTABLESPACE_NAME;

查看表空间的名称及大小

select ttablespace_name round(sum(bytes/(*))) ts_size

from dba_tablespaces t dba_data_files d

where ttablespace_name = dtablespace_name

group by ttablespace_name;

查看表空间物理文件的名称及大小

select tablespace_name file_id file_name

round(bytes/(*)) total_space

from dba_data_files

order by tablespace_name;

查看回滚段名称及大小

select segment_name tablespace_name rstatus

(initial_extent/) InitialExtent(next_extent/) NextExtent

max_extents vcurext CurExtent

From dba_rollback_segs r v$rollstat v

Where rsegment_id = vusn(+)

order by segment_name ;

查看控制文件

select name from v$controlfile;

查看日志文件

select member from v$logfile;

查看表空间的使用情况

select sum(bytes)/(*) as free_spacetablespace_name

from dba_free_space

group by tablespace_name;

SELECT ATABLESPACE_NAMEABYTES TOTALBBYTES USED CBYTES FREE

(BBYTES*)/ABYTES % USED(CBYTES*)/ABYTES % FREE

FROM SYSSM$TS_AVAIL ASYSSM$TS_USED BSYSSM$TS_FREE C

WHERE ATABLESPACE_NAME=BTABLESPACE_NAME AND ATABLESPACE_NAME=CTABLESPACE_NAME;

查看数据库库对象

select owner object_type status count(*) count# from all_objects group by owner object_type status;

查看数据库的版本

Select version FROM Product_component_version

Where SUBSTR(PRODUCT)=Oracle;

查看数据库的创建日期和归档方式

Select Created Log_Mode Log_Mode From V$Database;

如何远程判断Oracle数据库的安装平台

select * from v$version;

查看数据表的参数信息

SELECT partition_name high_value high_value_length tablespace_name

pct_free pct_used ini_trans max_trans initial_extent

next_extent min_extent max_extent pct_increase FREELISTS

freelist_groups LOGGING BUFFER_POOL num_rows blocks

empty_blocks avg_space chain_cnt avg_row_len sample_size

last_analyzed

FROM dba_tab_partitions

WHERE table_name = :tname AND table_owner = :towner

ORDER BY partition_position

查看还没提交的事务

select * from v$locked_object;

select * from v$transaction;

回滚段查看

select rownum sysdba_rollback_segssegment_name Name v$rollstatextents

Extents v$rollstatrssize Size_in_Bytes v$rollstatxacts XActs

v$rollstatgets Gets v$rollstatwaits Waits v$rollstatwrites Writes

sysdba_rollback_segsstatus status from v$rollstat sysdba_rollback_segs

v$rollname where v$rollnamename(+) = sysdba_rollback_segssegment_name and

v$rollstatusn (+) = v$rollnameusn order by rownum

捕捉运行很久的SQL

column username format a

column opname format a

column progress format a

select usernamesidopname

round(sofar* / totalwork) || % as progress

time_remainingsql_text

from v$session_longops v$sql

where time_remaining <>

and sql_address = address

and sql_hash_value = hash_value

/

查看数据表的参数信息

SELECT partition_name high_value high_value_length tablespace_name

pct_free pct_used ini_trans max_trans initial_extent

next_extent min_extent max_extent pct_increase FREELISTS

freelist_groups LOGGING BUFFER_POOL num_rows blocks

empty_blocks avg_space chain_cnt avg_row_len sample_size

last_analyzed

FROM dba_tab_partitions

WHERE table_name = :tname AND table_owner = :towner

ORDER BY partition_position

查找object为哪些进程所用

select

pspid

ssid

sserial# serial_num

susername user_name

atype object_type

sosuser os_user_name

aowner

aobject object_name

decode(sign( command)

to_char(command) Action Code # || to_char(command) ) action

pprogram oracle_process

sterminal terminal

sprogram program

sstatus session_status

from v$session s v$access a v$process p

where spaddr = paddr and

stype = USER and

asid = ssid and

aobject=SUBSCRIBER_ATTR

order by susername sosuser

耗资源的进程(top session)

select sschemaname schema_name decode(sign( command)

to_char(command) Action Code # || to_char(command) ) action status

session_status sosuser os_user_name ssid pspid sserial# serial_num

nvl(susername [Oracle process]) user_name sterminal terminal

sprogram program stvalue criteria_value from v$sesstat st v$session s v$process p

where stsid = ssid and ststatistic# = to_number() and (ALL = ALL

or sstatus = ALL) and paddr = spaddr order by stvalue desc pspid asc susername asc sosuser asc

查看锁(lock)情况

select /*+ RULE */ lsosuser os_user_name lsusername user_name

decode(lstype RW Row wait enqueue lock TM DML enqueue lock TX

Transaction enqueue lock UL User supplied lock) lock_type

oobject_name object decode(lslmode null Row Share

Row Exclusive Share Share Row Exclusive Exclusive null)

lock_mode oowner lssid lsserial# serial_num lsid lsid

from sysdba_objects o ( select sosuser susername ltype

llmode ssid sserial# lid lid from v$session s

v$lock l where ssid = lsid ) ls where oobject_id = lsid and oowner

<> SYS order by oowner oobject_name

根据sid查是哪台电脑的链接

column osuser format a

column username format a

column machine format a

select osusermachineusernamesidserial# from v$session where sid=;

根据sid查对应的sql

select SIDSQL_TEXT from v$open_cursor where SID=;

查看等待(wait)情况

SELECT v$waitstatclass v$unt count SUM(v$sysstatvalue) sum_value

FROM v$waitstat v$sysstat WHERE v$sysstatname IN (db block gets

consistent gets) group by v$waitstatclass v$unt

查看sga情况

SELECT NAME BYTES FROM SYSV_$SGASTAT ORDER BY NAME ASC

查看catched object

SELECT owner name db_link namespace

type sharable_mem loads executions

locks pins kept FROM v$db_object_cache

查看V$SQLAREA

SELECT SQL_TEXT SHARABLE_MEM PERSISTENT_MEM RUNTIME_MEM SORTS

VERSION_COUNT LOADED_VERSIONS OPEN_VERSIONS USERS_OPENING EXECUTIONS

USERS_EXECUTING LOADS FIRST_LOAD_TIME INVALIDATIONS PARSE_CALLS DISK_READS

BUFFER_GETS ROWS_PROCESSED FROM V$SQLAREA

查看object分类数量

select decode (otype#INDEX TABLE CLUSTER VIEW

SYNONYM SEQUENCE OTHER ) object_type count(*) quantity from

sysobj$ o where otype# > group by decode (otype#INDEX TABLE

CLUSTER VIEW SYNONYM SEQUENCE OTHER ) union select

COLUMN count(*) from l$ union select DB LINK count(*) from

按用户查看object种类

select uname schema sum(decode(otype# NULL)) indexes

sum(decode(otype# NULL)) tables sum(decode(otype# NULL))

clusters sum(decode(otype# NULL)) views sum(decode(otype#

NULL)) synonyms sum(decode(otype# NULL)) sequences

sum(decode(otype# NULL NULL NULL NULL NULL NULL ))

others from sysobj$ o sysuser$ u where otype# >= and uuser# =

oowner# and uname <> PUBLIC group by uname order by

syslink$ union select CONSTRAINT count(*) from n$

有关connection的相关信息

)查看有哪些用户连接

select sosuser os_user_name decode(sign( command) to_char(command)

Action Code # || to_char(command) ) action pprogram oracle_process

status session_status sterminal terminal sprogram program

susername user_name sfixed_table_sequence activity_meter query

memory max_memory cpu_usage ssid sserial# serial_num

from v$session s v$process p where spaddr=paddr and stype = USER

order by susername sosuser

)根据vsid查看对应连接的资源占用等情况

select nname

vvalue

nclass

nstatistic#

from v$statname n

v$sesstat v

where vsid = and

vstatistic# = nstatistic#

order by nclass nstatistic#

)根据sid查看对应连接正在运行的sql

select /*+ PUSH_SUBQ */

command_type

sql_text

sharable_mem

persistent_mem

runtime_mem

sorts

version_count

loaded_versions

open_versions

users_opening

executions

users_executing

loads

first_load_time

invalidations

parse_calls

disk_reads

buffer_gets

rows_processed

sysdate start_time

sysdate finish_time

> || address sql_address

N status

from v$sqlarea

where address = (select sql_address from v$session where sid = )

.查询表空间使用情况

select atablespace_name 表空间名称

round((nvl(bbytes_free)/abytes_alloc)*) 占用率(%)

round(abytes_alloc//) 容量(M)

round(nvl(bbytes_free)//) 空闲(M)

round((abytes_allocnvl(bbytes_free))//) 使用(M)

Largest 最大扩展段(M)

to_char(sysdateyyyymmdd hh:mi:ss) 采样时间

from (select ftablespace_name

sum(fbytes) bytes_alloc

sum(decode(fautoextensibleYESfmaxbytesNOfbytes)) maxbytes

from dba_data_files f

group by tablespace_name) a

(select ftablespace_name

sum(fbytes) bytes_free

from dba_free_space f

group by tablespace_name) b

(select round(max(fflength)*/) Largest

tsname tablespace_name

from sysfet$ ff sysfile$ tfsysts$ ts

where tsts#=ffts# and fffile#=tfrelfile# and tsts#=tfts#

group by tsname tfblocks) c

where atablespace_name = btablespace_name and atablespace_name = ctablespace_name

查询表空间的碎片程度

select tablespace_namecount(tablespace_name) from dba_free_space group by tablespace_name

having count(tablespace_name)>;

alter tablespace name coalesce;

alter table name deallocate unused;

create or replace view ts_blocks_v as

select tablespace_nameblock_idbytesblocksfree space segment_name from dba_free_space

union all

select tablespace_nameblock_idbytesblockssegment_name from dba_extents;

select * from ts_blocks_v;

select tablespace_namesum(bytes)max(bytes)count(block_id) from dba_free_space

group by tablespace_name;

查询有哪些数据库实例在运行

select inst_name from v$active_instances;

查找oracle性能瓶颈sql

select sql_textspidv$sessionprogramprocess from

v$sqlareav$sessionv$process

where v$sqlareaaddress=v$sessionsql_address

and v$sqlareahash_value=v$sessionsql_hash_value

and v$sessionpaddr=v$processaddr

and v$processspid in (操作系统PID);

select sideventpptext from v$session_wait;

找出最耗资源的sql

select * from v$process where spid=;

select sql_hash_valuemachineusernameprogram from v$session where PAddr=BA;

select * from v$sqltext where hash_value=;

select * from v$sql where hash_value=;

select * from v$sqlarea where hash_value=;

SELECT ausernameamachineaprogramasidaserial#astatuscpiececsql_text FROM v$session av$process bv$sqltext c WHERE bspid= AND baddr=apaddr AND asql_address=caddress(+) ORDER BY cpiece

要找出最耗资源的sql我们可以首先使用top等工具找到最好资源的进程(记住进程号)例如操作系统进程号为然后根据这个进程号(v$processspid)在v$process中找到进程地址(v$processaddr)然后根据这个地址在 v$session中找到相应的sid(v$sessionsid)然后根据这个sid找到相应的hash alue(v$session sql_hash_value)然后根据这个hash alue在v$sqltext$sqlv$sqlarea等视图中找到对应的sql语句(sql_text)

select * from v$process where spid=;

select sql_hash_valuemachineusernameprogram from v$session where PAddr=BA;

select * from v$sqltext where hash_value=;

select * from v$sql where hash_value=;

select * from v$sqlarea where hash_value=;

SELECT ausernameamachineaprogramasidaserial#astatuscpiececsql_text FROM v$session av$process bv$sqltext c WHERE bspid= AND baddr=apaddr AND asql_address=caddress(+) ORDER BY cpiece

               

上一篇:Oracle11g新特性之:数据压缩技术

下一篇:全面探讨PL/SQL的复合数据类型