Oracle common maintenance operations

I. Introduction

First, declare that I am not a DBA. Any inaccuracies in the document are welcome to point out. As for why I wrote this article, it is mainly a summary of my usual work. You may ask me "Does your company or department do not have a DBA?" There must be, but DBA is a scarce resource in our company. There are more than 100 people. There is only one DBA in China, it is not a matter of importance, it is impossible to tell them! There is no way for the project manager, some work can only be done by himself!

Two, Oracle common maintenance operations

1. Start and stop the database

# 启动
su - oracle
sqlplus / as sysdba;
SQL>startup;

# 停止
su - oracle
sqlplus / as sysdba;
SQL>shutdown immediate;

2. Start and stop monitoring

# 启动
su - oracle
lsnrctl start
lsnrctl status

# 停止
su - oracle
snrctl stop
lsnrctl status

Note : The listener is dynamically registered every few minutes. If there is no corresponding database instance when the listener status is executed, you can try to register manually.

sqlplus / as sysdba
SQL>alter system register;

3. IP whitelist

  • Single node
su - oracle
cd $ORACLE_HOEM/network/admin/
# 查看是否存在sqlnet.ora文件及是否已配置tcp.validnode_checking、tcp.invited_nodes、tcp.excluded_nodes项。如果文件存在且已有这几项,则在此基础上修改这几项;如果文件存在但这几项不存在,则在最后追加;如果文件不存在则直接新建。

vim sqlnet.ora
# 追加以下内容:
tcp.validnode_checking=yes
tcp.invited_nodes=(127.0.0.1,自身IP,192.168.1.128,192.168.1.129)
tcp.excluded_nodes=(192.168.1.242)
lsnrctl reload

Note : Be sure to add all your own IP, otherwise the status of the listener may not be able to register the database instance to the listener.

Description :

tcp.validnode_checking—This item is to enable IP legal checking, this item must be configured, if you do not enable the other two, it is useless
tcp.invited_nodes—This item is the IP address that is allowed to connect to the database, multiple addresses use commas ( Half-width); if you enable this option, you must match the local address, otherwise the monitoring will not start.
tcp.excluded_nodes—This item is the IP address that is not allowed to connect to the database. Multiple addresses are separated by commas (half-width); if they have the same IP as tcp.invited_nodes, the IP is still allowed to access the database
tcp.invited_nodes and tcp.excluded_nodes. You can configure only one item. When only tcp.invited_nodes is configured, it is a whitelist. All IPs in this list are allowed to access the database, and all IPs not in this list are denied access to the database;
when only tcp.excluded_nodes is configured, All IPs in this list are denied access to the database, and all IPs not in this list are allowed to access the database.

  • RAC cluster
# 备份和恢复oracle ini初始化文件,防止配置错误或其它原因导致的服务不可用
su - oracle
# 备份
SQL>create pifle='/home/backup/pfile20200920.ora' from spfile;
# 恢复
SQL>create spfile from pifle='/home/backup/pfile20200920.ora' ;

Note : spfile scope memory, spfile, both (default); when modifying initialization parameters such as memory, CPU, number of connections, etc., it is strongly recommended to backup spfile

su - grid
$GRID_HOME/NETWORK/ADMIN
vim sqlnet.ora
# 追加以下内容:
tcp.validnode_checking=yes
tcp.invited_nodes=(127.0.0.1,private IP,scan ip,virual IP,192.168.1.128,192.168.1.129)
tcp.excluded_nodes=(192.168.1.242)
# 执行以下命令重新加载监听器配置,等待几分钟(期间监听器对外服务不可用,建议一个一个节点执行,防止期间数据库无法访问)。
lsnrctl reload

Description :

  • There is no sqlnet. ora file in the ORACLEHOME / NETWORK / ADMIN directory of the ORACLE user . You must log in as a GUID user to enter the ORACLE_HOME/NETWORK/ADMIN directory. There is no sqlnet.ora file in the directory, you must log in as a GUID user.ORACLEHO M E / N E T W is O R & lt K / A D M the I N entry recorded at not have S Q L n- E T . O R & lt A text element , must be so with G the U- the I D with a user login entries into the GRID_HOME Add and modify the sqlnet.ora file under /NETWORK/ADMIN.

  • When adding the whitelist, the local IP must be added. Because it is a RAC environment, all the real IP, private IP, VIP, and SCAN IP of each section must be added to the whitelist.

  • Each node must perform this operation.

4. Password expired

su - oracle
sqlplus / as sysdba
SQL>SELECT * FROM dba_profiles s WHERE s.profile=\'DEFAULT\' AND resource_name=\'PASSWORD_LIFE_TIME\';
SQL>ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;
SQL>alter user scott identified by tiger;

5. Data pump import and export

(1) Manually create a data pump export directory** (optional)**

su - oracle
mkdir sdir
sqlplus / as sysdba
SQL>create directory DATA_PUMP_DIR_SCOTT as '/home/oracle/sdir';
SQL>grant read,write on directory DATA_PUMP_DIR_SCOTT to scott;
SQL> exit

