Query performance with concatenation and LIKE
While not a concrete answer, the following might help you to reach some conclusions:
Calling
concat
to concatenate the three strings, or using the||
operator, results in postgres having to allocate a new buffer to hold the concatenated string, then copy the characters into it. This has to be done for each row. Then the buffer has to be deallocated at the end.In the case where you are ORing together three conditions, postgres may only have to evaluate only one or maybe two of them to decide if it has to include the row.
It is possible that expression evaluation using the
||
operator might be more efficient, or perhaps more easily optimizable, compared with a function call toconcat
. I would not be surprised to find that there is some special case handling for internal operators.As mentioned in the comments, your sample is too small to make proper conclusions anyway. At the level of a fraction of a millisecond, other noise factors can distort the result.
What you have observed so far is interesting but hardly important. Minor cost overhead for concatenating strings.
The much more important difference between these expressions does not show in your minimal test case without indexes.
The first two examples are not sargable (unless you build a tailored expression index):
where concat(last_name, ' ', first_name, ' ', middle_name) like '%Ива%'
where (last_name || ' ' || first_name || ' ' || middle_name) like '%Ива%'
While this one is:
where last_name like '%Ива%' or first_name like '%Ива%' or middle_name like '%Ива%'
I.e., it can use a plain trigram index to great effect (order of columns is unimportant in a GIN index):
CREATE INDEX some_idx ON person USING gin (first_name gin_trgm_ops
, middle_name gin_trgm_ops
, last_name gin_trgm_ops);
Instructions:
- PostgreSQL LIKE query performance variations
Incorrect test if NULL is possible
concat()
is generally slightly more expensive than simple string concatenation with ||
. It is also different: If any of the input strings is NULL, the concatenated result is also NULL in your second case, but not in your first case, since concat()
just ignores NULL values - but you'd still get a useless space character in the result.
Detailed explanation:
- Combine two columns and add into one new column
If you are looking for a clean, elegant expression (about the same cost), use concat_ws()
instead:
concat_ws( ' ', last_name, first_name, middle_name)