What is the best way to implement a substring search in SQL?

前端 未结 4 2135
失恋的感觉
失恋的感觉 2020-12-08 11:55

We have a simple SQL problem here. In a varchar column, we wanted to search for a string anywhere in the field. What is the best way to implement this for performance? Obvio

4条回答
  •  無奈伤痛
    2020-12-08 12:29

    First, maybe this is an issue with a badly designed table that stores a delimited string in one field instead of correctly designing to make a related table. If this is the case, you should fix your design.

    If you have a field with long descriptive text (saya a notes field) and the search is always by whole word, you can do a full-text search.

    Consider if you can require your users to at least give you the first character of what they are searching for if it is an ordinary field like Last_name.

    Consider doing an exact match search first and only performing the wildcard match if no results are returned. This will work if you have users who can provide exact matches. We did this once with airport name searches, it came back really fast if they put inthe exact name and slower if they did not.

    If you want to search just for strings that are not words that may be somewhere in the text, you are pretty much stuck with bad performance.

提交回复
热议问题