(2) Export

# 查看默认的数据泵导出目录(DATA_PUMP_DIR),并记录下来。
su - oracle
sqlplus / as sysdba
SQL>select * from dba_directories;
SQL> exit
expdp SCOTT/SCOTT directory=DATA_PUMP_DIR dumpfile=scott20200920.dmp schemas=SCOTT

(3) Create table space, user, authorization

SQL>CREATE TEMPORARY TABLESPACE SCOTT_TEMP TEMPFILE 'D:\app\oracle\data\SCOTT_TEMP.DBF'  size 2048M autoextend on next 50M maxsize 20480M EXTENT MANAGEMENT LOCAL;
SQL>create tablespace TBS_SCOTT_TEST datafile 'D:\app\oracle\data\TBS_SCOTT01.DBF' size 2048M autoextend on next 50M maxsize 20480M EXTENT MANAGEMENT LOCAL;
SQL>create user SCOTT_TEST identified by SCOTT_TEST default tablespace TBS_SCOTT_TEST TEMPORARY TABLESPACE SCOTT_TEMP;
SQL>GRANT CONNECT,RESOURCE TO SCOTT_TEST;
# 可选
SQL>grant all privileges to SCOTT_TEST;--除DBA权限之外的所有权限(可选)
SQL>GRANT DBA TO SCOTT_TEST;  --DBA权限(可选)

(4) Import

impdp SCOTT_TEST/SCOTT_TEST directory=DATA_PUMP_DIR dumpfile=scott20200920.dmp remap_schema=SCOTT:SCOTT_TEST remap_tablespace=TBS_SCOTT:TBS_SCOTT_TEST

Note : If the schema and tablespace are different from those during export, you need to specify remap_schema and remap_tablespace.

Reference :

Oracle Data Pump

6. Table space usage (including temporary table space)

select * from ( 
Select a.tablespace_name, 
to_char(a.bytes/1024/1024,'99,999.999') total_bytes, 
to_char(b.bytes/1024/1024,'99,999.999') free_bytes, 
to_char(a.bytes/1024/1024 - b.bytes/1024/1024,'99,999.999') use_bytes, 
to_char((1 - b.bytes/a.bytes)*100,'99.99') || '%' use 
from (select tablespace_name, 
sum(bytes) bytes 
from dba_data_files 
group by tablespace_name) a, 
(select tablespace_name, 
sum(bytes) bytes 
from dba_free_space 
group by tablespace_name) b 
where a.tablespace_name = b.tablespace_name 
union all 
select c.tablespace_name, 
to_char(c.bytes/1024/1024,'99,999.999') total_bytes, 
to_char( (c.bytes-d.bytes_used)/1024/1024,'99,999.999') free_bytes, 
to_char(d.bytes_used/1024/1024,'99,999.999') use_bytes, 
to_char(d.bytes_used*100/c.bytes,'99.99') || '%' use 
from 
(select tablespace_name,sum(bytes) bytes 
from dba_temp_files group by tablespace_name) c, 
(select tablespace_name,sum(bytes_cached) bytes_used 
from v$temp_extent_pool group by tablespace_name) d 
where c.tablespace_name = d.tablespace_name 
) ;

7. Expand the table space

# 查看数据文件位置
SQL>select name from v$datafile;
# 为指定表空间增加数据文件
SQL>ALTER TABLESPACE TBS_SCOTT ADD DATAFILE 'D:\app\oracle\data\TBS_SCOTT02.DBF' SIZE 20480M;

Note : The grid disk is used under Oracle RAC, and the "+" following the directory cannot be omitted. For example: "+/datagrid01/data/TBS_SCOTT02.dbf"

8. Modify the maximum number of Oracle connections

# 备份和恢复oracle ini初始化文件,防止配置错误或其它原因导致的服务不可用
su - oracle
# 备份
SQL>create pifle='/home/backup/pfile20200920.ora' from spfile;
# 恢复
SQL>create spfile from pifle='/home/backup/pfile20200920.ora' ;
SQL>alter system set processes=1000 scope = spfile;

Note : sessions is a derived value, which is determined by the value of processes. The formula is generally sessions=1.1*process + 5, and no setting is required.

9. Manually release the lock

# 锁表情况
SQL>select object_name,machine,s.sid,s.serial#
from v$locked_object l,dba_objects o ,v$session s
where l.object_id = o.object_id and l.session_id=s.sid;

SQL>select S.sid,S.SERIAL# from v$session S where username='SCOTT';
SQL>alter system kill session 'sid,SERIAL'

Note : Oracle Linux version, session is process level, Windows version is thread level. If the resources cannot be completely released under Linux using the above commands, you can use OS level kill to force release.

Linux:

SQL>select spid, osuser,s.program
from v$session s,v$process p
where s.paddr=p.addr and s.sid=24 (24是上面的sid)
SQL>exit
kill -9 12345(12345为上面查询出的spid)

