Skip to content

Instantly share code, notes, and snippets.

View 3v0k4's full-sized avatar

Riccardo 3v0k4

View GitHub Profile
number_of_runs = # how many times to repeat the check
number_of_threads = # how many concurrent write request
write_url = # url to use for the write requests
write_params = # params to send in each write request
read_url = # url used to check the state of the system after the concurrent write requests
number_of_runs.times do |i|
puts “run ##{i+1}”
concurrent_post(number_of_threads, write_url, write_params)
state = get(read_url)
=> SELECT id, genre, title from films;
id | genre | title
----+----------+-------------------
1 | thriller | Blade Runner 2049
2 | thriller | Inception
3 | scifi | Arrival
=> SELECT id, film_id, content FROM reviews;
id | film_id | content
----+---------+-----------------------------------------------------------
1 | 1 | Visually stunning and thought provoking, but not flawless
2 | 1 | One of the best sequels of all time
3 | 1 | Strangely boring, lacking tension and intelligence
4 | 2 | Amazing Directing, Captivating Plot, Overall Great
=> SELECT genre, count(*) FROM films GROUP BY genre;
genre | count
----------+-------
thriller | 2
scifi | 1
=> SELECT title, genre, count(*) FROM films GROUP BY genre;
ERROR: column "films.title" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT title, genre, count(*) FROM films GROUP BY genre;
=> SELECT title, genre, count(*) FROM films GROUP BY title, genre;
title | genre | count
-------------------+----------+-------
Blade Runner 2049 | thriller | 1
Inception | thriller | 1
Arrival | scifi | 1
=> SELECT string_agg(title, ', ') as titles, genre, count(*) FROM films GROUP BY genre;
titles | genre | count
------------------------------+----------+-------
Blade Runner 2049, Inception | thriller | 2
Arrival | scifi | 1
=> SELECT id, title, genre FROM films GROUP BY id;
id | title | genre
----+-------------------+----------
3 | Arrival | scifi
1 | Blade Runner 2049 | thriller
2 | Inception | thriller
=> SELECT films.id, films.title, films.genre, count(*) as number_of_reviews FROM films LEFT JOIN reviews ON films.id = reviews.film_id GROUP BY films.id;
id | title | genre | number_of_reviews
----+-------------------+----------+-------------------
3 | Arrival | scifi | 1
1 | Blade Runner 2049 | thriller | 3
2 | Inception | thriller | 1
=> SELECT films.id, films.title, films.genre, string_agg(reviews.content, E'\n') FROM films LEFT JOIN reviews ON films.id = reviews.film_id GROUP BY films.id;
id | title | genre | string_agg
----+-------------------+----------+-----------------------------------------------------------
3 | Arrival | scifi |
1 | Blade Runner 2049 | thriller | Visually stunning and thought provoking, but not flawless+
| | | One of the best sequels of all time +
| | | Strangely boring, lacking tension and intelligence
2 | Inception | thriller | Amazing Directing, Captivating Plot, Overall Great