Re: two table join with order by on both tables attributes

Поиск
Список
Период
Сортировка
От Evgeniy Shishkin
Тема Re: two table join with order by on both tables attributes
Дата
Msg-id F5D93B74-237C-4595-B159-9D90CCC47448@gmail.com
обсуждение исходный текст
Ответ на Re: two table join with order by on both tables attributes  (Evgeniy Shishkin <itparanoia@gmail.com>)
Ответы Re: two table join with order by on both tables attributes
Список pgsql-performance
> On 08 Aug 2014, at 03:43, Evgeniy Shishkin <itparanoia@gmail.com> wrote:
>
>>>> select * from users join  notifications on users.id=notifications.user_id ORDER BY users.priority desc
,notifications.prioritydesc limit 10; 
>>
>>> In my understanding, i need to have two indexes
>>> on users(priority desc, id)
>>> and notifications(user_id, priority desc)
>>> then postgresql would choose nested loop and get sorted data from indexes.
>>> But it wont.
>>
>> Indeed.  If you think a bit harder, you'll realize that the plan you
>> suggest would *not* produce the sort order requested by this query.
>> It would (if I'm not confused myself) produce an ordering like
>>  users.priority desc, id asc, notifications.priority desc
>> which would only match what the query asks for if there's just a single
>> value of id per users.priority value.
>>
>> Offhand I think that the planner will not recognize a nestloop as
>> producing a sort ordering of this kind even if the query did request the
>> right ordering.  That could perhaps be improved, but I've not seen many
>> if any cases where it would be worth the trouble.
>

And actually with this kind of query we really want the most wanted notifications, by the user.
So we really can rewrite to order by users.priority desc, id asc, notifications.priority desc according to business
logic.
And we will benefit if this case would be improved.

В списке pgsql-performance по дате отправления:

Предыдущее
От: Evgeniy Shishkin
Дата:
Сообщение: Re: two table join with order by on both tables attributes
Следующее
От: Marti Raudsepp
Дата:
Сообщение: Re: two table join with order by on both tables attributes