windows:

orakill sid thread #分别为24 12345

10.Statistics status query and automatic collection

-- 查询统计信息是否陈旧
SQL>select * from dba_tab_statistics order by last_analyzed desc;

Introduction :

Statistic is very important to Oracle. It collects detailed information about objects in the database and stores them in the corresponding data dictionary. Based on these statistics, the optimizer can choose the best execution plan for each SQL. The collection of Oracle Statistic information is divided into two types: automatic collection and manual collection.

Reference :

Statistics

Automatic collection

Oracle's automatic maintenance tasks

In oracle11g, there is a new feature of autotask, which can be used to automatically collect optimization information, collect segment information, and so on. In EM, we look into the menu server-Oracle Scheduler-Automated Maintenance Tasks to see them:

Automatically collect tasks

linux start EM

emctl start dbconsole

Visit address:
http://localhost:1158/em

Enable automatic collection tasks

  • Turn on the main switch

    --可查看SELECT * FROM SYS.DBA_AUTOTASK_TASK;是否有值,默认没有,开启后有值!
    begin
    dbms_auto_task_admin.enable();--或dbms_auto_task_admin.enable;
    end;
    
  • Open the designated maintenance window (all open from Monday to Sunday by default, can be selectively closed)

    SELECT * FROM SYS.DBA_SCHEDULER_WINDOW;
    -- 打开
    dbms_scheduler.enable(name=>'SATURDAY_WINDOW');
    -- 关闭
    dbms_scheduler.disable(name=>'SATURDAY_WINDOW',froce=>true);
    
  • Perform historical query

    SELECT * FROM SYS.DBA_AUTOTASK_JOB_HISTORY;
    SELECT * FROM SYS.DBA_AUTOTASK_CLIENT_HISTORY;
    

11. Bind variables

Question : You may encounter such a problem. Some SQL in our java code or mybatis xml is very slow to execute, but I take the SQL into a tool such as sqldeveloper or navicate and execute it quickly. Why?

Possible reason: To answer this question, we must first know what operations are done in our code.

  • Whether it is paging SQL, in addition to a SELECT C1, C2 FROM SQL statement, there is also a SELECT COUNT(0) FROM SQL statement, and the slow one is often COUNT(0)
  • Whether to perform variable binding, pay attention to "variable binding", not "splicing character" (such as ${} in Mybatis)

“变量绑定”是什么?
bind variable: A variable in a SQL statement that must be replaced with a valid value, or the address of a value, in order for the statement to successfully execute.

Variable binding is a very interesting technology in OLTP systems. Good variable binding will make the SQL in the OLTP system database execute fast and have extremely high memory efficiency; not using bind variables may make the OLTP database overwhelmed, resources will be severely exhausted by SQL parsing, and the system will run slowly. When a user establishes a connection with the database, it will issue an operation request to the database, that is, send SQL statements to the database. After Oracle receives these SQLs, it will first perform a hash function operation on the SQL to obtain a hash value, and then go to the shared pool to find out whether there is a SQL matching the hash value. If found, Oracle will directly use the existing SQL execution plan to execute the current SQL, and then return the result to the user. If the SQL with the same hash value is not found in the shared pool, Oracle will consider this to be a new SQL. Will be resolved.

The steps of Oracle parsing are as follows:
(1) Syntax parsing
(2) Semantic parsing
(3) Execution plan generation, which is divided into soft parsing and hard parsing. Hard analysis is very resource intensive.
(4) The execution of SQL

One more question, if you use the "bind variable" method to assign values ​​to parameters, will the variables be ignored during SQL parsing?
Keywords : "Variable Spy" "SQL Dynamic Cursor"

Bind Peeking is a new feature introduced in Oracle 9i. Its role is to check the value of the current SQL predicate during the hard analysis of the SQL statement in order to generate the best execution plan.

If you want to understand the mechanism of SQL, you can explain it clearly without a blog. I recommend you to read these two books!
"Detailed Oracle Tutorial" : medium difficulty (DBA must read, R&D personnel recommend reading through)
"Oracle Core Technology %2B Louis" : high difficulty (DBA recommend reading through)

For example:

SELECT * FROM DUAL WHERE '1' = :v1;
SELECT * FROM DUAL WHERE '1' = '1';

Bind variable

12. Table partition

-- 按月分区,2018年以前数据归为分区P1
CREATE TABLE T_TEST
(
   TABLE_ID NUMBER(8),
   SUB_DATE DATE,
   VALUE NUMBER(8)
 
)
PARTITION BY RANGE(SUB_DATE)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(
  PARTITION P1 VALUES LESS THAN(TO_DATE('2018-01-01','YYYY-MM-DD'))
);

--查看分区
SELECT * FROM USER_TAB_PARTITIONS WHERE TABLE_NAME=UPPER('T_TEST_PARTITION');

To be continued...

Guess you like

Origin blog.csdn.net/ory001/article/details/108715361