Answer the question
In order to leave comments, you need to log in
How to force Postgres to use indexes?
Hello. There is this table:
CREATE TABLE public.core_entry (
id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('core_entry_id_seq'::regclass),
keyword CHARACTER VARYING(100) NOT NULL,
created TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE INDEX core_entry_e2fa5388 ON core_entry USING BTREE (created);
CREATE INDEX keyword_gist_idx ON core_entry USING GIST (keyword);
SELECT keyword FROM core_entry WHERE keyword='something';
SELECT keyword FROM core_entry WHERE keyword LIKE 'something';
SELECT keyword, count(keyword) as count FROM core_entry WHERE keyword LIKE '%something%' GROUP BY keyword;
CREATE INDEX keyword_idx ON core_entry USING BTREE (keyword);
SELECT keyword FROM core_entry WHERE keyword='something';
Answer the question
In order to leave comments, you need to log in
Make 2 tables something like
create table core_keywords(
keyword_id serial primary key,
keyword varchar(100)
);
create unique index u_idx_keyword on core_keywords(lower(keyword));
create index t_idx_keywords on core_keywords using gin (lower(keyword) gin_trgm_ops);
create table core_keywords_entry(
keyword_id int not null references core_keywords,
created_at timestamp with time zone not null default CURRENT_TIMESTAMP,
primary key (keyword_id, created_at)
);
select
e.keyword_id,
count(*)
from core_keywords_entry as e
join core_keywords as k using(keyword_id)
where lower(k.keyword) like '%something%'
group by 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question