patternsqlMinor
index mysql concatenated columns
Viewed 0 times
indexmysqlconcatenatedcolumns
Problem
I have a table for author names with two fields, first_name & last_name, both varchar(55), both nullable (for crazy business logic reasons) although in reality last_name is unlikely to ever be null.
My where clause for the search screen contains:
so that "Twain" or "Mark Twain" can be searched on. The table has about 15,000 rows & is expected to gradually grow, but won't ever be more than double that, and not for years.
I understand that there are many other parts of my query that will affect this, but given just that information, how might I best index this?
If it would make a great difference in performance, making last_name not nullable is an option, but not first_name
TIA!
My where clause for the search screen contains:
WHERE CONCAT(a.first_name, " " , a.last_name) LIKE "%twain%"so that "Twain" or "Mark Twain" can be searched on. The table has about 15,000 rows & is expected to gradually grow, but won't ever be more than double that, and not for years.
I understand that there are many other parts of my query that will affect this, but given just that information, how might I best index this?
If it would make a great difference in performance, making last_name not nullable is an option, but not first_name
TIA!
Solution
Two options I can think of. First, if you are on MyISAM or InnoDB 5.6+, you could store the concatenation in a separate field and use a FULLTEXT index on that field.
The other option is to index the first_name and last_name fields separately. Then change your query to:
Removing the wildcard from the beginning will allow the indexes to be used.
The other option is to index the first_name and last_name fields separately. Then change your query to:
WHERE a.first_name LIKE "twain%" OR a.last_name LIKE "twain%"Removing the wildcard from the beginning will allow the indexes to be used.
Code Snippets
WHERE a.first_name LIKE "twain%" OR a.last_name LIKE "twain%"Context
StackExchange Database Administrators Q#45840, answer score: 2
Revisions (0)
No revisions yet.