search
HomeDatabaseMysql Tutorial10gocm-)session4-)数据仓库

一 当有数据文件被误删除时如何恢复 RMAN规划 host target库 catalog库 ocm01 ocmdb ocm02 ocmdb02 模拟ocmdb库system01.dbf 数据文件丢失,利用catalogue库omcdb02进行RMAN恢复。 构造场景 shutdown immediate rm -f /u01/oracle/oradata/ocmdb/disk1/syste


一 当有数据文件被误删除时如何恢复
RMAN规划
host target库 catalog库
ocm01 ocmdb  
ocm02   ocmdb02

模拟ocmdb库system01.dbf 数据文件丢失,利用catalogue库omcdb02进行RMAN恢复。

构造场景
shutdown immediate
rm -f /u01/oracle/oradata/ocmdb/disk1/system01.dbf
sqlplus / as sysdba
startup
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u01/oracle/oradata/ocmdb/disk1/system01.dbf'
alert日志中同样记录了报错信息(现在数据库处于异常关闭状态),从告警日志中就可以知道是system文件没有了 --alert日志位置:/u01/oracle/admin/ocmdb/bdump/alert_ocmdb.log
将数据库启动到mount状态,才能restore and recover 恢复
shutdown abort
startup mount
exit
登录RMAN
rman target sys/sys@ocmdb1521 catalog catalog_admin/catalog_admin@ocmdb021521
用原来的RMAN备份来还原和恢复
restore datafile 1;
recover datafile 1;
alter database open;
select file_name,tablespace_name,status from dba_data_files where tablespace_name='SYSTEM';

二 物化视图

物化视图规划
host 基表&日志 物化视图 dblink
ocm01   mv_t dblink_to_ocm02
ocm02 t_mv_ocm02,MLOG$_T_MV_OCM02    

物化视图:执行sql并保留结果,直接放在数据文件中,不放在内存中方便重用【空间换时间】
场合:同步数据,过滤数据并两次利用

1.要求创建跨库物化视图并可手动更新
官方文档:PL/SQL Packages and Types Reference -> 61 DBMS_MVIEW
基表和物化视图不在同一个库上,基表和日志在ocm02上,物化视图在ocm01上,在ocm01上可手动刷新物化视图

ocm02

create user mv_ocm02 identified by mv_ocm02 ;
grant connect,resource to mv_ocm02 ;
conn mv_ocm02 /mv_ocm02
create table t_mv_ocm02(a varchar2(10));
insert into t values ('test01');
commit;

select * from t_mv_ocm02;

创建物化视图日志,基表在哪个库,日志就在哪个库,用于记录基表所有变化,实时刷新物化视图,完全刷新不需要物化视图日志,快速刷新需要物化视图日志

create materialized view log on t with rowid; 基于rowid变化记录到日志中 --创建MLOG$_T_MV_OCM02对象

ocm01

创建dblink
sqlplus / as sysdba@ocmdb
create public database link dblink_to_ocm02 connect to mv_ocm02 identified by mv_ocm02 using 'ocmdb021521';
注:global_names参数为FALSE 才允许DB link名字与我远程连接实例名不一致
create user mv_ocm01 identified by mv_ocm01 ;
grant dba to mv_ocm01 ;
conn mv_ocm01 /mv_ocm01
创建跨库可刷新物化视图

create materialized view mv_t refresh fast with rowid as select * from t_mv_ocm02@dblink_to_ocm02;

sqlplus mv_ocm02 /mv_ocm02 @ocmdb021521
insert into t values ('test02');
commit;
select * from t_mv_ocm02;
sqlplus mv_ocm01 /mv_ocm01 @ocmdb

select * from mv_t;

快速刷新方法:增量刷新 或 完全刷新方法:全表刷新
exec dbms_mview.refresh('mv_t','f'); 手动刷新物化视图
exec dbms_mview.refresh('mv_t','c');

select * from mv_t;

