Skip to content

Instantly share code, notes, and snippets.

@jehie

jehie/testdata Secret

Created May 4, 2017 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehie/87665c03bee124f8a96de24cae798194 to your computer and use it in GitHub Desktop.
Save jehie/87665c03bee124f8a96de24cae798194 to your computer and use it in GitHub Desktop.
SQL Performance issue
-- Create table
CREATE TABLE book
(
id SERIAL NOT NULL,
released timestamp without time zone NOT NULL,
author_id character varying(64) NOT NULL,
CONSTRAINT book_pkey PRIMARY KEY (id),
CONSTRAINT book_uq1 UNIQUE (author_id, released)
)
WITH (
OIDS=FALSE
);
-- Create index for author_id and released columns
CREATE INDEX book_idx1
ON book
USING btree
(author_id, released);
-- Add test data (50 authors, each have 100000 Books). Takes ~3 minutes to add.
do $$
begin
for r in 1..50 loop
for j in 1..100000 loop
insert into public.book(released, author_id) values(to_timestamp(extract(epoch from now()) + j), r);
end loop;
end loop;
end;
$$;
-- I need to search for the latest (created column) Book at certain time, for all or some of the author_ids. One Book per author.
-- This one using max() and group by is too slow, around 300ms (but I can search for multiple authors).
EXPLAIN ANALYZE
SELECT max(released), author_id
FROM book
WHERE author_id in ('1', '2', '3', '4', '5')
AND released<=to_timestamp(2e9)
AND released>=to_timestamp(0)
GROUP BY author_id
-- This query works fast really fast, around 0.2ms (but I cannot search latest books for multiple authors with same query).
EXPLAIN ANALYZE
SELECT released, author_id
FROM book
WHERE author_id = '1'
AND released<=to_timestamp(2e9)
AND released>=to_timestamp(0)
ORDER BY released desc
LIMIT 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment