diff --git a/benchmark/arrs_deps_by_non_existent_stop.sql b/benchmark/arrs_deps_by_non_existent_stop.sql index fbccfee..e7c74c6 100644 --- a/benchmark/arrs_deps_by_non_existent_stop.sql +++ b/benchmark/arrs_deps_by_non_existent_stop.sql @@ -1,5 +1,3 @@ -SELECT * from bench( -'SELECT count(*) +SELECT count(*) FROM arrivals_departures -WHERE stop_id = ''definitely-non-existent''' -); +WHERE stop_id = 'definitely-non-existent' diff --git a/benchmark/arrs_deps_by_route_name_and_time.sql b/benchmark/arrs_deps_by_route_name_and_time.sql index 5a6bace..360f77f 100644 --- a/benchmark/arrs_deps_by_route_name_and_time.sql +++ b/benchmark/arrs_deps_by_route_name_and_time.sql @@ -1,8 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE route_short_name = ''S1'' -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')' -); +WHERE route_short_name = 'S1' +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/arrs_deps_by_station_and_time.sql b/benchmark/arrs_deps_by_station_and_time.sql index 0a96cd4..b297b68 100644 --- a/benchmark/arrs_deps_by_station_and_time.sql +++ b/benchmark/arrs_deps_by_station_and_time.sql @@ -1,9 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')', -40 -); +WHERE station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/arrs_deps_by_station_and_time_seq_0.sql b/benchmark/arrs_deps_by_station_and_time_seq_0.sql index c171d81..2a2a20d 100644 --- a/benchmark/arrs_deps_by_station_and_time_seq_0.sql +++ b/benchmark/arrs_deps_by_station_and_time_seq_0.sql @@ -1,10 +1,7 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'') -AND stop_sequence = 0', -50 -); +WHERE station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') +AND stop_sequence = 0 diff --git a/benchmark/arrs_deps_by_stop.sql b/benchmark/arrs_deps_by_stop.sql index a641eb5..b92de4b 100644 --- a/benchmark/arrs_deps_by_stop.sql +++ b/benchmark/arrs_deps_by_stop.sql @@ -1,5 +1,3 @@ -SELECT * from bench( -'SELECT count(*) +SELECT count(*) FROM arrivals_departures -WHERE stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)' -); +WHERE stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin) diff --git a/benchmark/arrs_deps_by_stop_and_time.sql b/benchmark/arrs_deps_by_stop_and_time.sql index a2c147e..5b26ff6 100644 --- a/benchmark/arrs_deps_by_stop_and_time.sql +++ b/benchmark/arrs_deps_by_stop_and_time.sql @@ -1,8 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')' -); +WHERE stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/arrs_deps_by_time.sql b/benchmark/arrs_deps_by_time.sql index ad82cc3..1d01275 100644 --- a/benchmark/arrs_deps_by_time.sql +++ b/benchmark/arrs_deps_by_time.sql @@ -1,8 +1,5 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02''::timestamp with time zone) -AND date <= dates_filter_max(''2022-08-09T07:30+02''::timestamp with time zone)', -10 -); +WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02'::timestamp with time zone) +AND date <= dates_filter_max('2022-08-09T07:30+02'::timestamp with time zone) diff --git a/benchmark/arrs_deps_by_time_manual.sql b/benchmark/arrs_deps_by_time_manual.sql index 9707b28..5c4dada 100644 --- a/benchmark/arrs_deps_by_time_manual.sql +++ b/benchmark/arrs_deps_by_time_manual.sql @@ -1,8 +1,5 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= ''2022-08-08'' -AND date <= ''2022-08-09''', -10 -); +WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= '2022-08-08' +AND date <= '2022-08-09' diff --git a/benchmark/arrs_deps_by_trip_and_date.sql b/benchmark/arrs_deps_by_trip_and_date.sql index ec451aa..89d4609 100644 --- a/benchmark/arrs_deps_by_trip_and_date.sql +++ b/benchmark/arrs_deps_by_trip_and_date.sql @@ -1,6 +1,4 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM arrivals_departures -WHERE trip_id = ''168977951'' -AND date > ''2022-08-08'' AND date <= ''2022-08-09''' -); +WHERE trip_id = '168977951' +AND date > '2022-08-08' AND date <= '2022-08-09' diff --git a/benchmark/as-md.js b/benchmark/as-md.js index 3c6f534..2765c3c 100755 --- a/benchmark/as-md.js +++ b/benchmark/as-md.js @@ -15,7 +15,7 @@ pipeline( if (firstRow) { firstRow = false - const keys = Object.keys(row) + const keys = Object.keys(row).filter(key => key !== 'filename') process.stdout.write(`| ${keys.join(' | ')} |\n`) process.stdout.write(`| ${keys.map(_ => '-').join(' | ')} |\n`) } diff --git a/benchmark/connections_by_non_existent_stop.sql b/benchmark/connections_by_non_existent_stop.sql index fb9242d..6a5f353 100644 --- a/benchmark/connections_by_non_existent_stop.sql +++ b/benchmark/connections_by_non_existent_stop.sql @@ -1,5 +1,3 @@ -SELECT * from bench( -'SELECT count(*) +SELECT count(*) FROM connections -WHERE from_stop_id = ''definitely-non-existent''' -); +WHERE from_stop_id = 'definitely-non-existent' diff --git a/benchmark/connections_by_route_name_and_time.sql b/benchmark/connections_by_route_name_and_time.sql index e754e96..ca5bcc0 100644 --- a/benchmark/connections_by_route_name_and_time.sql +++ b/benchmark/connections_by_route_name_and_time.sql @@ -1,8 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE route_short_name = ''S1'' -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')' -); +WHERE route_short_name = 'S1' +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/connections_by_station_and_time.sql b/benchmark/connections_by_station_and_time.sql index d468673..861108e 100644 --- a/benchmark/connections_by_station_and_time.sql +++ b/benchmark/connections_by_station_and_time.sql @@ -1,9 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE from_station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')', -40 -); +WHERE from_station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/connections_by_station_and_time_seq_0.sql b/benchmark/connections_by_station_and_time_seq_0.sql index 5606e50..7eaa73d 100644 --- a/benchmark/connections_by_station_and_time_seq_0.sql +++ b/benchmark/connections_by_station_and_time_seq_0.sql @@ -1,10 +1,7 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE from_station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'') -AND from_stop_sequence = 0', -50 -); +WHERE from_station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') +AND from_stop_sequence = 0 diff --git a/benchmark/connections_by_stop.sql b/benchmark/connections_by_stop.sql index e993f2b..944bf4d 100644 --- a/benchmark/connections_by_stop.sql +++ b/benchmark/connections_by_stop.sql @@ -1,5 +1,3 @@ -SELECT * from bench( -'SELECT count(*) +SELECT count(*) FROM connections -WHERE from_stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)' -); +WHERE from_stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin) diff --git a/benchmark/connections_by_stop_and_time.sql b/benchmark/connections_by_stop_and_time.sql index 41eb953..7baf415 100644 --- a/benchmark/connections_by_stop_and_time.sql +++ b/benchmark/connections_by_stop_and_time.sql @@ -1,8 +1,6 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE from_stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin) -AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02'') -AND date <= dates_filter_max(''2022-08-09T07:30+02'')' -); +WHERE from_stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin) +AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02') +AND date <= dates_filter_max('2022-08-09T07:30+02') diff --git a/benchmark/connections_by_time.sql b/benchmark/connections_by_time.sql index c40aab9..de4dff1 100644 --- a/benchmark/connections_by_time.sql +++ b/benchmark/connections_by_time.sql @@ -1,10 +1,7 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= dates_filter_min(''2022-08-09T07:10+02''::timestamp with time zone) -AND date <= dates_filter_max(''2022-08-09T07:30+02''::timestamp with time zone) +WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= dates_filter_min('2022-08-09T07:10+02'::timestamp with time zone) +AND date <= dates_filter_max('2022-08-09T07:30+02'::timestamp with time zone) ORDER BY t_departure -LIMIT 100', -7 -); +LIMIT 100 diff --git a/benchmark/connections_by_time_manual.sql b/benchmark/connections_by_time_manual.sql index 4a0bfc0..c483d02 100644 --- a/benchmark/connections_by_time_manual.sql +++ b/benchmark/connections_by_time_manual.sql @@ -1,10 +1,7 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02'' -AND date >= ''2022-08-08'' -AND date <= ''2022-08-09'' +WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02' +AND date >= '2022-08-08' +AND date <= '2022-08-09' ORDER BY t_departure -LIMIT 100', -7 -); +LIMIT 100 diff --git a/benchmark/connections_by_trip_and_date.sql b/benchmark/connections_by_trip_and_date.sql index 992d081..93ef135 100644 --- a/benchmark/connections_by_trip_and_date.sql +++ b/benchmark/connections_by_trip_and_date.sql @@ -1,6 +1,4 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM connections -WHERE trip_id = ''168977951'' -AND date > ''2022-08-08'' AND date <= ''2022-08-09''' -); +WHERE trip_id = '168977951' +AND date > '2022-08-08' AND date <= '2022-08-09' diff --git a/benchmark/index.sql b/benchmark/index.sql index 532843c..de3c5e9 100644 --- a/benchmark/index.sql +++ b/benchmark/index.sql @@ -1,5 +1,6 @@ BEGIN; CREATE TEMP TABLE _benchmark ( + filename TEXT, query TEXT, avg FLOAT, min FLOAT, @@ -14,7 +15,7 @@ CREATE TEMP TABLE _benchmark ( -- slightly modified from "How to benchmark PostgreSQL queries well" -- https://www.tangramvision.com/blog/how-to-benchmark-postgresql-queries-well#sql-function-with-clock_timestamp -CREATE OR REPLACE FUNCTION bench(_query TEXT, _iterations INTEGER = 100) +CREATE OR REPLACE FUNCTION bench(_filename TEXT, _query TEXT, _iterations INTEGER = 100) RETURNS void AS $$ DECLARE @@ -41,6 +42,7 @@ BEGIN INSERT INTO _benchmark SELECT + _filename, _query, round(avg(elapsed)::numeric, 0), min(elapsed), @@ -58,26 +60,46 @@ END $$ LANGUAGE plpgsql; -\i stops_by_distance.sql -\i arrs_deps_by_route_name_and_time.sql -\i arrs_deps_by_station_and_time.sql -\i arrs_deps_by_station_and_time_seq_0.sql -\i arrs_deps_by_stop_and_time.sql -\i arrs_deps_by_trip_and_date.sql -\i arrs_deps_by_stop.sql -\i arrs_deps_by_non_existent_stop.sql -\i arrs_deps_by_time.sql -\i arrs_deps_by_time_manual.sql -\i connections_by_route_name_and_time.sql -\i connections_by_station_and_time.sql -\i connections_by_station_and_time_seq_0.sql -\i connections_by_stop_and_time.sql -\i connections_by_trip_and_date.sql -\i connections_by_stop.sql -\i connections_by_non_existent_stop.sql -\i connections_by_time.sql -\i connections_by_time_manual.sql -\i stats_by_route_date.sql +\set query `cat arrs_deps_by_non_existent_stop.sql` +SELECT bench('arrs_deps_by_non_existent_stop.sql', :'query'); +\set query `cat arrs_deps_by_route_name_and_time.sql` +SELECT bench('arrs_deps_by_route_name_and_time.sql', :'query'); +\set query `cat arrs_deps_by_station_and_time.sql` +SELECT bench('arrs_deps_by_station_and_time.sql', :'query', 40); +\set query `cat arrs_deps_by_station_and_time_seq_0.sql` +SELECT bench('arrs_deps_by_station_and_time_seq_0.sql', :'query', 50); +\set query `cat arrs_deps_by_stop.sql` +SELECT bench('arrs_deps_by_stop.sql', :'query'); +\set query `cat arrs_deps_by_stop_and_time.sql` +SELECT bench('arrs_deps_by_stop_and_time.sql', :'query'); +\set query `cat arrs_deps_by_time.sql` +SELECT bench('arrs_deps_by_time.sql', :'query', 10); +\set query `cat arrs_deps_by_time_manual.sql` +SELECT bench('arrs_deps_by_time_manual.sql', :'query', 10); +\set query `cat arrs_deps_by_trip_and_date.sql` +SELECT bench('arrs_deps_by_trip_and_date.sql', :'query'); +\set query `cat connections_by_non_existent_stop.sql` +SELECT bench('connections_by_non_existent_stop.sql', :'query'); +\set query `cat connections_by_route_name_and_time.sql` +SELECT bench('connections_by_route_name_and_time.sql', :'query'); +\set query `cat connections_by_station_and_time.sql` +SELECT bench('connections_by_station_and_time.sql', :'query', 40); +\set query `cat connections_by_station_and_time_seq_0.sql` +SELECT bench('connections_by_station_and_time_seq_0.sql', :'query', 50); +\set query `cat connections_by_stop.sql` +SELECT bench('connections_by_stop.sql', :'query'); +\set query `cat connections_by_stop_and_time.sql` +SELECT bench('connections_by_stop_and_time.sql', :'query'); +\set query `cat connections_by_time.sql` +SELECT bench('connections_by_time.sql', :'query', 7); +\set query `cat connections_by_time_manual.sql` +SELECT bench('connections_by_time_manual.sql', :'query', 7); +\set query `cat connections_by_trip_and_date.sql` +SELECT bench('connections_by_trip_and_date.sql', :'query'); +\set query `cat stats_by_route_date.sql` +SELECT bench('stats_by_route_date.sql', :'query', 10); +\set query `cat stops_by_distance.sql` +SELECT bench('stops_by_distance.sql', :'query'); SELECT * FROM _benchmark; diff --git a/benchmark/run.sh b/benchmark/run.sh index dd8a89c..83da505 100755 --- a/benchmark/run.sh +++ b/benchmark/run.sh @@ -7,4 +7,4 @@ set -x psql -c 'VACUUM ANALYZE' -psql -q -b --csv -f index.sql +psql -q -b -v 'ON_ERROR_STOP=1' --csv -f index.sql diff --git a/benchmark/stats_by_route_date.sql b/benchmark/stats_by_route_date.sql index 6f191d8..a894e09 100644 --- a/benchmark/stats_by_route_date.sql +++ b/benchmark/stats_by_route_date.sql @@ -1,8 +1,5 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM stats_by_route_date -WHERE route_id = ''17452_900'' -- M4 -AND date >= ''2022-08-08'' AND date <= ''2022-08-14'' -AND is_effective = true', -10 -); +WHERE route_id = '17452_900' -- M4 +AND date >= '2022-08-08' AND date <= '2022-08-14' +AND is_effective = true diff --git a/benchmark/stops_by_distance.sql b/benchmark/stops_by_distance.sql index 2fe7636..fc112f9 100644 --- a/benchmark/stops_by_distance.sql +++ b/benchmark/stops_by_distance.sql @@ -1,6 +1,4 @@ -SELECT * from bench( -'SELECT * +SELECT * FROM stops ORDER BY ST_Distance(stop_loc::geometry, ST_SetSRID(ST_MakePoint(9.7, 50.547), 4326)) ASC -LIMIT 100' -); +LIMIT 100