Commit 462a4e00 authored by Lacoin Achille's avatar Lacoin Achille
Browse files

ajout du TP1 de BD

parent d15d7d92
This diff is collapsed.
# TD1
## Question 1
1. explain analyse select * from film;
```
Seq Scan on film (cost=0.00..33.44 rows=1844 width=30) (actual time=0.010..0.965 rows=1844 loops=1)
Planning time: 0.095 ms
Execution time: 1.849 ms
(3 rows)
```
2. select tablename from pg_indexes;
**TOUT EST FAUX CAR ON A CHOISI NOTE ET PAS VOTE**
3. select * from film where note between 0 and 1000;
```
Seq Scan on film (cost=0.00..42.66 rows=1844 width=30) (actual time=0.017..1.258 rows=1844 loops=1)
Filter: ((note >= 0::double precision) AND (note <= 1000::double precision))
Planning time: 0.102 ms
Execution time: 2.127 ms
```
select * from film where note between 0 and 2000;
```
Seq Scan on film (cost=0.00..42.66 rows=1844 width=30) (actual time=0.013..1.219 rows=1844 loops=1)
Filter: ((note >= 0::double precision) AND (note <= 2000::double precision))
Planning time: 0.099 ms
Execution time: 2.100 ms
```
select * from film where note between 0 and 2000;
``` Seq Scan on film (cost=0.00..42.66 rows=1844 width=30) (actual time=0.014..1.260 rows=1844 loops=1)
Filter: ((note >= 0::double precision) AND (note <= 10000::double precision))
Planning time: 0.107 ms
Execution time: 2.141 ms
(4 rows)
```
select * from film where note between 0 and 10000;
```
Seq Scan on film (cost=0.00..42.66 rows=1844 width=30) (actual time=0.015..1.243 rows=1844 loops=1)
Filter: ((note >= 0::double precision) AND (note <= 50000::double precision))
Planning time: 0.107 ms
Execution time: 2.113 ms
(4 rows)
```
**A partir de 2000 on passe en sequentielle, donc la suppression de l'index ne change rien**
**Pour 1000, on remarque une baisse des performances**
##Question 2
1. Sans Index
* SELECT titre FROM film WHERE votes >= (SELECT max(votes) FROM film);
```
Seq Scan on film (cost=38.06..76.11 rows=615 width=16) (actual time=1.948..2.161 rows=1 loops=1)
Filter: (votes >= $0)
Rows Removed by Filter: 1843
InitPlan 1 (returns $0)
-> Aggregate (cost=38.05..38.06 rows=1 width=4) (actual time=1.927..1.928 rows=1 loops=1)
-> Seq Scan on film film_1 (cost=0.00..33.44 rows=1844 width=4) (actual time=0.003..0.843 rows=1844 loops=1)
Planning time: 0.160 ms
Execution time: 2.203 ms
```
* SELECT titre FROM film WHERE votes >= ALL (SELECT votes FROM film);
```
Seq Scan on film (cost=0.00..43620.99 rows=922 width=16) (actual time=3.937..5.982 rows=1 loops=1)
Filter: (SubPlan 1)
Rows Removed by Filter: 1843
SubPlan 1
-> Materialize (cost=0.00..42.66 rows=1844 width=4) (actual time=0.000..0.002 rows=2 loops=1844)
-> Seq Scan on film film_1 (cost=0.00..33.44 rows=1844 width=4) (actual time=0.004..1.038 rows=1844 loops=1)
Planning time: 0.103 ms
Execution time: 6.032 ms
```
2. Avec index
* SELECT titre FROM film WHERE votes >= (SELECT max(votes) FROM film);
```
Index Scan using index_votes on film (cost=0.61..31.37 rows=615 width=16) (actual time=0.067..0.068 rows=1 loops=1)
Index Cond: (votes >= $1)
InitPlan 2 (returns $1)
-> Result (cost=0.32..0.33 rows=1 width=0) (actual time=0.059..0.059 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.28..0.32 rows=1 width=4) (actual time=0.054..0.055 rows=1 loops=1)
-> Index Only Scan Backward using index_votes on film film_1 (cost=0.28..82.55 rows=1844 width=4) (actual time=0.052..0.052 rows=1 loops=1)
Index Cond: (votes IS NOT NULL)
Heap Fetches: 1
Planning time: 0.377 ms
Execution time: 0.107 ms
```
* SELECT titre FROM film WHERE votes >= ALL (SELECT votes FROM film);
```
Seq Scan on film (cost=0.00..43620.99 rows=922 width=16) (actual time=3.867..6.043 rows=1 loops=1)
Filter: (SubPlan 1)
Rows Removed by Filter: 1843
SubPlan 1
-> Materialize (cost=0.00..42.66 rows=1844 width=4) (actual time=0.000..0.002 rows=2 loops=1844)
-> Seq Scan on film film_1 (cost=0.00..33.44 rows=1844 width=4) (actual time=0.004..1.087 rows=1844 loops=1)
Planning time: 0.145 ms
Execution time: 6.095 ms
```
**Pour la requete avec max, amélioration drastique des performances : on est en O(1) !**
**Pour l'autre pas de changement car on selecte tout (on est en sequentielle)**
##Question 3
1. Sans Index
* SELECT titre FROM film WHERE votes > (SELECT votes FROM film WHERE titre=’Star Wars’);
```
Seq Scan on film (cost=8.29..46.34 rows=615 width=16) (actual time=0.278..0.278 rows=0 loops=1)
Filter: (votes > $0)
Rows Removed by Filter: 1844
InitPlan 1 (returns $0)
-> Index Scan using film_titre on film film_1 (cost=0.28..8.29 rows=1 width=4) (actual time=0.016..0.017 rows=1 loops=1)
Index Cond: ((titre)::text = 'Star Wars'::text)
Planning time: 0.178 ms
Execution time: 0.306 ms
```
* SELECT f1.titre FROM film f1, film f2 WHERE f1.votes > f2.votes AND f2.titre=’Star Wars’;
```
Nested Loop (cost=0.28..64.78 rows=615 width=16) (actual time=2.081..2.081 rows=0 loops=1)
Join Filter: (f1.votes > f2.votes)
Rows Removed by Join Filter: 1844
-> Index Scan using film_titre on film f2 (cost=0.28..8.29 rows=1 width=4) (actual time=0.025..0.028 rows=1 loops=1)
Index Cond: ((titre)::text = 'Star Wars'::text)
-> Seq Scan on film f1 (cost=0.00..33.44 rows=1844 width=20) (actual time=0.010..0.986 rows=1844 loops=1)
Planning time: 0.134 ms
Execution time: 2.121 ms
```
2. Avec index
* SELECT titre FROM film WHERE votes > (SELECT votes FROM film WHERE titre=’Star Wars’);
```
Index Scan using index_votes on film (cost=8.57..39.34 rows=615 width=16) (actual time=0.053..0.053 rows=0 loops=1)
Index Cond: (votes > $0)
InitPlan 1 (returns $0)
-> Index Scan using film_titre on film film_1 (cost=0.28..8.29 rows=1 width=4) (actual time=0.018..0.019 rows=1 loops=1)
Index Cond: ((titre)::text = 'Star Wars'::text)
Planning time: 0.250 ms
Execution time: 0.078 ms
```
* SELECT f1.titre FROM film f1, film f2 WHERE f1.votes > f2.votes AND f2.titre=’Star Wars’;
```
Nested Loop (cost=0.56..45.49 rows=615 width=16) (actual time=0.033..0.033 rows=0 loops=1)
-> Index Scan using film_titre on film f2 (cost=0.28..8.29 rows=1 width=4) (actual time=0.020..0.021 rows=1 loops=1)
Index Cond: ((titre)::text = 'Star Wars'::text)
-> Index Scan using index_votes on film f1 (cost=0.28..31.04 rows=615 width=20) (actual time=0.007..0.007 rows=0 loops=1)
Index Cond: (votes > f2.votes)
Planning time: 0.239 ms
Execution time: 0.068 ms
```
**La jointure coûte très chère dans le premier cas. Dans deuxième cas, l'index permet de d'augmenter la vitesse de la jointure**
## Question 4
1. Sans index
* SELECT titre FROM film WHERE titre LIKE ’I%’;
```
Seq Scan on film (cost=0.00..38.05 rows=18 width=16) (actual time=0.020..0.317 rows=25 loops=1)
Filter: ((titre)::text ~~ 'I%'::text)
Rows Removed by Filter: 1819
Planning time: 0.131 ms
Execution time: 0.378 ms
(5 rows)
```
* SELECT titre FROM film WHERE substr(titre,1,1) = ’I’;
```
Seq Scan on film (cost=0.00..42.66 rows=9 width=16) (actual time=0.024..0.547 rows=25 loops=1)
Filter: (substr((titre)::text, 1, 1) = 'I'::text)
Rows Removed by Filter: 1819
Planning time: 0.072 ms
Execution time: 0.580 ms
```
* SELECT titre FROM film WHERE titre LIKE ’%A’;
```
Seq Scan on film (cost=0.00..38.05 rows=18 width=16) (actual time=0.020..0.351 rows=30 loops=1)
Filter: ((titre)::text ~~ '%A'::text)
Rows Removed by Filter: 1814
Planning time: 0.079 ms
Execution time: 0.384 ms
```
2. Avec index
* SELECT titre FROM film WHERE titre LIKE ’I%’;
* SELECT titre FROM film WHERE substr(titre,1,1) = ’I’;
* SELECT titre FROM film WHERE titre LIKE ’%A’;
**Dans les 3 cas, Pas de changements**
3.
* SELECT titre FROM film WHERE titre LIKE ’I%’;
* SELECT titre FROM film WHERE substr(titre,1,1) = ’I’;
* SELECT titre FROM film WHERE titre LIKE ’%A’;
**Seul la première est plus performante car elle est capable d'exploiter varchar_pattern_ops (fonction LIKE), et la dernière ne change rien car on part de la fin du mot**
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment