Adding index to mysql dynamic column
I have a select query which has a where clause: WHERE CONCAT(att.subjectId,'#',att.classId) IN ('132#100') . I have added a composite index on subjectId and classId but will these indexes be of any use for such a dynamic column. If not is it possible to create a column on a dynamic column like C开发者_如何学编程ONCAT(att.subjectId,'#',att.classId).
Best I'm aware MySQL offers no means to create an index on an expression/function result.
The work around is to populate an extra field using triggers, and to index it.
Probably, functions are not the best idea for "where" clauses, they force dbms to do "full scan" which kills performance.
I think it's better to use few "(att.subjectId = '132' and att.classId = '100')" concatenated with "OR" (for each term in your "IN" list) and add some indexes for separated columns.
精彩评论