首页 数据库

达梦7、8
1.查询会话中程序占用会话数

select appname,count(*) from v$sessions group by  "V$SESSIONS".APPNAME;

2.查看用户下指定表占用空间

SELECT TABLE_USED_SPACE('username','tablename')*SF_GET_PAGE_SIZE()/1024/1024||'M'

3.查看用户下指定表实际使用的空间

SELECT TABLE_USED_PAGES('unsername','tablename')*SF_GET_PAGE_SIZE()/1024/1024||'M'

4.批量删除包含test字段的表

select 'drop table '||owner||'.'||table_name||'; 'from DBA_TABLES where TABLE_NAME like '%test%' and OWNER = 'test';

5.批量查询表空间大小以及占用百分比

select
        a.tablespace_name                    ,
        a.bytes        /1024/1024 "Sum MB"          ,
        (a.bytes       -b.bytes)/1024/1024 "used MB",
        b.bytes        /1024/1024 "free MB"         ,
        round(((a.bytes-b.bytes)/a.bytes)*100, 2) "percent_used"
from
        (
                select
                        tablespace_name,
                        sum(bytes) bytes
                from
                        dba_data_files
                group by
                        tablespace_name
        )
        a,
        (
                select
                        tablespace_name ,
                        sum(bytes) bytes,
                        max(bytes) largest
                from
                        dba_free_space
                group by
                        tablespace_name
        )
        b
where
        a.tablespace_name=b.tablespace_name
order by
        ((a.bytes-b.bytes)/a.bytes) desc

6.查询用户下有哪些模式

select ur.name,sch.name from sysobjects sch ,sysbojects ur where ur.subtype$='USER' and sch.type$='SCH' and ur.name not like 'SYS%' and sch.pid=ur.id oder by 1;

7.查询某个用户下被授权了哪些角色

select * from "SYS"."DBA_ROLE_PRIVS" where GRANTED_ROLE like '%用户名%';

8.列出平均工资在1000之上,并且最低工资不低于800的工作信息(基本函数使用)

select job,avg(sal),min(sal) from emp group by job having avg(sal)>1000 and min(sal)>=800

9.列出不是销售员最低薪资大于1500的各种工作的信息(基本函数使用)

select job,min(sal) from emp where job<>'SALESMAN' group by job having min(sal)>1500

10.显示非销售人员工作名称以及从事同一工作雇员的月工资的总和,并且要满足从事同一工作的雇员的月工资合计大于$5000,输出结果按月工资的合计升序排列(基本函数使用)

SELECT job,SUM(sal) S_U_M FROM emp WHERE job<>'SALESMAN' GROUP BY job HAVING S_U_M>5000 ORDER BY S_U_M 

11.查看使用内存大的SQL语句:

SELECT SF_GET_SESSION_SQL(SESSID),MAX_MEM_USED,SQL_TXT FROM V$SQL_STAT ORDER BY MAX_MEM_USED DESC;
SELECT * FROM V$LARGE_MEM_SQLS;

12.查看未提交的事务

select t2.name from v$lock t1,sysobjects t2 where t1.table_id=t2.id and subtype$='UTAB';

13.查看最大会话数

select para_value from v$dm_ini where para_name='MAX_SESSIONS';

14.显示最近 100 条执行时间较长的 SQL 语句

select * from v$long_exec_sqls;

15.显示服务器启动以来执行时间最长的20 条 SQL 语句

select * from v$system_long_exec_sqls;

16.查询执行SQL错误码

select * from V$ERR_INFO where code = '-6111';

17.表重命名

alter table test rename to test_table

18.查询表注释

select comments from user_tab_comments where table_name = 'test_table'

19.增加列

alter table test_table add ids int

20.增加一列不为空

alter table test_table add id_notnull int not null

21.删除列,列名:ids?

alter table test_table drop ids

22.修改列名

alter table test_table rename column idss to ids

23.禁用所有表的外键

select 'alter table '||owner||'.'||table_name||' disable constraint '||constraint_name||'; ' from sys.dba_constraints where owner not like 'SYS%' and constraint_type='R';

24.某张表执行delete操作时卡住,sql查出与问题表关联的外键后删除数据库后启用

