Re: index on search - pg 9.2

From: John R Pierce <pierce(at)hogranch(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: index on search - pg 9.2
Date: 2017-03-15 03:23:36
Message-ID: 5f93cc16-1bee-7bea-8521-5a8d9ff3a879@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 3/14/2017 7:18 PM, Patrick B wrote:
>
> SELECT j.id <http://j.id>, ff.gtime
> FROM public.status AS s
> JOIN public.job AS j ON j.status_label_id = s.id <http://s.id> AND
> j.clientid = 3369
> JOIN public.log AS ff ON ff.jobid = j.id <http://j.id>
> AND ff.clientid = 3369
> AND (ff.description LIKE '%change%')
> ORDER BY gtime DESC
> LIMIT 100
>
>
> Explain analyze: https://explain.depesz.com/s/1OLW
>
> I'm using PG 9.2 and, read about gin indexes.
> I've created the index to test, but the query is not using it.
>
> create index on log gin (description gin_trgm_ops)
>
>
> Can you guys help to improve that part please?

that index won't be of any use if ff.clientid=3369 selects fewer records
than (ff.description like '%change%') would on its own. and you don't
even have a WHERE clause, you piled all your conditions on the JOIN ON
clauses. the only things there that are actually join clauses are the
things that join two tables, such as ff.jobid=j.id

functionally, your query is equivalent to...

SELECT j.id <http://j.id>, ff.gtime
FROM public.status AS s
JOIN public.job AS j ON j.status_label_id = s.id <http://s.id>
JOIN public.log AS ff ON ff.jobid = j.id <http://j.id>
WHERE j.clientid = 3369 AND ff.clientid = 3369
AND (ff.description LIKE '%change%')
ORDER BY gtime DESC
LIMIT 100

--
john r pierce, recycling bits in santa cruz

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Kretschmer 2017-03-15 06:50:58 Re: controlled switchover with repmgr
Previous Message Patrick B 2017-03-15 02:18:36 index on search - pg 9.2