如何在 Oracle 中缓存查询结果?
问题陈述
您希望提高常用查询的性能。
解决方案
我们必须使用 Oracle 的结果缓存来存储常用 SQL 的查询结果,以便在将来执行相同查询时可以快速检索它们。
结果缓存是 Oracle 11g 的新增功能,它允许我们将常用查询的结果存储在内存中,以便快速轻松地检索。
示例
SELECT /*+ result_cache */ e.class_id, min_fees, max_fees FROM students e ,jobs j WHERE e.class_id = j.class_id GROUP BY e.class_id, min_fees, max_fees;
为了演示如何使用它,我们将检查上述 sql 的 explain_plan。
示例
EXPLAIN PLAN FOR SELECT /*+ result_cache */ e.class_id, min_fees, max_fees FROM students e ,jobs j WHERE e.class_id = j.class_id GROUP BY e.class_id, min_fees, max_fees; SELECT * FROM table(DBMS_XPLAN.DISPLAY (FORMAT=>'ALL'));
输出
--------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 19 | 475 | 4 (25)| 00:00:01 | | 1 | RESULT CACHE | a1pfq5gpbag4wgj0zth2xasdj1 | 19 | 475 | 4 (25)| 00:00:01 | | 2 | HASH GROUP BY | | 19 | 475 | 4 (25)| 00:00:01 | | 3 | NESTED LOOPS SEMI | | 19 | 475 | 3 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL| JOBS | 19 | 304 | 3 (0)| 00:00:01 | |* 5 | INDEX RANGE SCAN | stu_JOB_IX | 107 | 963 | 0 (0)| 00:00:01 | ---------------------------------------------------------------------------------------------------
然后,我们将查询 V$RESULT_CACHE_OBJECTS 视图,通过查看 explain 计划中的缓存 ID 值来验证查询的结果是否存储在结果缓存中。
示例
SELECT ID, type, status, creation_timestamp, column_count AS cols, row_count AS "rows" FROM V$RESULT_CACHE_OBJECTS WHERE CACHE_ID = 'a1pfq5gpbag4wgj0zth2xasdj1';
示例
1947 Result Published 11/NOV/20 3 19
结果缓存提示在添加到 SQL 时,将覆盖任何数据库、表或会话级别的结果缓存设置。在将提示添加到 SQL 之前,您需要验证数据库上结果缓存的配置。
使用结果缓存可以显着提高查询时间,因为首先会在结果缓存中检查查询结果,如果结果存在,则只需从内存中提取它们,并且不会执行查询。结果缓存最适合用于产生相同结果的频繁执行的查询。
为了显示性能改进,上述 SQL 运行了两次,第一次没有提示,第二次有提示。
示例
SELECT /*+ no_result_cache */ e.class_id, min_fees, max_fees FROM students e ,jobs j WHERE e.class_id = j.class_id GROUP BY e.class_id, min_fees, max_fees;
输出
19 rows selected. Elapsed: 00:00:17.20
示例
SELECT /*+ result_cache */ e.class_id, min_fees, max_fees FROM students e ,jobs j WHERE e.class_id = j.class_id GROUP BY e.class_id, min_fees, max_fees;
输出
19 rows selected. Elapsed: 00:00:01.03
用于上述问题的数据库
示例
DROP TABLE students; COMMIT; CREATE TABLE students ( student_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) , email VARCHAR2(40) , phone_number VARCHAR2(20) , join_date DATE , class_id VARCHAR2(20) , fees NUMBER(8,2) , professor_id NUMBER(6) , department_id NUMBER(4) ) ;
示例
CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ; INSERT INTO students VALUES (100,'SMITH','JAMES','[email protected]','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','[email protected]','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','[email protected]','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','[email protected]','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','[email protected]','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','[email protected]','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','[email protected]','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','[email protected]','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','[email protected]','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','[email protected]','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','[email protected]','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','[email protected]','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','[email protected]','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','[email protected]','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;