select
       'alter table "'||OWNER||'"."'||rel.table_name||'" disable constraint "'||rel.constraint_name||'";' 
from
        sysobjects ptab,
        sysobjects pidx,
        dba_constraints rel
where
        pidx.pid=ptab.id
    and rel.r_constraint_name=pidx.name
    and rel.constraint_type='R'
    and ptab.name    ='表名不用带模式名'

25.查询服务器连接达梦数据库的应用数量和ip最多的前20个

netstat -an |grep ESTABLISHED |awk '{print $5}' |awk -F: '{print $1}' |sort|uniq -c |sort -rn |head -n 20

26.查询数据库占用大小(单位/bite)

select SUM(bytes) from dba_data_files;

27.查询锁

select s.name,trx_id from v$lock l ,sysobjects s   where blocked=0 and ign_flag!=1 and lmode like '%X' and l.table_id=s.id

28.top -HP pid查出长期占用的进程后使用该命令查出进程所执行的sql

select
        SESS_ID                                       ,
        "V$SESSIONS".USER_NAME                        ,
        SQL_TEXT                                      ,
        "V$SESSIONS".TRX_ID                           ,
        "V$SESSIONS".THRD_ID,
        "V$SESSIONS".CREATE_TIME,
        "V$SESSIONS".CLNT_IP
FROM
        V$SESSIONS
WHERE
        "V$SESSIONS".THRD_ID IN (47944);

29.查询慢sql

SELECT
        *
FROM
        (
                SELECT
                        USER_NAME                               ,
                        CLNT_IP                                 ,
                        SESS_ID                                 ,
                        SQL_TEXT                                ,
                        DATEDIFF(SS, LAST_SEND_TIME, SYSDATE) SS,
                        SF_GET_SESSION_SQL(SESS_ID) FULLSQL     ,
                        APPNAME                                 
                FROM
                        V$SESSIONS
                WHERE
                        STATE='ACTIVE'
                    AND USER_NAME!='SYSDBA'
        )
WHERE
        SS>1
ORDER BY
        5 DESC

30.数据库还原命令

RESTORE DATABASE '/dbdata/dmdata/DKY/dm.ini' FROM BACKUPSET '/dbdata/dmdata/DKY/bak/bak_full_1111/DB_DKY_INCREMENT_2021_11_11_03_00_55';

31.查询进程占用较大的服务

ps aux|grep -v PID|sort -nr -k3 |head -n10

32.查询进程占用较大的服务

ps aux|grep -v PID|sort -nr -k4 |head -n10

33.当外键引用过多删除数据缓慢时,在一个manager窗口执行如下命令可以忽略约束关联检查

set_session_cons_chk(0)

34.查看外键属于哪个表

SELECT * FROM DBA_CONSTRAINTS where CONSTRAINT_NAME    = 'CONS134230442';

35.批量授权拼sql

SELECT 'GRANT SELECT ON "'||OWNER||'"."'||TABLE_NAME||'" TO "SG_DATACENTER";' FROM DBA_TABLES WHERE OWNER = '模式名';

36.查看哪些用户有连接限制

select
        A.allow_addr    ,
        A.not_allow_addr,
        A.allow_dt      ,
        A.not_allow_dt  ,
        B.USERNAME
from
        SYSUSERS A,
        DBA_USERS B
where
        A.ID=B.USER_ID;

37.查看某个模式下所有表的表的数据量

CREATE TABLE "SYSDBA"."COUNT_SG_DATACENTER"
(
"TAB_NAME" VARCHAR(1000),
"ROW_NUM" INT) STORAGE(ON "MAIN", CLUSTERBTR) ;

select * from "SYSDBA"."COUNT_SG_DATACENTER" ORDER BY ROW_NUM DESC;


DECLARE
        COUNT_ROWS INT;
        V_SQL      VARCHAR(100);
BEGIN
        COUNT_ROWS:=0;
        FOR A     IN
        (
                SELECT
                        TABLE_NAME
                FROM
                        DBA_TABLES
                WHERE
                        OWNER='SG_DATACENTER'
                    AND TABLE_NAME LIKE '%2021')
                LOOP
                        V_SQL:='SELECT COUNT(*) FROM SG_DATACENTER.'||A.TABLE_NAME;
                        EXECUTE IMMEDIATE V_SQL INTO COUNT_ROWS;
                        INSERT INTO COUNT_SG_DATACENTER VALUES
                                (A.TABLE_NAME,COUNT_ROWS
                                );
                END LOOP;
                END;
