in reply to Faster of two options for finding keywords in a database

I don't know the PG features you are using, I can only guess that the fields text and keywordhash are both the same JSON string.

Using a regex here is not a wise decision, even with proper delimiters (which are dangerously lacking).

If destructing¹ the hash is fine I'd try delete @kws{@kwlist} (see delete and hash slice) and calculate the difference of scalar keys %kws before and after.

But - as you already mentioned- I suppose PG has already a built-in hash slice feature you could use for faster execution.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

update

FWIW your use of defined is also error prone, exists it's the way to go.

Footnotes

¹) and if destructing is not ok consider delete local @kws{@kwlist} , DISCLAIMER I've never used it, wasn't even actively aware of it.

Replies are listed 'Best First'.
Re^2: Faster of two options for finding keywords in a database
by cormanaz (Deacon) on Feb 19, 2024 at 18:20 UTC
    "FWIW your use of defined is also error prone, exists it's the way to go." More about this, please?
      As soon as undef is used as value, your code will break. Better be safe than sorry.

      Debugger demo:

      DB<2> $h{a}=1 DB<3> $h{b}=undef DB<4> x grep {defined $h{$_}} "a".."d" 0 'a' DB<5> x grep {exists $h{$_}} "a".."d" 0 'a' 1 'b' DB<6>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery