3 EXPLAIN="EXPLAIN(ANALYZE,COSTS)"
6 echo "ex1.5.d unoptimized"
7 echo "${EXPLAIN} SELECT p.*, c.*, u.* FROM posts p, comments c, users u, badges b WHERE c.postid=p.id AND u.id=p.owneruserid AND u.upvotes+3 >= (SELECT AVG(upvotes) FROM users WHERE u.creationdate > c.creationdate) AND EXISTS (SELECT 1 FROM postlinks l WHERE l.relatedpostid > p.id) AND u.id = b.userid AND (b.name SIMILAR TO 'Autobiographer' OR b.name SIMILAR TO 'Supporter');" | psql
9 echo "ex1.5.d optimized"
10 echo "${EXPLAIN} SELECT p.*, c.*, u.* FROM posts p
11 JOIN users u ON p.owneruserid = u.id
12 JOIN badges b on u.id = b.userid
13 JOIN comments c ON p.id = c.postid
14 WHERE b.name IN ('Autobiographer','Supporter')
15 AND p.id < (SELECT MAX(relatedpostid) FROM postlinks)
17 (SELECT AVG(upvotes) FROM users WHERE u.creationdate > c.creationdate);" | psql