SELECT * FROM COUNT_SG_DATACENTER ORDER BY ROW_NUM DESC;

38.查看正在使用有事物的表的purge

select
       b.NAME,COUNT(1) AS NUM
from
        V$PURGE_PSEG_TAB a,
        SYS.SYSOBJECTS b
where
        a.TAB_ID = b.ID
GROUP BY b.NAME
ORDER BY NUM DESC

39.查看执行时间长的sql

SELECT
        *
FROM
        (
                SELECT
                        sess_id                                         ,
                        sql_text                                        ,
                        datediff (ss, last_recv_time, SYSDATE) Y_EXETIME,
                        SF_GET_SESSION_SQL (SESS_ID) fullsql            ,
                        clnt_ip
                FROM
                        V$SESSIONS
                WHERE
                        STATE = 'ACTIVE'
        )
WHERE
      Y_EXETIME >= 2;  --执行时间超2s,可以自定义该时间

40.查看所有表的大小GB

select
        owner     ,
        table_name,
        table_used_pages(owner, table_name)*(page()/1024)/1024/1024 GB 
from
        all_tables
WHERE
        owner not in ('SYS', 'SYSJOB', 'CTISYS') ORDER BY GB DESC

41.收集大表上某列的统计信息

stat 100 on LFDB.SG_DEV_TRANSFMWD_H1_MEA_2021(CREATE_TIME);
stat 100 on LFDB.SG_DEV_ACLINE_H1_MEA_2021(CREATE_TIME);

42.更新大表上列的统计信息

stat 100 on SG_DATACENTER.SG_DEV_BUSBAR_H1_MEA_2023(create_time);

42.更新大表上索引的统计信息

stat 100 on SG_DATACENTER INDX_ BUSBAR_2023;

43.查询活动会话执行的时间倒序

select timestampdiff(ss,last_send_time,sysdate),* from v$sessions  where state = 'ACTIVE'  order by timestampdiff(ss,last_send_time,sysdate) desc

44.dump执行计划

select * from v$cachepln;
alter session set events 'immediate trace name plndump level 281338592839728,dump_file ''/home/dmdba/dmdbms/0715.log''';

45.清除执行计划

sp clear plan cache

46杀掉会话

说明:281224128100360是v$sessions中的sess_id,如果无法杀掉可以先执行对该会话的终止命令后在进行杀会话
sp_close_session(281224128100360);
终止 id 为 310509680 的会话的操作
call sp_cancel_session_operation (310509680);

达梦6
1.查询cpu占用较长时间未释放的sql

select sql_text,app_name,login_name,cpu_time_call frome v$session where cpu_time_call > 1000;

--dm6,dm7登录问题
问题原因:
@作为特殊符号使用,有歧义无法在密码中直接识别
解决方法:
使用用例:
(1)DM6的密码包含@特殊字符处理办法:
使用时请注意,如果密码中包含@符号,则连接串中的数据库服务器名或者ip必须被填充。
例如:./isql test/test@ch@127.0.0.1,最后一个@后的字符为数据库服务器ip
(2)DM7的密码包含特殊字符处理办法:
有时候密码包含了@等特殊字符导致disql无法直接连接和运行。需要通过转移符来处 理。disql转义符使用如下
1.linux下,需要使用双引号将密码包含进来,同时外层再使用单引号进行转移,具体例子如下:
./disql SYSDBA/'"abcd@efgh"'@localhost
2.windows下,需要使用双引号将密码包含进来,同时对双引号使用进行转移,具体例子如下:
disql SYSDBA/"abcd@efgh"@localhost
背景知识:
使用’””’或者””的方式可将特殊字符转化为普通字符使用
--动态开启dm.ini参数
call sp_set_para_value(2,’RLOG_APPEND_LOGIC’,1);
call sp_set_para_value(2,'ENABLE_ENCRYPT',0);


文章评论

    rgovsocpjk 访客ChromeWindows
    21天 前 回复

    叼茂SEO.bfbikes.com