2.要求创建物化视图具有自动快速刷新功能
sqlplus mv_ocm01 /mv_ocm01 @ocmdb
注:grant connect,resource,create materialized view to mv_ocm01 ; 如果是普通用户需要授予创建物化视图权限
drop table t1 purge;
create table t1 (x int,y int,z int);
insert into t1 values (1,2,3);
insert into t1 values (4,5,6);
commit;
select * from t1;
create materialized view log on t1 with sequence,rowid (x,y,z) including new values;
我们只看x和y列信息
drop materialized view mv_t1;
create materialized view mv_t1 build immediate refresh with rowid fast on commit enable query rewrite as select x,y from t1;
参数说明:
build immediate:创建物化视图时,立即刷新基表
fast on commit:支持基于commit动作的自动刷新
enable query rewrite:启动查询重写功能
refresh with rowid:物化视图默认是基于主键PK方式来刷新的,由于基表没有主键,因此我们基于ROWID刷新
select * from mv_t1;
验证物化视图是否随记录增加而更新
insert into t1 values (7,8,9);
select * from t1;
select * from mv_t1; mv_t1随记录增加而木有刷新,必须commit之后才触发物化视图刷新
commit;
select * from mv_t1;
delete from t1 where x=1;
select * from t1;
commit;
select * from mv_t1;

三 外部表

官方文档:Administrator’s Guide -> 15 Managing Tables -> Managing External Tables
Utilities->Part III External Tables –> 14 The ORACLE_DATAPUMP Access Driver
外部表:把数据保存在操作系统层面上,并不保存在表空间上,即可以把数据写入外部表又可以读取外部表到数据库(只读到内存里)。外部表就是操作系统上的一个二进制文件不是纯文本文件。可用strings查看

场合:导出的数据可用于二次开发。数据迁移

要求使用oracle数据泵导出外部表并跨库加载
过程:ocm01卸载数据,传输到ocm02加载数据

外部表实验规划
步骤 ocm01 ocm02 说明
1 创建dir_dmp   位置:ocm01:/home/oracle
2 导出t2_part1.dat,t2_part2.dat到dir_dmp    
3   创建dir_dmp 位置:ocm02:/home/oracle
4   拷贝t2_part1.dat,t2_part2.dat到dir_dmp scp把文件从ocm01拷贝到ocm02
5   加载到数据库  

创建directory
sqlplus / as sysdba@ocmdb
create user ext identified by ext;
grant dba to ext;
conn ext/ext
create directory dir_dmp as '/home/oracle';
grant read,write on directory dir_dmp to public; 授予读/写权限给目录对象,把目录对象给所有用户
创建外部表,使用数据泵工具卸载数据,使用2个cup并行卸载,加快速度
create table t2
organization external
(type oracle_datapump
default directory dir_dmp
location ('t2_part1.dat','t2_part2.dat')
)
parallel 2
as
select owner,object_id,object_name from dba_objects where owner='SYSTEM';
strings命令可以读取卸载得到的二进制文件内容

strings t2_part1.dat

在ocm02创建外部表t3读取t2_part1.dat和t2_part2.dat
scp t2_part1.dat t2_part2.dat ocm02:/home/oracle
sqlplus / as sysdba@ocmdb02
create user ext identified by ext;
grant dba to ext;
conn ext/ext
create directory dir_dmp as '/home/oracle';
grant read,write on directory dir_dmp to public;
加载数据到t3
create table t3 (owner varchar2(100),object_id varchar2(100),object_name varchar2(100))
organization external
(type oracle_datapump
default directory dir_dmp
location ('t2_part1.dat','t2_part2.dat'));
select count(*) from t3;
到此我们的外部表方式数据迁移完成。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to analyze the execution plan of MySQL queryHow to analyze the execution plan of MySQL queryApr 29, 2025 pm 04:12 PM

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

How to use MySQL subquery to improve query efficiencyHow to use MySQL subquery to improve query efficiencyApr 29, 2025 pm 04:09 PM

Subqueries can improve the efficiency of MySQL query. 1) Subquery simplifies complex query logic, such as filtering data and calculating aggregated values. 2) MySQL optimizer may convert subqueries to JOIN operations to improve performance. 3) Using EXISTS instead of IN can avoid multiple rows returning errors. 4) Optimization strategies include avoiding related subqueries, using EXISTS, index optimization, and avoiding subquery nesting.

How to configure the character set and collation rules of MySQLHow to configure the character set and collation rules of MySQLApr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

How to uninstall MySQL and clean residual filesHow to uninstall MySQL and clean residual filesApr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

How to rename a database in MySQLHow to rename a database in MySQLApr 29, 2025 pm 04:00 PM

Renaming a database in MySQL requires indirect methods. The steps are as follows: 1. Create a new database; 2. Use mysqldump to export the old database; 3. Import the data into the new database; 4. Delete the old database.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function