[Solved] How to search for a full name when first name and last name are stored in a different columns
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

How to search for a full name when first name and last name are stored in a different columns

Database type:

How can I search for a person full name if I have first name stored in a column called "first_name" and the last name is stored in a column called "last_name"

Note that this table has couple million records so I need an efficient way to do the search. and I am using MySQL Server

the column first_name and the column last_name are both type VARCHAR(80).

I have tried the following so far which works but slow because it ignores the indexes because of the concat function

SELECT first_name, phone FROM people
WHERE CONCAT_WS(' ',first_name, last_name) like '%John Smith%'

I also have tried to add index full text index on( first_name, last_name) and then this query

SELECT *
FROM people
WHERE MATCH(first_name, last_name) AGAINST('John Smith')

but it is not the fastest query. is there a better approach to this problem?

Thanks

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Calling Functions With Indexed Columns (query line: 7): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `first_name` is indexed, the index won’t be used as it’s wrapped with the function `CONCAT_WS`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 7): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `last_name` is indexed, the index won’t be used as it’s wrapped with the function `CONCAT_WS`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid LIKE Searches With Leading Wildcard (query line: 7): The database will not use an index when using like searches with a leading wildcard (e.g. '%John Smith%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
The optimized query:
SELECT
        people.first_name,
        people.phone 
    FROM
        people 
    WHERE
        CONCAT_WS(' ', people.first_name, people.last_name) LIKE '%John Smith%'

Related Articles



* original question posted on StackOverflow here.