TODO item: Implement Boyer-Moore searching in LIKE queries

From: Karan Sikka <karanssikka(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: TODO item: Implement Boyer-Moore searching in LIKE queries
Date: 2016-08-01 17:19:32
Message-ID: CALkFZpcbipVJO=xVvNQMZ7uLUgHzBn65GdjtBHdeb47QV4XzLw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi pgsql-hackers,

Following the patch to implement strpos with Boyer-Moore-Horspool,
it was proposed we bring BMH to LIKE as well.

Original thread:
https://www.postgresql.org/message-id/flat/27645(dot)1220635769%40sss(dot)pgh(dot)pa(dot)us#27645(dot)1220635769(at)sss(dot)pgh(dot)pa(dot)us

I'm a first time hacker and I found this task on the TODO list. It seemed
interesting and isolated enough. But after looking at the code in
like_match.c, it's clearly a non-trivial task.

How desirable is this feature? To begin answering that question,
I did some initial benchmarking with an English text corpus to find how much
faster BMH (Boyer-Moore-Horspool) would be compared to LIKE, and the results
were as follows: BMH can be up to 20% faster than LIKE, but for short
substrings, it's roughly comparable or slower.

Here are the results visualized:

http://ctrl-c.club/~ksikka/pg/like-strpos-short-1469975400.png
http://ctrl-c.club/~ksikka/pg/like-strpos-long-1469975400.png

Data attached, and description of the benchmark below.

I'd love to hear your thoughts:
- is the benchmark valid? am I missing anything?
- what conclusions can we draw from the results?
- what action should we take from here?
- is this the right mailing list for this discussion?

Thank you!
- Karan, pg community newbie

--- Benchmark Details ---

The easiest way to approximate the potential speedup from BMH,
is to compare the performance of the following queries:

1. SELECT count(*) FROM test_table WHERE text LIKE '%substring%';
2. SELECT count(*) FROM test_table WHERE strpos('substring', text) > 0;

We expect the strpos query to outperform the LIKE query since strpos is
implemented with BMH.

I loaded up the database with chunks of english text from the bible, a
commonly
used corpus for simple text analysis. The exact procedure is described in
more
detail at the end of the email. I ran a few queries, using short substrings
and
long substrings, the choice of which is discussed in more detail below.

## Choice of test data

BMH is known to be particularly fast on english text, so I loaded the test
table
with 5k-character chunks of text from the bible. BMH is expected to be
slower
for small substrings where the overhead of creating a skip table may not be
justified. For larger substrings, BMH may outperform LIKE due to the skip
table.
In the best case, if a text character does not exist in the substring, the
substring can jump length-of-substring characters, skipping what would be a
lot
of work.

I chose small (< 15 character) substrings to be the most popular bigrams and
trigrams in the text. I chose long (10-250 character) substrings at
random, and
took varied-length prefixes and suffixes to see how it affected algorithm
performance. The reason that prefixes were not sufficient is that BMH
compares
from the right end of the substring, unlike the LIKE algorithm. The full
strings are included in the attached excel file.

## Database setup

Download the corpus (englishTexts) from
http://www.dmi.unict.it/~faro/smart/corpus.php

Create the table:

CREATE TABLE test_table (text text);

Generate the rows for insertion (generates chunks of 5k characters):

wget http://ctrl-c.club/~ksikka/pg/gen_rows.py
python gen_rows.py path/to/englishTexts/bible.txt > path/to/output/file

Load the table:

COPY test_table (text) from '/absolute/path/to/previous/output/file'
WITH ENCODING 'UTF-8';

I ran the COPY command 21 times to exacerbate performance.

Queries were timed with psql's \timing feature. The mean of five queries was
reported.

## Hardware

Apple Macbook Air (Mid 2012)
CPU: 1.8 GHz Intel Core i5
RAM: 4 GB 1600 MHz DDR3

Attachment Content-Type Size
gen_rows.py text/x-python-script 1.9 KB
like-strpos-comparison.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 53.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2016-08-01 17:24:10 Re: [Patch] Temporary tables that do not bloat pg_catalog (a.k.a fast temp tables)
Previous Message David Fetter 2016-08-01 17:19:25 Re: New version numbering practices