I'm using this query to search by "first and last name" or by "last and first name":
SELECT
"id",
"firstname",
"lastname"
FROM
"plyers"
WHERE
lower( REPLACE ( CONCAT ( lastname, firstname ), ' ', '' ) ) LIKE '%joh%'
OR
lower( REPLACE ( CONCAT ( firstname, lastname ), ' ', '' ) ) LIKE '%joh%'
ORDER BY "id" DESC
GIVEN THAT
I'm using Postgresql 13
User can search with "
John Belushi" or "Belushi John" or "lushi" or "lushi jo"The search text (
johin this case) is alwayslowered()and cleaned of every space: before, during and after each word. E.g. if I write "lushi JO" the final search text is: "lushijo"This query is used A LOT to search for players in a list and for the automatic completion of some selects
QUESTIONS
Do you think it's a good query?
Do you think there is a way I can avoid
CONCAT(lastname,firstname)andCONCAT(firstname,lastname)?What indexes should I create? I read this: https://stackoverflow.com/a/2709967/10088259 but I didn't quite understand how to use it.