Created
October 8, 2017 09:51
-
-
Save tennisonchan/2e4f103b10ac09dba0a3e6ef81ed0718 to your computer and use it in GitHub Desktop.
Tests between SQL commands with and without covering indexes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET SERVEROUTPUT ON | |
DECLARE | |
v_ts TIMESTAMP; | |
v_repeat CONSTANT NUMBER := 100000; | |
BEGIN | |
v_ts := SYSTIMESTAMP; | |
FOR i IN 1..v_repeat LOOP | |
FOR rec IN ( | |
-- Worst query: Memory overhead AND table access | |
SELECT * | |
FROM actor | |
WHERE last_name LIKE 'A%' | |
) LOOP | |
NULL; | |
END LOOP; | |
END LOOP; | |
dbms_output.put_line('Statement 1 : ' || (SYSTIMESTAMP - v_ts)); | |
v_ts := SYSTIMESTAMP; | |
FOR i IN 1..v_repeat LOOP | |
FOR rec IN ( | |
-- Better query: Still table access | |
SELECT /*+INDEX(actor(last_name))*/ | |
first_name, last_name | |
FROM actor | |
WHERE last_name LIKE 'A%' | |
) LOOP | |
NULL; | |
END LOOP; | |
END LOOP; | |
dbms_output.put_line('Statement 2 : ' || (SYSTIMESTAMP - v_ts)); | |
v_ts := SYSTIMESTAMP; | |
FOR i IN 1..v_repeat LOOP | |
FOR rec IN ( | |
-- Best query: Covering index | |
SELECT /*+INDEX(actor(last_name, first_name))*/ | |
first_name, last_name | |
FROM actor | |
WHERE last_name LIKE 'A%' | |
) LOOP | |
NULL; | |
END LOOP; | |
END LOOP; | |
dbms_output.put_line('Statement 3 : ' || (SYSTIMESTAMP - v_ts)); | |
END; | |
/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment