前段时间,需要从异地一个测试数据库中将测试数据(一张表)导入本地库,表数据量大约500万,字段160多个,开始用了exp/imp方式,速
背景:
前段时间,需要从异地一个测试数据库中将测试数据(一张表)导入本地库,表数据量大约500万,字段160多个,开始用了exp/imp方式,速度奇慢,不能忍,于是转而使用expdp/impdp方式。
expdp/impd介绍:
从10g开始,除了传统的exp/imp导入导出工具外,Oracle提供了expdp/impdp的数据泵导入导出工具。
从上看,Oracle数据泵由三部分组成:
>The command-line clients, expdp and impdp
>The DBMS_DATAPUMP PL/SQL package (also known as the Data Pump API)
>The DBMS_METADATA PL/SQL package (also known as the Metadata API)
The Data Pump clients, expdp and impdp, invoke the Data Pump Export utility and Data Pump Import utility, respectively.
数据泵客户端(expdp/impdp),分别会调用数据泵Data Pump Export/Import Utility应用工具。
The expdp and impdp clients use the procedures provided in the DBMS_DATAPUMP PL/SQL package to execute export and import commands, using the parameters entered at the command line. These parameters enable the exporting and importing of data and metadata for a complete database or for subsets of a database.
expdp/impdp客户端使用由DBMS_DATAPUMP PL/SQL包提供的存储过程来执行export/import命令,并且可以在命令行中添加参数,这些参数可以导入导出数据库中的数据和元数据或其中的一部分。
When metadata is moved, Data Pump uses functionality provided by the DBMS_METADATA PL/SQL package. The DBMS_METADATA package provides a centralized facility for the extraction, manipulation, and re-creation of dictionary metadata.
如果需要导入导出元数据,数据泵会使用DBMS_METADATA PL/SQL包提供的函数。DBMS_METADATA包会提供便捷的方法,用于抽取、控制和重建数据字典元数据。
The DBMS_DATAPUMP and DBMS_METADATA PL/SQL packages can be used independently of the Data Pump clients.
DBMS_DATAPUMP和DBMS_METADATA的PL/SQL包可以独立于数据泵客户端使用。
All Data Pump Export and Import processing, including the reading and writing of dump files, is done on the system (server) selected by the specified database connect string. This means that for unprivileged users, the database administrator (DBA) must create directory objects for the Data Pump files that are read and written on that server file system. (For security reasons, DBAs must ensure that only approved users are allowed access to directory objects.) For privileged users, a default directory object is available. See "Default Locations for Dump, Log, and SQL Files" for more information about directory objects.
所有的数据泵导入导出过程,包括读取和写入的dump文件,都在数据库服务器(server)上。这意味着对于非授权用户,DBA必须为数据泵文件创建服务器端可读写的目录对象,处于安全考虑,DBA必须确保只有授权用户可以访问这些目录对象。对于授权用户,可以使用默认的目录对象。
总结:
数据泵工具expdp/impdp工具,执行时其实是调用的两个PL/SQL包(一个对应数据,一个对应元数据),读取和写入的dump文件存储于服务器上directory目录对应的文件夹中。
问题来了,如果没有服务器的账号,即使可以导出,但如何获取到导出的dump呢?
现在的需求是,从本地服务器使用expdp从远程服务器导出一张表的数据,然后使用impdp导入到本地库,且没有远程服务器的登录账号。
一个远程数据表的导入导出问题,当然这里使用exp/imp完全可以做到,现在看看expdp/impdp如何做。
解决方案:通过dblink实现远程数据的导入导出。
1. 本地库创建dblink指向远程库。
create database link gf_local connect to username identified by password using 'tnsname';
2. 本地库创建directory目录对象。
create directory expdp_dir as '/home/oracle11g/expdp_dir/';
3. 本地库创建数据泵导出参数文件。
vi exp.par:
userid=username/password //本地库的用户名密码,可用上面创建的dblink和directory目录。
directory=expdp_dir //本地库创建的directory目录。
dumpfile=gf_expdp.dump //dump文件名。
logfile=gf_expdp.log //日志文件名。
tables=depkf15.t_cp //要导出的表。
query='"where rownum
network_link=gf_local //dblink名。
注意,这里有个小问题值得注意,tables=depkf15.t_cp,如果待导出表和登录用户不同,则这里需要添加导出的对象schema(用户),否则会报错。
4. 执行导出命令。
expdp parfile=exp.par
开始报错了,提示:
ORA-31631:需要权限
ORA-39149:无法将授权用户链接到非授权用户
此时需要授予远程数据库用户exp_full_database权限:
GRANT exp_full_database TO username;
接下来就是等待了,dump文件会存储至本地的expdp_dir指向路径下。
总结:
1. expdp/impdp是10g以上提供的数据泵工具,运行时会调用两个PL/SQL包,,当然也可以直接运行这两个PL/SQL包,没试过,如果大家试过,也可以分享出来。
2. 导入数据库账户需要有imp_full_database权限,导出数据库账户需要有exp_full_database权限。
3. expdp比exp更优,还有一些地方,比如从help=y可以看到expdp有更多的参数可选,其中expdp有COMPRESSION压缩参数可选,解释如下:
Reduce the size of a dump file.
Valid keyword values are: ALL, DATA_ONLY, [METADATA_ONLY] and NONE.
可参考secooler的这篇文章(),比对了四种参数的压缩比。
4. 由于我的实验中,远程库和本地库之间网络不是很好,因此实际效果上,exp和expdp都比较慢,也是因为用了dblink的方式,就没再深入了,这里只是为了说明expdp/impdp如何实现远程数据库的导出。
后经同事测试,一个800MB的文件,使用exp导出大约用了1小时,expdp只需要不到10分钟,请注意,这里未使用COMPRESSION参数。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.