Search code examples
sqlsql-serverentity-frameworkfull-text-searchazure-cognitive-search

SQL Indexes and Multicolumn Database search


I have to implement search that finds substring in the name of the user. User has FirstName and LastName in 2 columns. It is good enough to do WHERE FirstName LIKE '%searchText%' OR LastName LIKE '%searchText%'

What is my problem that I want to solve is performance. Let's say that currently I expect like 1000 users tops. I do not want to search to take ages. So I thought of indexes (those columns will not change much, I expect that the value of these columns will almost never change). I know that I will be looking for both columns I need multi column index.

Is this correct way of doing this? Or it is better to use SQL full text search for this (please provide some good link)? Would it be better to create a View where FirstName and LastName will be concatenated and search there? Or it is better to just use e.g. Azure Search (Currently, this is the only and it may be the last entity I would need to search for)?

I am using .NET 4.6 and EntityFramework hosted on Azure web apps.

Thanks


Solution

  • Because of the leading wildcard character, an index will not be used. The only time an index will be used is if the wildcard is either in the middle or at the end of the string. Adding one index on LastName, FirstName can be a good suggestion as well if your table is very wide, like David Browne said.

    If you are truly needing to search with both wildcards (i.e. a partial match), then I would take a look at the amount of data in your table. If it's just a few thousand rows we're talking about, a table scan will still be very fine performance wise. If we're talking about something like 50.000 rows or more, then a full text index would be best. This seems like a good tutorial on the matter: https://learn.microsoft.com/en-us/sql/relational-databases/search/get-started-with-full-text-search