Database Scan Algorithms Quiz

scan algorithms, query optimizer, sequential scans

quiz on Scans

This set of questions relates to the results of the EXPLAIN query on simple queries.

The quiz is available as a google form https://forms.gle/5fdjpUSr8YaqA9KNA

Q1. What elements are used by the query optimizer to compute the cost function

Q2. What is a Sequential scan

in the query

select * from trees;

The EXPLAIN diagram uses a sequential scan

What is a sequential scan ?

Q3. difference between EXPLAIN and EXPLAIN ANALYZE

Q4. how does the query planner chooses the algorithms to execute the query

Q5. compare the 2 queries. why has the cost gone up when we add a filter

epita@airdb=> explain select * from passenger;
                              QUERY PLAN
----------------------------------------------------------------------
 Seq Scan on passenger  (cost=0.00..334838.55 rows=16311855 width=47)
(1 row)

so we have

and

epita@airdb=> explain select * from passenger where age > 68;
                                   QUERY PLAN
---------------------------------------------------------------------------------
 Seq Scan on passenger  (cost=0.00..375618.19 rows=3613778 width=47)
   Filter: (age > 18)
(5 rows)

so we have

In the second query the cost has gone up although less rows are returned.

Why ?

Q6. Index and Bitmap scans

The passenger table has a id primary key

Explain the difference between Index Scan and Index Heap scan in the 2 examples

 explain select * from passenger where passenger_id = 8888;
                                   QUERY PLAN
---------------------------------------------------------------------------------
 Index Scan using passenger_pkey on passenger  (cost=0.43..8.45 rows=1 width=47)
   Index Cond: (passenger_id = 8888)
(2 rows)

and

explain select * from passenger where passenger_id < 8888;
                                    QUERY PLAN
----------------------------------------------------------------------------------
 Bitmap Heap Scan on passenger  (cost=184.03..31558.41 rows=9754 width=47)
   Recheck Cond: (passenger_id < 8888)
   ->  Bitmap Index Scan on passenger_pkey  (cost=0.00..181.59 rows=9754 width=0)
         Index Cond: (passenger_id < 8888)

Which if the following statement is true

Bitmap Heap Scans are often more efficient than Index Scan when retrieving a larger number of rows. They are the same thing but bitmap scanning is for boolean data types. When retrieving a small number of rows, Index Scan is faster because it avoids building the bitmap. Bitmap Scanning is a 2 step process: create the bitmap, then accesses the table.

Q7. Index Scans

Q8. Bitmap Heap Scans

Q9. order of operations

In the following query plan

explain select * from passenger order by random() limit 50;
                                    QUERY PLAN
----------------------------------------------------------------------------------
 Limit  (cost=917486.28..917486.41 rows=50 width=55)
   ->  Sort  (cost=917486.28..958265.92 rows=16311855 width=55)
         Sort Key: (random())
         ->  Seq Scan on passenger  (cost=0.00..375618.19 rows=16311855 width=55)
 JIT:
   Functions: 3
   Options: Inlining true, Optimization true, Expressions true, Deforming true
(7 rows)

what is the order of operations ?