1

I have a table which looks like this:

id price barcode
1 300 A_100-15437859603-233
2 200 A_123-49875452222-128
3 180 A_231-21284568323-367
4 150 B_122457

Having two (or more) formats of data in the "barcode" column. Now my queries look like this:

SELECT * FROM my_table
WHERE barcode like 'A_%' AND
SUBSTRING(barcode, '-(.*?)-')='15437859603'

In order to find the first row for example. This table has tens of millions of rows, how can I speed up this regex search in PostgreSQL? Can I create an index on SUBSTRING(barcode, '-(.*?)-')?

2
  • 1
    Would it allready help just to expand the LIKE() operator. For example something like like 'A[_]%[-]15437859603[-]%'? Commented Sep 16, 2021 at 5:43
  • @JvdV Good idea, but this would also be a sequential scan right? How to avoid that? Commented Sep 16, 2021 at 5:46

1 Answer 1

1

Yes, you can create an index on SUBSTRING(barcode, '-(.*?)-').

To support the first condition, you should change it to substr(barcode, 1, 2) = 'A_', then you can use the following index to support the query ideally:

CREATE INDEX ON my_table (
   SUBSTRING(barcode, '-(.*?)-'),
   substr(barcode, 1, 2)
);

If the first condition always compares with 'A_', you could also use

CREATE INDEX ON my_table (SUBSTRING(barcode, '-(.*?)-'))
   WHERE substr(barcode, 1, 2) = 'A_';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.