In my Azure hosted ASP.NET Core site I have a table of users and I implemented search as follows:
var inner = from user in db.Users
select
SQL Server has to use a scan to find rows matching the .Contains clause. There is no way around this.
However, if we reduce the amount of data that SQL server has to scan, we will speed up the query.
An index is "covering" if it contains all the data needed to be returned in a query.
CREATE INDEX IX_User_Name_filtered ON USER ([Verified], [Name])
INCLUDE ( [PhotoURL], [Id], [Subdomain], [Deleted], [AppearInSearch] )
WHERE [AppearInSearch]=1 AND [Deleted]=0
This index is likely substantially smaller than the original table, so even if a scan is required, it will be quicker.
Depending on the plan that is generated, this index may be a better choice. it doesn't include the extra columns and will be smaller still. Testing will be required to determine the best choice.
CREATE INDEX IX_User_Name_filtered ON USER ([Verified], [Name])
WHERE [AppearInSearch]=1 AND [Deleted]=0