Re: Different performance of two simple queries - Mailing list pgsql-novice

From Jayadevan M
Subject Re: Different performance of two simple queries
Date
Msg-id CAFS1N4hyJ6RWy9JncZ6eKouPcddHH9PFCrhu_i7bjMezGESh0Q@mail.gmail.com
Whole thread Raw
In response to Different performance of two simple queries  ("petrov.boris.v@mail.ru" <petrov.boris.v@mail.ru>)
Responses Re: Different performance of two simple queries  ("petrov.boris.v@mail.ru" <petrov.boris.v@mail.ru>)
List pgsql-novice


On Tue, Nov 17, 2015 at 3:59 PM, petrov.boris.v@mail.ru <petrov.boris.v@mail.ru> wrote:
Hi all.
Two queries return same result. The first one always takes about 7ms, the second 1.5ms.

Query 1:
-------------------------
select
c2c.position, c2c.category, c.*
from categories_companies c2c
join companies c on c2c.company = c.id
where c2c.category ~ 'otdelka_i_remont.*'::lquery
order by c2c.position, c.id
limit 20 offset 1760;
-------------------------

Query 2:
-------------------------
with cte as (
    select c2c.position, c2c.company, c2c.category
    from categories_companies c2c
    where c2c.category ~ 'otdelka_i_remont.*'::lquery
    order by c2c.position, c2c.company, c2c.category
    limit 20 offset 1760
)
select c2c.position, c2c.category, c.*
from cte c2c
join companies c on c2c.company = c.id;
-------------------------

Indexes:
- categories_companies.category (c2c.category) is of type ltree, indexed by both gist and btree
- categories_companies (c2c) have composite PK of company and category
- companies.id (c.id) is PK, no explicit indexes created

Questions:
1. Is this is normal, or I done something incorrectly?
2. What can I do to make first query perform as fast as the second one?

I would say it is normal. Please read this for explanation :

Thanks,
Jayadevan

pgsql-novice by date:

Previous
From: "petrov.boris.v@mail.ru"
Date:
Subject: Different performance of two simple queries
Next
From: "petrov.boris.v@mail.ru"
Date:
Subject: Re: Different performance of two simple queries