问题陈述:
您想提高常用查询的性能。
解:
我们必须使用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视图,以通过查看解释计划中的缓存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','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;