简体   繁体   中英

Indexing a Varchar with MySQL

I have a table foo like this:

id | column1
1  | normalized_string_format1
2  | normalized_string_format1
3  | normalized_string_format2
4  | normalized_string_format3

The queries on the table are always:

SELECT * FROM foo WHERE column1 = 'some_string'

and

INSERT INTO foo (column1) VALUES ('some_string')

Note that two first rows in the example above has normalized_string_format1 so column1 is not unique. Should I do create some index in column1 in order to increase the performance? in such case, which one index should I choise?

If table foo is non-trivial, then adding an index on column1 will likely benefit performance.

You could create an index like this:

CREATE INDEX foo_IX1 ON foo (column1) 

The INSERT/UPDATE/DELETE statements will do more work, to maintain the index entries.

But the SELECT statement with an equality predicate on column1 will be able to use an index range scan operation, which means that it can avoid inspect every flipping row in the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM