本文介绍如何提高Oracle数据库的查询统计速度。大型数据库系统中往往要用到查询统计,但是对于数据量大的系统,用户在进行复杂的查询统计时往往感到速度很慢,不能满足应用要求,这就要求我们在设计数据库系统时进行合理设置,提高查询统计的速度。本文结合笔者的项目开发经验,阐述具体的设置方法。
以Oracle7.33数据库系统为例,我们在开发大型Oracle数据库系统时结合项目的特点,本着安全、高效的原则对数据库进行了一些物理设计,从而大大提高了数据库的查询统计速度。总结为如下几点:
1)扩大数据表空间到500MB,用于存放本系统的数据;
2)段盘区的初始大小为10KB,增长大小为10KB,增长幅度为1;
3)用户临时空间增大40MB;
4)系统临时表空间和回滚段表空间增大40MB,并且新建4个回滚段;
5)需要经常联结查询,而且数据量又大的库存表、名录表、收发料表放在一簇内;
6)提供定时备份,备份文件放在另外的机器上。
设置数据表空间的SQL语句如下:
CREATE TABLESPACE WXGL_DATA1 DATAFILE 'WXGL_DATA1.ORA' SIZE 500M ONLINE;
增加系统临时表空间和回滚段表空间的SQL语句如下:
ALTER TABLESPACE TEMPORARY_DATA ADD DATAFILE 'TMP2ORCL.ORA' SIZE 40M;
ALTER TABLESPACE ROLLBACK_DATA ADD DATAFILE 'RBS2ORCL.ORA' SIZE 40M;
将数据空间设置在指定的数据文件的SQL语句如下:
CREATE USER ZBGL IDENTIFIED BY ZBGL;
GRANT DBA TO ZBGL;
ALTER USER ZBGL DEFAULT TABLESPACE WXGL_DATA1 TEMPORARY TABLESPACE TEMPORARY_DATA;
设置五个回滚段的SQL语句如下:
SELECT SEGMENT_NAME FROM DBA_ROLLBACK_SEGS WHERE INITIAL_EXTENT < 512000 AND
UPPPER(OWNER) = 'PUBLIC';
SELECT UPPER(STATUS) FROM DBA_ROLLBACK_SEGS WHERE UPPER(SEGMENT_NAME) = ''
ALTER ROLLBACK SEGMENT RB1 OFFLINE;
ALTER ROLLBACK SEGMENT RB2 OFFLINE;
ALTER ROLLBACK SEGMENT RB3 OFFLINE;
ALTER ROLLBACK SEGMENT RB4 OFFLINE;
ALTER ROLLBACK SEGMENT RB5 OFFLINE;
DROP ROLLBACK SEGMENT RB1;
DROP ROLLBACK SEGMENT RB2;
DROP ROLLBACK SEGMENT RB3;
DROP ROLLBACK SEGMENT RB4;
DROP ROLLBACK SEGMENT RB5;
CREATE PUBLIC ROLLBACK SEGMENT RB1 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB2 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB3 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB4 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB5 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
ALTER ROLLBACK SEGMENT RB1 ONLINE;
ALTER ROLLBACK SEGMENT RB2 ONLINE;
ALTER ROLLBACK SEGMENT RB3 ONLINE;
ALTER ROLLBACK SEGMENT RB4 ONLINE;
ALTER ROLLBACK SEGMENT RB5 ONLINE;
COMMIT;
将数据量大的库存表等放在一簇内的SQL语句如下:
KCB='CREATE TABLE QC_KCB( '
+' CKNM NUMBER(8) ,'
+' QCNM NUMBER(10) ,'
+' CKKC NUMBER(12,2),'
+' SNCKKC NUMBER(12,2),'
+' LDJ NUMBER(12,2),'
+' BZ VARCHAR(100),'
+' PRIMARY KEY(CKNM,QCNM))'
+' TABLESPACE WXGL_DATA1 ' ; (大数据量的库存表等放在WXGL_DATA1)
QCFL = 'CREATE TABLE QC_QCFL '
+ '(FLBH NUMBER(2) PRIMARY KEY,'
+ ' FLMC VARCHAR(20) '
+ ' ) '
+' TABLESPACE WXGL_DATA2 ' ;(其他表放在WXGL_DATA2)
系统的基础数据库存表、名录表大约有数据80M;一个单位一般每年收发300次,收发料单大约有数据50M;系统冗余数据100M,系统辅助数据10M;因此,系统总共需要空间大约是240M,现在系统开辟数据空间500M,完全满足存储要求。由于系统使用了冗余数据,在查询常用数据时,避免了多表联结查询的情况,这样,虽然使用了更多的存储空间,但查询效率大幅度提高;同时,系统将需要经常联结查询的数据放在一簇,即将存放这些数据的空间在物理上相邻,这样也使查询速度大大提高。
另外在oracle7.33数据库的Database目录下有一个Initorcl.ora文件,改变其中的设置也可以提高查询统计速度。该文件的内容如下:
#
# $Header: init.ora 1.2 94/10/18 16:12:36 gdudey Osd<desktop/netware> $ init.ora Copyr (c) 1991 Oracle
#
##############################################################################
# Example INIT.ORA file
#
# This file is provided by Oracle Corporation to help you customize
# your RDBMS installation for your site. Important system parameters
# are discussed, and example settings given.
#
# Some parameter settings are generic to any size installation.
# For parameters that require different values in different size
# installations, three scenarios have been provided: SMALL, MEDIUM
# and LARGE. Any parameter that needs to be tuned according to
# installation size will have three settings, each one commented
# according to installation size.
#
# Use the following table to approximate the SGA size needed for the
# three scenarious provided in this file:
#
# -------Installation/Database Size------
# SMALL MEDIUM LARGE
# Block 2K 4500K 6800K 17000K
# Size 4K 5500K 8800K 21000K
#
# To set up a database that multiple instances will be using, place
# all instance-specific parameters in one file, and then have all
# of these files point to a master file using the IFILE command.
# This way, when you change a public
# parameter, it will automatically change on all instances. This is
# necessary, since all instances must run with the same value for many
# parameters. For example, if you choose to use private rollback segments,
# these must be specified in different files, but since all gc_*
# parameters must be the same on all instances, they should be in one file.
#
# INSTRUCTIONS: Edit this file and the other INIT files it calls for
# your site, either by using the values provided here or by providing
# your own. Then place an IFILE= line into each instance-specific
# INIT file that points at this file. ###############################################################################
db_name = oracle
db_files = 20
control_files = C:\ORAWIN95\DATABASE\ctl1orcl.ora
compatible = 7.3.0.0.0
db_file_multiblock_read_count = 8 # INITIAL
# db_file_multiblock_read_count = 8 # SMALL
# db_file_multiblock_read_count = 16 # MEDIUM
# db_file_multiblock_read_count = 32 # LARGE
db_block_buffers = 200 # INITIAL
# db_block_buffers = 200# SMALL
# db_block_buffers = 550 # MEDIUM
# db_block_buffers = 3200 # LARGE
shared_pool_size = 3500000 # INITIAL
# shared_pool_size = 3500000 # SMALL
# shared_pool_size = 6000000 # MEDIUM
# shared_pool_size = 9000000 # LARGE
log_checkpoint_interval = 10000
processes = 50 # INITIAL
# processes = 50 # SMALL
# processes = 100 # MEDIUM
# processes = 200 # LARGE
dml_locks = 100 # INITIAL
# dml_locks = 100 # SMALL
# dml_locks = 200 # MEDIUM
# dml_locks = 500 # LARGE
log_buffer = 8192 # INITIAL
# log_buffer = 8192 # SMALL
# log_buffer = 32768 # MEDIUM
# log_buffer = 163840 # LARGE
sequence_cache_entries = 10 # INITIAL
# sequence_cache_entries = 10 # SMALL
# sequence_cache_entries = 30 # MEDIUM
# sequence_cache_entries = 100# LARGE
sequence_cache_hash_buckets = 10 # INITIAL
# sequence_cache_hash_buckets = 10 # SMALL
# sequence_cache_hash_buckets = 23 # MEDIUM
# sequence_cache_hash_buckets = 89 # LARGE
# audit_trail = true # if you want auditing
# timed_statistics = true # if you want timed statistics
max_dump_file_size = 10240 # limit trace file size to 5 Meg each
# log_archive_start = true # if you want automatic archiving
log_archive_dest=%ORACLE_HOME%\DATABASE\ARCHIVE
# define directories to store trace and alert files
background_dump_dest=%RDBMS73%\trace
user_dump_dest=%RDBMS73%\trace
db_block_size = 2048
snapshot_refresh_processes = 1
remote_login_passwordfile = shared
可以修改该文件的db_file_multiblock_read_count,db_block_buffers,shared_pool_size,processes ,log_buffer,sequence_cache_entries,sequence_cache_hash_buckets等项(文件中均有提示),根据需要和系统使用的数据库服务器的特点适当改大数值,可以提高查询统计速度。这里要注意的是,必须将 log_archive_start = true 项改为False,设置才能生效。
本文介绍的Oracle数据库设置方法均在用户实际使用中经过了严格测试,证明是有效和实用的。
以Oracle7.33数据库系统为例,我们在开发大型Oracle数据库系统时结合项目的特点,本着安全、高效的原则对数据库进行了一些物理设计,从而大大提高了数据库的查询统计速度。总结为如下几点:
1)扩大数据表空间到500MB,用于存放本系统的数据;
2)段盘区的初始大小为10KB,增长大小为10KB,增长幅度为1;
3)用户临时空间增大40MB;
4)系统临时表空间和回滚段表空间增大40MB,并且新建4个回滚段;
5)需要经常联结查询,而且数据量又大的库存表、名录表、收发料表放在一簇内;
6)提供定时备份,备份文件放在另外的机器上。
设置数据表空间的SQL语句如下:
CREATE TABLESPACE WXGL_DATA1 DATAFILE 'WXGL_DATA1.ORA' SIZE 500M ONLINE;
增加系统临时表空间和回滚段表空间的SQL语句如下:
ALTER TABLESPACE TEMPORARY_DATA ADD DATAFILE 'TMP2ORCL.ORA' SIZE 40M;
ALTER TABLESPACE ROLLBACK_DATA ADD DATAFILE 'RBS2ORCL.ORA' SIZE 40M;
将数据空间设置在指定的数据文件的SQL语句如下:
CREATE USER ZBGL IDENTIFIED BY ZBGL;
GRANT DBA TO ZBGL;
ALTER USER ZBGL DEFAULT TABLESPACE WXGL_DATA1 TEMPORARY TABLESPACE TEMPORARY_DATA;
设置五个回滚段的SQL语句如下:
SELECT SEGMENT_NAME FROM DBA_ROLLBACK_SEGS WHERE INITIAL_EXTENT < 512000 AND
UPPPER(OWNER) = 'PUBLIC';
SELECT UPPER(STATUS) FROM DBA_ROLLBACK_SEGS WHERE UPPER(SEGMENT_NAME) = ''
ALTER ROLLBACK SEGMENT RB1 OFFLINE;
ALTER ROLLBACK SEGMENT RB2 OFFLINE;
ALTER ROLLBACK SEGMENT RB3 OFFLINE;
ALTER ROLLBACK SEGMENT RB4 OFFLINE;
ALTER ROLLBACK SEGMENT RB5 OFFLINE;
DROP ROLLBACK SEGMENT RB1;
DROP ROLLBACK SEGMENT RB2;
DROP ROLLBACK SEGMENT RB3;
DROP ROLLBACK SEGMENT RB4;
DROP ROLLBACK SEGMENT RB5;
CREATE PUBLIC ROLLBACK SEGMENT RB1 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB2 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB3 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB4 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
CREATE PUBLIC ROLLBACK SEGMENT RB5 TABLESPACE ROLLBACK_DATA
STORAGE (INITIAL 512000 NEXT 512000 MAXEXTENTS 121);
ALTER ROLLBACK SEGMENT RB1 ONLINE;
ALTER ROLLBACK SEGMENT RB2 ONLINE;
ALTER ROLLBACK SEGMENT RB3 ONLINE;
ALTER ROLLBACK SEGMENT RB4 ONLINE;
ALTER ROLLBACK SEGMENT RB5 ONLINE;
COMMIT;
将数据量大的库存表等放在一簇内的SQL语句如下:
KCB='CREATE TABLE QC_KCB( '
+' CKNM NUMBER(8) ,'
+' QCNM NUMBER(10) ,'
+' CKKC NUMBER(12,2),'
+' SNCKKC NUMBER(12,2),'
+' LDJ NUMBER(12,2),'
+' BZ VARCHAR(100),'
+' PRIMARY KEY(CKNM,QCNM))'
+' TABLESPACE WXGL_DATA1 ' ; (大数据量的库存表等放在WXGL_DATA1)
QCFL = 'CREATE TABLE QC_QCFL '
+ '(FLBH NUMBER(2) PRIMARY KEY,'
+ ' FLMC VARCHAR(20) '
+ ' ) '
+' TABLESPACE WXGL_DATA2 ' ;(其他表放在WXGL_DATA2)
系统的基础数据库存表、名录表大约有数据80M;一个单位一般每年收发300次,收发料单大约有数据50M;系统冗余数据100M,系统辅助数据10M;因此,系统总共需要空间大约是240M,现在系统开辟数据空间500M,完全满足存储要求。由于系统使用了冗余数据,在查询常用数据时,避免了多表联结查询的情况,这样,虽然使用了更多的存储空间,但查询效率大幅度提高;同时,系统将需要经常联结查询的数据放在一簇,即将存放这些数据的空间在物理上相邻,这样也使查询速度大大提高。
另外在oracle7.33数据库的Database目录下有一个Initorcl.ora文件,改变其中的设置也可以提高查询统计速度。该文件的内容如下:
#
# $Header: init.ora 1.2 94/10/18 16:12:36 gdudey Osd<desktop/netware> $ init.ora Copyr (c) 1991 Oracle
#
##############################################################################
# Example INIT.ORA file
#
# This file is provided by Oracle Corporation to help you customize
# your RDBMS installation for your site. Important system parameters
# are discussed, and example settings given.
#
# Some parameter settings are generic to any size installation.
# For parameters that require different values in different size
# installations, three scenarios have been provided: SMALL, MEDIUM
# and LARGE. Any parameter that needs to be tuned according to
# installation size will have three settings, each one commented
# according to installation size.
#
# Use the following table to approximate the SGA size needed for the
# three scenarious provided in this file:
#
# -------Installation/Database Size------
# SMALL MEDIUM LARGE
# Block 2K 4500K 6800K 17000K
# Size 4K 5500K 8800K 21000K
#
# To set up a database that multiple instances will be using, place
# all instance-specific parameters in one file, and then have all
# of these files point to a master file using the IFILE command.
# This way, when you change a public
# parameter, it will automatically change on all instances. This is
# necessary, since all instances must run with the same value for many
# parameters. For example, if you choose to use private rollback segments,
# these must be specified in different files, but since all gc_*
# parameters must be the same on all instances, they should be in one file.
#
# INSTRUCTIONS: Edit this file and the other INIT files it calls for
# your site, either by using the values provided here or by providing
# your own. Then place an IFILE= line into each instance-specific
# INIT file that points at this file. ###############################################################################
db_name = oracle
db_files = 20
control_files = C:\ORAWIN95\DATABASE\ctl1orcl.ora
compatible = 7.3.0.0.0
db_file_multiblock_read_count = 8 # INITIAL
# db_file_multiblock_read_count = 8 # SMALL
# db_file_multiblock_read_count = 16 # MEDIUM
# db_file_multiblock_read_count = 32 # LARGE
db_block_buffers = 200 # INITIAL
# db_block_buffers = 200# SMALL
# db_block_buffers = 550 # MEDIUM
# db_block_buffers = 3200 # LARGE
shared_pool_size = 3500000 # INITIAL
# shared_pool_size = 3500000 # SMALL
# shared_pool_size = 6000000 # MEDIUM
# shared_pool_size = 9000000 # LARGE
log_checkpoint_interval = 10000
processes = 50 # INITIAL
# processes = 50 # SMALL
# processes = 100 # MEDIUM
# processes = 200 # LARGE
dml_locks = 100 # INITIAL
# dml_locks = 100 # SMALL
# dml_locks = 200 # MEDIUM
# dml_locks = 500 # LARGE
log_buffer = 8192 # INITIAL
# log_buffer = 8192 # SMALL
# log_buffer = 32768 # MEDIUM
# log_buffer = 163840 # LARGE
sequence_cache_entries = 10 # INITIAL
# sequence_cache_entries = 10 # SMALL
# sequence_cache_entries = 30 # MEDIUM
# sequence_cache_entries = 100# LARGE
sequence_cache_hash_buckets = 10 # INITIAL
# sequence_cache_hash_buckets = 10 # SMALL
# sequence_cache_hash_buckets = 23 # MEDIUM
# sequence_cache_hash_buckets = 89 # LARGE
# audit_trail = true # if you want auditing
# timed_statistics = true # if you want timed statistics
max_dump_file_size = 10240 # limit trace file size to 5 Meg each
# log_archive_start = true # if you want automatic archiving
log_archive_dest=%ORACLE_HOME%\DATABASE\ARCHIVE
# define directories to store trace and alert files
background_dump_dest=%RDBMS73%\trace
user_dump_dest=%RDBMS73%\trace
db_block_size = 2048
snapshot_refresh_processes = 1
remote_login_passwordfile = shared
可以修改该文件的db_file_multiblock_read_count,db_block_buffers,shared_pool_size,processes ,log_buffer,sequence_cache_entries,sequence_cache_hash_buckets等项(文件中均有提示),根据需要和系统使用的数据库服务器的特点适当改大数值,可以提高查询统计速度。这里要注意的是,必须将 log_archive_start = true 项改为False,设置才能生效。
本文介绍的Oracle数据库设置方法均在用户实际使用中经过了严格测试,证明是有效和实用的。