Home Database Mysql Tutorial 三招让Oracle表列管理更加简单

三招让Oracle表列管理更加简单

Jun 07, 2016 pm 04:54 PM
o oracle database

有时候出于改善数据库表的性能等目的出发,需要对Oralce数据库的表进行重新组织。如在Oracle数据库中,如果对表进行频繁的DM

  Oracle数据库是目前为止最复杂的一个数据库之一。也正是这种复杂性,让Oracle数据库能够面对企业各种各样的需求。不过大部分时候,数据库管理员掌握一些技巧,可以让数据库维护工作变得简单。笔者这里就以Oracle数据库中表列的管理为例,谈谈这方面的问题。

  一、 把列设置为UNUSED状态,代替删除。

  当数据库部署完成之后,即使在数据库使用过程中,数据库管理员仍然可以对数据库的表进行维护,如删除列等等。删除列将删除表中每条记录的相应列的值,同时释放其所占用的存储空间。所以如果要删除一个大表中的列时,由于其必须对每条记录都进行相应的处理,为此这个删除列的操作会占用比较长的时间。如有个数据库管理员一次在设计一个人员管理系统时,一开始是把人员的住址跟人员信息表放置同一个表中。可是后来证明这个设计是错误的。一方面因为人员住址的字段比较长,而且有些由于住址难以确定,这个字段还是空的。另一方面,一个人员其可能住址变更了好几次,而出于管理的需要,又要保存起以前的住址信息。为此最后一个人员可能同时对应多个住址信息。所以后来数据库管理员调整了设计,另外建立了一张表,然后利用人员编号把它们关联起来。由于那时表中的纪录已经比较多了,如果要删除列的话,那么可能这个删除作业需要执行时间比较长。

  为了避免在数据库使用高峰期间由于执行删除列的操作而占用过多的系统资源(而且时间比较长),为此笔者建议不要马上采用DROP关键字来删除列。而可以先用UNUSED关键字把某个列设置为不活跃状态。如可以利用命令ALTER TABLE ADRESS SET UNUSED,把某个列设置为不活跃。如此设置之后,从用户的角度来看,被设置为UNUSED状态的列于被直接删除的列之间是没有任何区别的。用户无法通过查询或者在数据字典中看到这些列。而且即使在表中,也可以插入相同列名的列。简单的说,对于用户来说,这个设置为UNUSED的列就好像删除了一样。但是从数据库角度来说,在是不一样的。其这个列只是别设置为UNUNSED列,但是在数据库中仍然是存在的。也就是说,这个列所占用的存储空间没有被释放。为此即使在数据库运行的高峰时期,,为列加入这个标记也不会占用多少的时间和系统资源。

  为此,笔者的意见是,当数据库比较繁忙时而且数据库表中的纪录又比较多,则可以不从物理上删除这个列,而先把这个列标记为UNUSED状态。这可以减少因为删除列而给数据库正常使用带来的负面影响。虽然这要牺牲一点硬盘空间,可是现在硬盘便宜。为了数据库的性能,牺牲这点硬盘空间是值得的。等到数据库比较空的时候,再把这些列删除即可。这对于用户来说,是不会受到任何影响的。这只是在技术处理上的问题。

  为了便于管理这些UNUSED列,在Oracle数据库中还提供了一张视图(这张视图的名字为USER_UNUSED_COL_TABS),帮助数据库管理员来管理这些列。通过查询这张视图,数据库管理员可以了解数据库哪些表中存在UNUSED列。然后数据库管理员可以根据实际情况,一个月或者一年来清除一下。这不仅可以提高数据库的运行效率,而且也不会因为删除列的操作影响到用户的正常使用。

  二、 给列表添加相关的注释。

  俗话说,好记性不如烂笔头。在数据库设计的时候,给表或者列添加一些必要的注释,可以提高其可读性,也有利于后续的维护与升级。像笔者这种专业的数据库设计与开发人员,往往一个人需要负责很多项目,即同时要负责多个企业的数据库软件。当项目数量一多,一年后可能就不知道某个数据库的某张表到底用来做什么用途。即使采用了比较合适的表名字编码或者列名字编码规则,但是凭借这些简单的代码,仍然不能够直观的反应出这些代码的含义。为此,在必要的时候,我们往往需要给表或者列添加相关的注释,方便后续对其进行维护与升级。

  为了实现这个目的,我们可以利用COMMENT关键字。如COMMENT ON TABLE 表名 IS ‘注释内容’即可。在使用这个命令的时候需要注意的是,注释中的内容必须利用单引号括起来,而不是使用双引号。因为如果注释中含有特殊字符的话,如单引号、通配符等等,最好能够采用其他的字符代替。如果非要用这些字符的话,那么就需要采用转义字符,让数据库系统认为这是普通的字符。通常情况下,注释的内容可以多大4000个字节,足够数据库管理员用来表述表的用途。另外在注释的内容中可以包含标点符号、空格字符、特殊字符(不建议使用)等等。而且还可以跨越多个物理行,以提高可读性等等。不过虽然其提供了长达4000个字节的说明,但是数据库管理员在写注释的时候,最好能够短话短说。不然的话,以后要花大量的时间来读这些备注的内容,可不是一件好差事。未必笔者认为,在写这个表注释的时候,最好能够点到为止,而不要想写项目说明书那样写的面面俱到。

linux

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

See all articles