In SqlServer 2017 we have a table with millions of rows. The table has a few dozen columns. One of the columns is a varchar(50)
with a well defined range of allowed values, the value of this column is guaranteed to be one string from a list of about 4,000 strings. The only queries we care to optimize against this table all exclusively deal with the rows that have just one of those values, but they need to retrieve all of the columns of those rows. Does it make sense to put an index on that column and if so which type of index should be used?
Example Schema:
+---------------------------------------+
| Id | Category | ... 20 other columns|
+------+----------+---------------------+
| 1 | Food | ..... |
+------+----------+---------------------+
| 2 | Lumber | ..... |
+------+----------+---------------------+
Example Query: select * from table where Category = 'food';
So, in this example, the Category
column contains one string from a list of about 4000. I've considered a clustered index, but this column is not unique. I'd add a non-clustered index, but the query calls for all columns to be returned so it has to go from the index back to the main table to retrieve all of the data anyway, right? Then do we have to use a full table index or is there a better option?
Category
into another table (e.g.Category
), add another column that utilizes aForeign Key
to that look-up table, and re-write the queries so that you can create a non-clustered index on an integer?