]> git.somenet.org - pub/jan/adbs.git/blob - ex1/main_5.tex
layoutstuff
[pub/jan/adbs.git] / ex1 / main_5.tex
1 %ex1.5
2
3 \begin{enumerate}[label=(\alph*)]
4 % (a)
5 \item
6 Optimized query:
7 \begin{verbatim}
8 SELECT distinct(displayname) FROM posts p
9     JOIN users u ON p.owneruserid = u.id
10     WHERE p.viewcount > u.views;
11 \end{verbatim}
12
13 Improvements:\\
14 Just converted the selection to a JOIN.
15 This improves the $n_{users} + n_{users} * n_{posts}$ Seq Scans
16 to one JOIN and two Seq Scans.
17
18 Original version:
19 \begin{verbatim}
20  Planning time: 0.978 ms
21  Execution time: 86179.847 ms
22 \end{verbatim}
23
24 Optimized version:
25 \begin{verbatim}
26  Planning time: 0.960 ms
27  Execution time: 37.354 ms
28 \end{verbatim}
29
30 % (b)
31 \item
32 Optimized query:
33 \begin{verbatim}
34 SELECT score FROM comments WHERE text ILIKE 'yes%';
35 \end{verbatim}
36
37 Improvements:\\
38 None so far.
39
40 Original version:
41 \begin{verbatim}
42  Planning time: 0.540 ms
43  Execution time: 60.946 ms
44 \end{verbatim}
45
46 Optimized version:
47 \begin{verbatim}
48 TODO, current query is slower..
49 \end{verbatim}
50
51 % (b)
52 \item
53 Optimized query:
54 \begin{verbatim}
55 SELECT DISTINCT postid FROM votes WHERE postid NOT IN (
56     SELECT postid FROM votes WHERE votetypeid != 2
57 );
58 \end{verbatim}
59
60 Improvements:\\
61 Instead of doing about twelve million Seq Scans this is now reduced to
62 two Seq Scans by inverting the condition.
63
64 Original version:
65 \begin{verbatim}
66 Did not terminate.
67 On a different machine with SSD and no time limit it took 16 minutes.
68 \end{verbatim}
69
70 Optimized version:
71 \begin{verbatim}
72  Planning time: 1.093 ms
73  Execution time: 81.776 ms
74 \end{verbatim}
75
76 % (d)
77 \item
78 Optimized query:
79 \begin{verbatim}
80 SELECT p.*, c.*, u.* FROM posts p
81     JOIN comments c ON p.id = c.postid
82     JOIN users u ON p.owneruserid = u.id
83     JOIN badges b on u.id = b.userid
84     WHERE u.upvotes+3 >= (
85         SELECT AVG(upvotes) FROM users
86             WHERE u.creationdate > c.creationdate
87         ) AND EXISTS (
88             SELECT 1 FROM postlinks l
89                 WHERE l.relatedpostid > p.id
90         ) AND b.name IN ('Autobiographer','Supporter');
91 \end{verbatim}
92
93 Improvements:\\
94 Converted the joins via WHERE clause to proper JOINs
95
96 Original version:
97 \begin{verbatim}
98  Planning time: 4.084 ms
99  Execution time: 4006.044 ms
100 \end{verbatim}
101
102 Optimized version:
103 \begin{verbatim}
104  Planning time: 2.954 ms
105  Execution time: 3081.461 ms
106 \end{verbatim}
107 \end{enumerate}