Oracle最终篇(oracle最后一天函数)
sinye56 2024-09-23 23:51 6 浏览 0 评论
1触发器
1.1语法
【语法】 CREATE [OR REPLACE] TRIGGER <触发器名> BEFORE|AFTER INSERT|DELETE|UPDATE [OF <列名>] ON <表名> [FOR EACH ROW] <pl/sql块> 【说明】 关键字"BEFORE"在操作完成前触发;"AFTER"则是在操作完成后触发; 关键字"FOR EACH ROW"指定触发器每行触发一次,若不指定则为表级触发器. 关键字"OF <列名>" 不写表示对整个表的所有列. pl/sql块中不能使用commit; 【特殊变量】 :new --为一个引用最新的行值; :old --为一个引用以前的行值; 这些变量只有在使用了关键字 "FOR EACH ROW"时才存在.且update语句两个都有,而insert只有:new ,delect 只有:old; |
1.2行级触发器
【示例1】涨工资
/* 触发器使用:给员工涨工资(涨后工资应该大于涨前)后,在后台输出更新前和更新后的工资 */ createorreplacetrigger tri_emp_upd_sal after updateof sal on emp foreachrow begin if:old.sal <:new.sal then dbms_output.put_line('更新前工资为:'||:old.sal||',更新后工资为:'||:new.sal); else raise_application_error(-20002,'工资不能越涨越低!'); endif; end; / -- 更新工资值,并触发行级触发器 update emp set sal =8888where empno =1002; |
【示例2】触发器+序列实现主键自增长
/* 触发器使用:给emp表的empno添加触发器,在插入记录时自动填入值 */ -- 1、创建序列 createsequence seq_emp_empno; -- 2、创建触发器 createorreplacetrigger tri_emp_ins_empno before inserton emp foreachrow begin -- 给将要插入表的记录:new 中的empno设置sequence中的值 select seq_emp_empno.nextval into:new.empno from dual; end; / -- 新增员工数据,测试触发器+序列的组合使用 insertinto emp(ename,sal)values('itcast002',2000); commit; |
1.3表级触发器
/* 触发器使用:删除表的同时备份表数据到另一张备份表 */ -- 1、从emp表结果中创建一张表并复制数据 createtable emp2 asselect*from emp; -- 2、创建备份表emp_bak createtable emp_bak asselect*from emp2 where1=2; -- 3、创建表触发器,当对表操作时触发 createorreplacetrigger tri_emp2_del before deleteon emp2 begin -- 将emp2表中的数据备份到emp_bak insertinto emp_bak select*from emp2; end; / -- 4、测试删除emp2表的数据 deletefrom emp2; select*from emp2; select*from emp_bak; |
1.4开启禁用触发器
【禁用某个触发器】 ALTER TRIGGER <触发器名> DISABLE 【示例】 altertrigger tri_emp_upd_sal disable; update emp set sal =8888where empno =1002; 【重新启用触发器】 ALTER TRIGGER <触发器名> ENABLE 【示例】 altertrigger tri_emp_upd_sal enable; update emp set sal =8888where empno =1002; 【禁用表的所有触发器】 ALTER TABLE <表名> DISABLE ALL TRIGGERS; 【示例】 altertable emp disablealltriggers; 【启用表的所有触发器】 ALTER TABLE <表名> ENABLE ALL TRIGGERS; 【示例】 altertable emp enablealltriggers; 【删除触发器】 DROP TRIGGER <触发器名>; 【示例】 droptrigger tri_emp_upd_sal; |
2数据字典
Oracle数据字典中,对象名称多数以"USER.","ALL.","DBA."。前缀"USER."视图中记录通常记录执行查询的帐户所拥有的对象的信息,"ALL."视图中记录包括"USER"记录和授权至PUBLIC或用户的对象的信息,"DBA."视图包含所有数据库对象,而不管其所有者。
视图名 | 描述 |
ALL_CATALOG | All tables, views, synonyms, sequences accessible to the user |
ALL_COL_COMMENTS | Comments on columns of accessible tables and views |
ALL_COL_GRANTS_MADE | Grants on columns for which the user is owner or grantor |
ALL_COL_GRANTS_RECD | Grants on columns for which the user or PUBLIC is the grantee |
ALL_COL_PRIVS | Grants on columns for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is the grantee |
ALL_COL_PRIVS_MADE | Grants on columns for which the user is owner or grantor |
ALL_COL_PRIVS_RECD | Grants on columns for which the user, PUBLIC or enabled role is the grantee |
ALL_CONSTRAINTS | Constraint definitions on accessible tables |
ALL_CONS_COLUMNS | Information about accessible columns in constraint definitions |
ALL_DB_LINKS | Database links accessible to the user |
ALL_DEF_AUDIT_OPTS | Auditing options for newly created objects |
ALL_DEPENDENCIES | Dependencies to and from objects accessible to the user |
ALL_ERRORS | Current errors on stored objects that user is allowed to create |
ALL_INDEXES | Descriptions of indexes on tables accessible to the user |
ALL_IND_COLUMNS | COLUMNs comprising INDEXes on accessible TABLES |
ALL_OBJECTS | Objects accessible to the user |
ALL_REFRESH | All the refresh groups that the user can touch |
ALL_REFRESH_CHILDREN | All the objects in refresh groups, where the user can touch the group |
ALL_SEQUENCES | Description of SEQUENCEs accessible to the user |
ALL_SNAPSHOTS | Snapshots the user can look at |
ALL_SOURCE | Current source on stored objects that user is allowed to create |
ALL_SYNONYMS | All synonyms accessible to the user |
ALL_TABLES | Description of tables accessible to the user |
ALL_TAB_COLUMNS | Columns of all tables, views and clusters |
ALL_TAB_COMMENTS | Comments on tables and views accessible to the user |
ALL_TAB_GRANTS_MADE | User's grants and grants on user's objects |
ALL_TAB_GRANTS_RECD | Grants on objects for which the user or PUBLIC is the grantee |
ALL_TAB_PRIVS | Grants on objects for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is the grantee |
ALL_TAB_PRIVS_MADE | User's grants and grants on user's objects |
ALL_TAB_PRIVS_RECD | Grants on objects for which the user, PUBLIC or enabled role is the grantee |
ALL_TRIGGERS | Triggers accessible to the current user |
ALL_TRIGGER_COLS | Column usage in user's triggers or in triggers on user's tables |
ALL_USERS | Information about all users of the database |
ALL_VIEWS | Text of views accessible to the user |
USER_AUDIT_CONNECT | Audit trail entries for user logons/logoffs |
USER_AUDIT_OBJECT | Audit trail records for statements concerning objects, specifically: table, cluster, view, index, sequence, [public] database link, [public] synonym, procedure, trigger, rollback segment, tablespace, role, user |
USER_AUDIT_SESSION | |
USER_AUDIT_STATEMENT | Audit trail records concerning grant, revoke, audit, noaudit and alter system |
USER_AUDIT_TRAIL | Audit trail entries relevant to the user |
USER_CATALOG | Tables, Views, Synonyms and Sequences owned by the user |
USER_CLUSTERS | Descriptions of user's own clusters |
USER_CLU_COLUMNS | Mapping of table columns to cluster columns |
USER_COL_COMMENTS | Comments on columns of user's tables and views |
USER_COL_GRANTS | Grants on columns for which the user is the owner, grantor or grantee |
USER_COL_GRANTS_MADE | All grants on columns of objects owned by the user |
USER_COL_GRANTS_RECD | Grants on columns for which the user is the grantee |
USER_COL_PRIVS | Grants on columns for which the user is the owner, grantor or grantee |
USER_COL_PRIVS_MADE | All grants on columns of objects owned by the user |
USER_COL_PRIVS_RECD | Grants on columns for which the user is the grantee |
USER_CONSTRAINTS | Constraint definitions on user's own tables |
USER_CONS_COLUMNS | Information about accessible columns in constraint definitions |
USER_CROSS_REFS | Cross references for user's views and synonyms |
USER_DB_LINKS | Database links owned by the user |
USER_DEPENDENCIES | Dependencies to and from a users objects |
USER_ERRORS | Current errors on stored objects owned by the user |
USER_EXTENTS | Extents comprising segments owned by the user |
USER_FREE_SPACE | Free extents in tablespaces accessible to the user |
USER_INDEXES | Description of the user's own indexes |
USER_IND_COLUMNS | COLUMNs comprising user's INDEXes or on user's TABLES |
USER_JOBS | All jobs owned by this user |
USER_OBJECTS | Objects owned by the user |
USER_OBJECT_SIZE | Sizes, in bytes, of various pl/sql objects |
USER_OBJ_AUDIT_OPTS | Auditing options for user's own tables and views |
USER_REFRESH | All the refresh groups |
USER_REFRESH_CHILDREN | All the objects in refresh groups, where the user owns the refresh group |
USER_RESOURCE_LIMITS | Display resource limit of the user |
USER_ROLE_PRIVS | Roles granted to current user |
USER_SEGMENTS | Storage allocated for all database segments |
USER_SEQUENCES | Description of the user's own SEQUENCEs |
USER_SNAPSHOTS | Snapshots the user can look at |
USER_SNAPSHOT_LOGS | All snapshot logs owned by the user |
USER_SOURCE | Source of stored objects accessible to the user |
USER_SYNONYMS | The user's private synonyms |
USER_SYS_PRIVS | System privileges granted to current user |
USER_TABLES | Description of the user's own tables |
USER_TABLESPACES | Description of accessible tablespaces |
USER_TAB_AUDIT_OPTS | Auditing options for user's own tables and views |
USER_TAB_COLUMNS | Columns of user's tables, views and clusters |
USER_TAB_COMMENTS | Comments on the tables and views owned by the user |
USER_TAB_PRIVS | Grants on objects for which the user is the owner, grantor or grantee |
USER_TAB_PRIVS_MADE | All grants on objects owned by the user |
USER_TAB_PRIVS_RECD | Grants on objects for which the user is the grantee |
USER_TRIGGERS | Triggers owned by the user |
USER_TRIGGER_COLS | Column usage in user's triggers |
USER_TS_QUOTAS | Tablespace quotas for the user |
USER_USERS | Information about the current user |
USER_VIEWS | Text of views owned by the user |
AUDIT_ACTIONS | Description table for audit trail action type codes. Maps action type numbers to action type names |
COLUMN_PRIVILEGES | Grants on columns for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is the grantee |
DICTIONARY | Description of data dictionary tables and views |
DICT_COLUMNS | Description of columns in data dictionary tables and views |
DUAL | |
GLOBAL_NAME | global database name |
INDEX_HISTOGRAM | statistics on keys with repeat count |
INDEX_STATS | statistics on the b-tree |
RESOURCE_COST | Cost for each resource |
ROLE_ROLE_PRIVS | Roles which are granted to roles |
ROLE_SYS_PRIVS | System privileges granted to roles |
ROLE_TAB_PRIVS | Table privileges granted to roles |
SESSION_PRIVS | Privileges which the user currently has set |
SESSION_ROLES | Roles which the user currently has enabled. |
TABLE_PRIVILEGES | Grants on objects for which the user is the grantor, grantee, owner, or an enabled role or PUBLIC is the grantee |
ACCESSIBLE_COLUMNS | Synonym for ALL_TAB_COLUMNS |
ALL_COL_GRANTS | Synonym for COLUMN_PRIVILEGES |
ALL_JOBS | Synonym for USER_JOBS |
ALL_TAB_GRANTS | Synonym for TABLE_PRIVILEGES |
CAT | Synonym for USER_CATALOG |
CLU | Synonym for USER_CLUSTERS |
COLS | Synonym for USER_TAB_COLUMNS |
DBA_AUDIT_CONNECT | Synonym for USER_AUDIT_CONNECT |
DBA_AUDIT_RESOURCE | Synonym for USER_AUDIT_RESOURCE |
DBA_REFRESH_CHILDREN | Synonym for USER_REFRESH_CHILDREN |
DICT | Synonym for DICTIONARY |
IND | Synonym for USER_INDEXES |
OBJ | Synonym for USER_OBJECTS |
SEQ | Synonym for USER_SEQUENCES |
SM$VERSION | Synonym for SM_$VERSION |
SYN | Synonym for USER_SYNONYMS |
TABS | Synonym for USER_TABLES |
V$ACCESS | Synonym for V_$ACCESS |
V$ARCHIVE | Synonym for V_$ARCHIVE |
V$BACKUP | Synonym for V_$BACKUP |
V$BGPROCESS | Synonym for V_$BGPROCESS |
V$CIRCUIT | Synonym for V_$CIRCUIT |
V$COMPATIBILITY | Synonym for V_$COMPATIBILITY |
V$COMPATSEG | Synonym for V_$COMPATSEG |
V$CONTROLFILE | Synonym for V_$CONTROLFILE |
V$DATABASE | Synonym for V_$DATABASE |
V$DATAFILE | Synonym for V_$DATAFILE |
V$DBFILE | Synonym for V_$DBFILE |
V$DBLINK | Synonym for V_$DBLINK |
V$DB_OBJECT_CACHE | Synonym for V_$DB_OBJECT_CACHE |
V$DISPATCHER | Synonym for V_$DISPATCHER |
V$ENABLEDPRIVS | Synonym for V_$ENABLEDPRIVS |
V$FILESTAT | Synonym for V_$FILESTAT |
V$FIXED_TABLE | Synonym for V_$FIXED_TABLE |
V$LATCH | Synonym for V_$LATCH |
V$LATCHHOLDER | Synonym for V_$LATCHHOLDER |
V$LATCHNAME | Synonym for V_$LATCHNAME |
V$LIBRARYCACHE | Synonym for V_$LIBRARYCACHE |
V$LICENSE | Synonym for V_$LICENSE |
V$LOADCSTAT | Synonym for V_$LOADCSTAT |
V$LOADTSTAT | Synonym for V_$LOADTSTAT |
V$LOCK | Synonym for V_$LOCK |
V$LOG | Synonym for V_$LOG |
V$LOGFILE | Synonym for V_$LOGFILE |
V$LOGHIST | Synonym for V_$LOGHIST |
V$LOG_HISTORY | Synonym for V_$LOG_HISTORY |
V$MLS_PARAMETERS | Synonym for V_$MLS_PARAMETERS |
V$MTS | Synonym for V_$MTS |
V$NLS_PARAMETERS | Synonym for V_$NLS_PARAMETERS |
V$NLS_VALID_VALUES | Synonym for V_$NLS_VALID_VALUES |
V$OPEN_CURSOR | Synonym for V_$OPEN_CURSOR |
V$OPTION | Synonym for V_$OPTION |
V$PARAMETER | Synonym for V_$PARAMETER |
V$PQ_SESSTAT | Synonym for V_$PQ_SESSTAT |
V$PQ_SLAVE | Synonym for V_$PQ_SLAVE |
V$PQ_SYSSTAT | Synonym for V_$PQ_SYSSTAT |
V$PROCESS | Synonym for V_$PROCESS |
V$QUEUE | Synonym for V_$QUEUE |
V$RECOVERY_LOG | Synonym for V_$RECOVERY_LOG |
V$RECOVER_FILE | Synonym for V_$RECOVER_FILE |
V$REQDIST | Synonym for V_$REQDIST |
V$RESOURCE | Synonym for V_$RESOURCE |
V$ROLLNAME | Synonym for V_$ROLLNAME |
V$ROLLSTAT | Synonym for V_$ROLLSTAT |
V$ROWCACHE | Synonym for V_$ROWCACHE |
V$SESSION | Synonym for V_$SESSION |
V$SESSION_CURSOR_CACHE | Synonym for V_$SESSION_CURSOR_CACHE |
V$SESSION_EVENT | Synonym for V_$SESSION_EVENT |
V$SESSION_WAIT | Synonym for V_$SESSION_WAIT |
V$SESSTAT | Synonym for V_$SESSTAT |
V$SESS_IO | Synonym for V_$SESS_IO |
V$SGA | Synonym for V_$SGA |
V$SGASTAT | Synonym for V_$SGASTAT |
V$SHARED_SERVER | Synonym for V_$SHARED_SERVER |
V$SQLAREA | Synonym for V_$SQLAREA |
V$STATNAME | Synonym for V_$STATNAME |
V$SYSSTAT | Synonym for V_$SYSSTAT |
V$SYSTEM_CURSOR_CACHE | Synonym for V_$SYSTEM_CURSOR_CACHE |
V$SYSTEM_EVENT | Synonym for V_$SYSTEM_EVENT |
V$THREAD | Synonym for V_$THREAD |
V$TIMER | Synonym for V_$TIMER |
V$TRANSACTION | Synonym for V_$TRANSACTION |
V$TYPE_SIZE | Synonym for V_$TYPE_SIZE |
V$VERSION | Synonym for V_$VERSION |
V$WAITSTAT | Synonym for V_$WAITSTAT |
V$_LOCK | Synonym for V_$_LOCK |
3角色
Oracle提供了三种标准的角色(role):CONNECT、RESOURCE和DBA。
1. CONNECT Role(连接角色)
临时用户,特别是那些不需要建表的用户,通常只赋予他们CONNECT role。CONNECT是使用Oracle的简单权限,这种权限只有在对其他用户的表有访问权时,包括select、insert、update和delete等,才会变得有意义。
2. RESOURCE Role(资源角色)
更可靠和正式的数据库用户可以授予RESOURCE role。RESOURCE提供给用户另外的权限以创建他们自己的表、序列、过程、触发器、索引和簇。
3. DBA Role(数据库管理员角色)
DBA role拥有所有的系统权限--包括无限制的空间限额和给其他用户授予各种权限的能力。
除此以上角色外;还可以自行创建角色。用户创建的role可以由表或系统权限或两者的组合构成。为了创建role,用户必须具有CREATE ROLE系统权限。
3.1创建角色
创建角色后,可以对角色授予权限;授权的语法和前面授权给用户的语法相同。
【语法】 CREATE ROLE <role_name>; 【示例】 -- system 用户登录,授予itcast 创建角色的权限 grantcreateroleto itcast; -- 创建角色 createrole role_itcast; -- 授予emp的select 操作权限给role_itcast角色 grantselecton emp to role_itcast; -- 给scott用户授予role_itcast的角色 grant role_itcast to scott; |
3.2删除角色
【语法】 DROP ROLE <role_name>; 【示例】 droprole role_itcast; |
4闪回
4.1闪回简介
在Oracle的操作工程中,会不可避免地出现操作失误或者用户失误,例如不小心删除了一个表等,这些失误和错误可能会造成重要数据的丢失,最终导致Oracle数据库停止。
在传统操作上,当发生数据丢失、数据错误问题时,解决的主要办法是数据的导入导出、备份恢复技术,这些方法都需要在发生错误前,有一个正确的备份才能进行恢复。为了减少这方面的损失,Oracle提供了闪回技术。有了闪回技术,就可以实现数据的快速恢复,而且不需要数据备份。
闪回特点:
传统的恢复技术缓慢:它是整个数据库或者一个文件恢复,不只恢复损坏的数据在数据库日志中每个修改都必须被检查;
闪回速度快:通过行和事务把改变编入索引,仅仅改变了的数据会被恢复;
闪回命令容易,没有复杂步骤。
4.2闪回类型
主要有三种闪回:闪回表(flashback table)、闪回删除(flashback drop)、闪回数据库(flashback database);一般情况下对数据库的闪回需要配置闪回数据库,然后自动产生闪回日志;再根据闪回日志恢复数据库。
4.3闪回查询
根据闪回日志可以快速查询在某个时间点的数据。
--查看10秒之前的emp表 select * from emp as of timestamp sysdate - interval'10'second; select * from emp as of scntimestamp_to_scn(sysdate - interval'10'second); 【说明】 as of timestamp 是固定写法,查询某个时间点对应的数据 as of scn查询某scn对应的数据 sysdate – interval ‘10’second 是时间值的计算 --通过查询某个时间的数据来更新现有数据 --将7499员工的姓名更新为5分钟之前的姓名 update emp e set ename = (select ename from emp as of timestamp systimestamp - interval'5'minute where empno=e.empno) where empno=7499; |
4.4闪回表
闪回表(flashback table)实际上是将表中的数据快速恢复到过去的一个焦点或者系统改变号SCN上;对进行表闪回的表必须row movement为enable。
SCN: System Change Number.
实现表的闪回,需要使用到与撤销表空间相关的undo信息,通过show parameter undo命令可以了解这些信息。
conn sys/orcl as sysdba
show parameters undo; // undo表空间
alter system set undo_retention=1200 scope=both;
undo_retention:数据保留时间长度(默认是900秒)
scope参数的值:
momory-当前session中有效
spfile: 修改配置文件,但当前会话中无效
both:当前会话有效,同时修改配置文件
undo表空间:保存了所有的操作记录(2G的空间) 因为有了该表空间才可以进行闪回
【语法】 flashback table [schema.]table_name[,...n] to {[scn] | [timestamp] [[enable | disable] triggers]}; 【说明】 scn:表示通过系统改变号进行闪回;scn系统改变号一般和系统时间相对应;查看当前系统时间和所对应系统scn: select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), timestamp_to_scn(sysdate)from dual; timestamp:表示通过时间戳的形式来进行闪回; enable|disable triggers:表示触发器恢复之后的状态,默认为disable。 rowid这个伪列是Oracle默认提供给每一个表的,主要用于记录每一行数据存储的磁盘物理地址。当删除一行记录后,后面的记录依次跟进上来,当 需要恢复某一个中间的行时,就需要行具备行移动功能(alter table <表名> enable row movement;) 【示例】 -- 授权用户闪回表的权限 grantflashbackanytableto itcast; -- 查看当前时间点或scn号 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'), timestamp_to_scn(sysdate)from dual; -- 删除数据 deletefrom emp where empno =7449; commit; --允许行移动 altertable emp enablerowmovement; -- 方式一;使用时间点闪回表 flashbacktable emp totimestamp to_timestamp('时间格式字符串','yyyy-mm-dd HH24:mi:ss'); -- 方式二;使用SCN闪回表 flashbacktable emp to SCN号; |
4.5闪回删除
闪回删除(flashback drop)。当整个表被删除并在回收站查询到的话;可以对表进行闪回。show recyclebin:可以显示当前用户recyclebin中的表。
系统参数recyclebin控制表删除后是否到回收站,show parameter recyclebin可以查看该参数的状态。
对于系统参数的修改有两种,全局的修改和会话的修改:
(1)alter system set param_name=param_value;
(2)alter session set param_name=param_value;
show recyclebin; --查看回收站
purge recyclebin; --清空回收站
【语法】 flashback table table_name to before drop [rename to new_name]; 【说明】 rename to new_name:如果在删除原表之后又重新创建了一个一样名称的表,那么恢复回收站的表时可以对表名进行重命名 【示例】 -- 删除表 droptable emp; -- 恢复表 flashbacktable emp tobeforedrop; |
5数据备份与恢复
5.1数据备份
--全表备份 exp itcast/itcast@orcl file=d:\database\oracle_data\itcast.dmp full=y; --指定表备份 exp itcast/itcast@orcl file=d:\database\oracle_data\itcast_emp_dept.dmp tables=(emp,dept); 【说明】full:完整导出数据库,一般使用system具有管理员权限的用户在命令行下进行操作。 |
5.2数据恢复
--全表恢复 imp itcast/itcast@orcl ignore=y file=d:\database\oracle_data\itcast.dmp full=y; --指定表恢复 imp itcast/itcast@orcl ignore=y file=d:\database\oracle_data\itcast_emp_dept.dmp tables=(emp,dept); 【说明】ignore:忽略创建错误 |
6性能优化
1、 查两张以上表时,把记录少的放在右边
2、 WHERE子句中的连接顺序
ORACLE采用自上而下的顺序解析WHERE子句,根据这个原则,那些可以过滤掉最大数量记录的条件应写在WHERE子句最后。
例如:查询员工的编号,姓名,工资,部门名
如果emp.sal>1500能过滤掉半数记录的话,
select emp.empno,emp.ename,emp.sal,dept.dname
from emp,dept
where (emp.deptno = dept.deptno) and (emp.sal > 1500)
.......
3、 SELECT子句中避免使用*号
ORACLE在解析的过程中,会将*依次转换成所有的列名,这个工作是通过查询数据字典完成的,这意味着将耗费更多的时间
4、 避免对大表进行无条件或无索引的的扫描
5、 清空表时用TRUNCATE替代DELETE
6、 尽量多使用COMMIT;因为COMMIT会释放回滚点
7、 用索引提高查询效率,善用索引
避免在索引列上使用NOT;因为Oracle服务器遇到NOT后,他就会停止目前的工作,转而执行全表扫描。
避免在索引列上使用计算;WHERE子句中,如果索引列是函数的一部分,优化器将不使用索引而使用全表扫描,这样会变得慢
例如,SAL列上有索引,
低效:
SELECT EMPNO,ENAME
FROM EMP
WHERE SAL*12 > 24000;
高效:
SELECT EMPNO,ENAME
FROM EMP
WHERE SAL > 24000/12;
8、字符串型,能用=号,不用like;=号表示精确比较,like表示模糊比较
9、 用 >= 替代 >
低效:
SELECT * FROM EMP WHERE DEPTNO > 3
首先定位到DEPTNO=3的记录并且扫描到第一个DEPT大于3的记录
高效:
SELECT * FROM EMP WHERE DEPTNO >= 4
直接跳到第一个DEPT等于4的记录
10、用IN替代OR
select * from emp where sal = 1500 or sal = 3000 or sal = 800;
select * from emp where sal in (1500,3000,800);
11、用exists代替in;not exists代替 not in
not in 字句将执行一个内部的排序和合并,任何情况下,not in是最低效的,子查询中全表扫描;表连接比exists更高效
12、用UNION-ALL 替换UNION
当SQL语句需要UNION两个查询结果集合时,这两个结果集合会以UNION-ALL的方式被合并, 然后在输出最终结果前进行排序. 如果用UNION ALL替代UNION, 这样排序就不是必要了. 效率会因此得到提高。
13、避免使用耗费资源的操作
带有DISTINCT,UNION,MINUS,INTERSECT的SQL语句会启动SQL引擎 执行耗费资源的排序(SORT)功能. DISTINCT需要一次排序操作, 而其他的至少需要执行两次排序. 通常, 带有UNION, MINUS , INTERSECT的SQL语句都可以用其他方式重写。
最后;同样的操作有些时候可以在程序上处理的就程序上处理,毕竟在内存中的执行速度比在硬盘上执行要高非常多。
7项目1切换到oracle数据库
1、添加oracle的驱动类包的到项目下的lib目录
2、修改applicationContext.xml中的数据库方言
3、修改db.properties中的数据库驱动类和用户名密码
4、其它修改(如创建user表时将表名修改为t_user,user是oracle数据库的关键字)
相关推荐
- RHEL8和CentOS8怎么重启网络
-
本文主要讲解如何重启RHEL8或者CentOS8网络以及如何解决RHEL8和CentOS8系统的网络管理服务报错,当我们安装好RHEL8或者CentOS8,重启启动网络时,会出现以下报错:...
- Linux 内、外网双网卡路由配置
-
1.路由信息的影响Linux系统中如果有多张网卡的情况下,如果路由信息配置不正确,...
- Linux——centos7修改网卡名
-
修改网卡名这个操作可能平时用不太上,可作为了解。修改网卡默认名从ens33改成eth01.首先修改网卡配置文件名(建议将原配置文件进行备份)...
- CentOS7下修改网卡名称为ethX的操作方法
-
?Linux操作系统的网卡设备的传统命名方式是eth0、eth1、eth2等,而CentOS7提供了不同的命名规则,默认是基于固件、拓扑、位置信息来分配。这样做的优点是命名全自动的、可预知的...
- Linux 网卡名称enss33修改为eth0
-
一、CentOS修改/etc/sysconfig/grub文件(修改前先备份)为GRUB_CMDLINE_LINUX变量增加2个参数(net.ifnames=0biosdevname=0),修改完成...
- CentOS下双网卡绑定,实现带宽飞速
-
方式一1.新建/etc/sysconfig/network-scripts/ifcfg-bond0文件DEVICE=bond0IPADDR=191.3.60.1NETMASK=255.255.2...
- linux 双网卡双网段设置路由转发
-
背景网络情况linux双网卡:网卡A(ens3)和网卡B(...
- Linux-VMware设置网卡保持激活
-
Linux系统只有在激活网卡的状态下才能去连接网络,进行网络通讯。修改配置文件(永久激活网卡)...
- VMware虚拟机三种网络模式
-
01.VMware虚拟机三种网络模式由于linux目前很热门,越来越多的人在学习linux,但是买一台服务放家里来学习,实在是很浪费。那么如何解决这个问题?虚拟机软件是很好的选择,常用的虚拟机软件有v...
- 2023年最新版 linux克隆虚拟机 解决网卡uuid重复问题
-
问题描述1、克隆了虚拟机,两台虚拟机里面的ip以及网卡的uuid都是一样的2、ip好改,但是uuid如何改呢?解决问题1、每台主机应该保证网卡的UUID是唯一的,避免后面网络通信有问题...
- Linux网卡的Vlan配置,你可能不了解的玩法
-
如果服务器上连的交换机端口已经预先设置了TRUNK,并允许特定的VLAN可以通过,那么服务器的网卡在配置时就必须指定所属的VLAN,否则就不通了,这种情形在虚拟化部署时较常见。例如在一个办公环境中,办...
- Centos7 网卡绑定
-
1、切换到指定目录#备份网卡数据cd/etc/sysconfig/network-scriptscpifcfg-enp5s0f0ifcfg-enp5s0f0.bak...
- Linux搭建nginx+keepalived 高可用(主备+双主模式)
-
一:keepalived简介反向代理及负载均衡参考:...
- Linux下Route 路由指令使用详解
-
linuxroute命令用于显示和操作IP路由表。要实现两个不同子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- oracle忘记用户名密码 (59)
- oracle11gr2安装教程 (55)
- mybatis调用oracle存储过程 (67)
- oracle spool的用法 (57)
- oracle asm 磁盘管理 (67)
- 前端 设计模式 (64)
- 前端面试vue (56)
- linux格式化 (55)
- linux图形界面 (62)
- linux文件压缩 (75)
- Linux设置权限 (53)
- linux服务器配置 (62)
- mysql安装linux (71)
- linux启动命令 (59)
- 查看linux磁盘 (72)
- linux用户组 (74)
- linux多线程 (70)
- linux设备驱动 (53)
- linux自启动 (59)
- linux网络命令 (55)
- linux传文件 (60)
- linux打包文件 (58)
- linux查看数据库 (61)
- linux获取ip (64)
- linux进程通信 (63)