0

I have a table with several hundred million rows. One of the columns is `status` varchar(10).

Most values in the status are 1 character, some varying up to 10. However a subset of the values has a pattern of its own. A whole group of status values begin with a single character c followed by a number ranging from 0 to 10,000.

I would like to index this column with the following:

ALTER TABLE tbl ADD KEY (status(1), status);

This would be better than having two individual keys, one on status(1) (first character of the whole column) and second status. Together they would always be faster.

However MySQL prohibits me from creating such:

ERROR 1060 (42S21): Duplicate column name 'status'

How can I solve this?

2 Answers 2

1

There's really no reason to index status(1) independently of status. One index created on status should handle both situations equally well.

Sign up to request clarification or add additional context in comments.

3 Comments

To me that seems like having an index for each individual name in the phonebook -- would take comparably long to search the index as the database. Is this wrong thinking?
@Mikhail Yes, that thinking is wrong. Any search on status, independent of how it is formatted, can take advantage of the index.
I understand that it can take advantage of it. What you're saying is that looking up the first character, and then the rest of the value isn't any faster?
1

You could create a second column in your table and populate it with the first character of the other column and then create an index on each. However, this might have poor selectivity and not be all that useful.

2 Comments

I thought about that, but to take advantage of this new column I would have to rewrite all the code that uses this
If you are doing a like 'c%' type select, it isn't buying you anything to make a separate index as they will be ordered together. If, however, you wanted to search for the last character, then something like splitting off a second column might be useful since using a % at the head of a like requires scanning ALL the items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.