我创建了这样的表(类似于http://use-the-index-luke.com/sql/example-schema/postgresql/performance-testing-scalability 中的示例)
CREATE TABLE scale_data (
section NUMERIC NOT NULL,
id1 NUMERIC NOT NULL, -- unique values simulating ID or Timestamp
id2 NUMERIC NOT NULL -- a kind of Type
);
Run Code Online (Sandbox Code Playgroud)
填充它:
INSERT INTO scale_data
SELECT sections.sections, sections.sections*10000 + gen.gen
, CEIL(RANDOM()*100)
FROM GENERATE_SERIES(1, 300) sections,
GENERATE_SERIES(1, 90000) gen
WHERE gen <= sections * 300;
Run Code Online (Sandbox Code Playgroud)
它生成了 13545000 条记录。
综合指数就可以了:
CREATE INDEX id1_id2_idx
ON public.scale_data
USING btree
(id1, id2);
Run Code Online (Sandbox Code Playgroud)
并选择#1:
select id2 from scale_data
where id2 in (50)
order …Run Code Online (Sandbox Code Playgroud) postgresql performance index postgresql-9.5 postgresql-performance