Skip to content

Commit 543c5c5

Browse files
committed
analyzing executing and Indexes - B-Tree, Bitmap, Hash
1 parent 6e7ddc7 commit 543c5c5

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--------------------- Analyzing Execution Plan --------------------
2+
3+
/* get explaination
4+
EXPLAIN won't actually execute query. Instead it try to make estimation.
5+
*/
6+
EXPLAIN SELECT * FROM staff;
7+
8+
/* get details time to build the execution plan and explaination */
9+
EXPLAIN ANALYZE SELECT * FROM staff;
10+
11+
/* now width is 7, which is lower than previous one*/
12+
-- NOTE: if we are working with large amount data, we can use rows and width as a guide to understand the amount of data returned.
13+
EXPLAIN ANALYZE SELECT last_name FROM staff;
14+
15+
16+
/* As EXPLAIN just make estimation, rows=xxx sometimes a bit off by 1 or 2
17+
when we run the actual query, the rows returned are 717. but using EXPLAIN, it said 715.
18+
*/
19+
EXPLAIN SELECT *
20+
FROM staff
21+
WHERE salary > 75000;
22+
23+
24+
EXPLAIN ANALYZE SELECT *
25+
FROM staff
26+
WHERE salary > 75000;
27+
28+
29+
--------------------- Indexes --------------------
30+
31+
/* create index on salary column*/
32+
CREATE INDEX idx_staff_salary ON staff(salary);
33+
34+
35+
/* list all indexes*/
36+
SELECT
37+
tablename,
38+
indexname,
39+
indexdef
40+
FROM
41+
pg_indexes
42+
WHERE
43+
schemaname = 'public'
44+
ORDER BY
45+
tablename,
46+
indexname;
47+
48+
49+
/* compare against full table scan (without index) and using indexes */
50+
51+
EXPLAIN SELECT * FROM staff;
52+
53+
/*
54+
when we check the query plan, we can see full table scan is used instead of using index
55+
the reason is the criteria is fulfilled on many rows and system decided to use full table scan instead of using index.
56+
*/
57+
EXPLAIN ANALYZE SELECT * FROM staff WHERE salary > 75000;
58+
59+
/* here index is used because salary cut off criteria is much more selective */
60+
EXPLAIN ANALYZE SELECT * FROM staff WHERE salary > 150000;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/***************** Types of Indexes ************************/
2+
/*
3+
B-tree indexes
4+
Bitmap indexes
5+
Hash indexes
6+
PostgreSQL specific indexes
7+
*/
8+
9+
10+
11+
/************** B-tree Indexing (Binary Tree Indexing) ******************/
12+
13+
-- check on full table scan
14+
EXPLAIN SELECT * FROM staff
15+
WHERE email = '[email protected]';
16+
17+
18+
-- create index
19+
CREATE INDEX idx_email ON staff(email);
20+
21+
EXPLAIN SELECT * FROM staff
22+
WHERE email = '[email protected]';
23+
24+
25+
/******************** Bit Map Indexing *********************/
26+
27+
-- get all unique job titles
28+
SELECT DISTINCT(job_title)
29+
FROM staff
30+
ORDER BY job_title;
31+
32+
33+
EXPLAIN SELECT *
34+
FROM staff
35+
WHERE job_title = 'Operator';
36+
37+
-- create index
38+
CREATE INDEX idx_staff_job_title ON staff(job_title);
39+
40+
41+
/*
42+
now we can see query plan is used Bitmap Heap Scan using Bitmap index
43+
In postgreSQL, we don't need to explicty create bitmap index.
44+
postgreSQL will create on the fly if it finds situation if bitmap index is needed.
45+
*/
46+
EXPLAIN SELECT *
47+
FROM staff
48+
WHERE job_title = 'Operator';
49+
50+
51+
/************** Hash Index ******************/
52+
53+
-- create index
54+
CREATE INDEX idx_staff_email ON staff USING HASH(email);
55+
56+
EXPLAIN SELECT * FROM staff
57+
WHERE email = '[email protected]';
58+
59+
60+
/************** PostgreSQL specific indexes ******************/
61+
/*
62+
GIST
63+
SP-GIST
64+
GIN
65+
BRIN
66+
*/

0 commit comments

Comments
 (0)