I recall in Oracle it is possible to index based on a function, e.g. SUBSTRING(id,1,8).
Does MySQL support this? If not, is there is any alternative?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, not in a general sense, I don’t believe even 5.6 (the latest version when this answer was first written) has this functionality. It’s worth noting that 8.0.13 and above now support functional indexes, allowing you to achieve what you need without the trigger method described below.
If you are running an older version of
mysql, it is possible to only use the leading part of a column (this functionality has been around for a long time), but not one starting at the second or subsequent characters, or any other more complex function.For example, the following creates an index using the first five characters of a name:
For more complex expressions, you can achieve a similar effect by having another column with the indexable data in it, then using insert/update triggers to ensure it’s populated correctly.
Other than the wasted space for redundant data, that’s pretty much the same thing.
And, although it technically violates 3NF, that’s mitigated by the use of triggers to keep the data in sync (this is something that’s often done for added performance).