Re: Finding rows with text columns beginning with other text columns

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Christoph Zwerschke <cito(at)online(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Finding rows with text columns beginning with other text columns
Date: 2010-05-10 21:34:58
Message-ID: 6BA3143F-C2FD-4A2E-9C42-B73970FD08A5@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On 10 May 2010, at 21:24, Christoph Zwerschke wrote:

> Am 10.05.2010 11:50 schrieb Alban Hertroys:
> > On 10 May 2010, at 24:01, Christoph Zwerschke wrote:
> >
> >> select * from b join a on b.txt like a.txt||'%'
> >>
> >> I feel there should be a performat way to query these entries,
> >> but I can't come up with anything. Can anybody help me?
> >
> > Have you tried using substring instead of like?
>
> How exactly? I tried this:
>
> substr(b.txt, 1, length(a.txt)) = a.txt
>
> but it cannot be optimized and results in a nested loop, too.

I feared as much, but it was worth a try.

Thinking more on the issue, I don't see a way to prevent the nested loop as there's no way to decide beforehand what part of the string to index for b.txt. It depends on a.txt after all.

You would basically need a cross-table index, those are not supported. If it were, you could create a functional index of substrings of b.txt with string lengths from a.txt (eeps, that'd be a table product!).

Your best solution is probably to add a column to b that contains the substring of b.txt that would match a.txt.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4be87c0b10418212361837!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2010-05-10 21:58:28 Re: Sorting with materialized paths
Previous Message Alban Hertroys 2010-05-10 21:22:20 Re: Sorting with materialized paths