I’d like to ask 2 questions about indexes in MySQL:
1. is it possible to have index of type HASH in MyISAM engine?
2. is it possible to create index on computed value like e.g.
CREATE INDEX AD_MAIN_CATEGORY ON ad ((category_id % 100) ASC) USING BTREE;
I’ve got query that selects all categories and counts number of ads in main/sub categories. Each ad has only subcategory_id from which main_category_id can be computed (subcategory_id = 120 => main_category_id = 100). The evil (slow) query:
SELECT `c`.*, (SELECT COUNT(*) FROM ad WHERE IF(c.parent_id = 0,(ad.category_id DIV 100) * 100 = c.id,ad.category_id=c.id)) AS `count`, `ct`.`label` FROM `category` AS `c` INNER JOIN `category_translation` AS `ct` ON c.id = ct.category_id WHERE (ct.language_id = 1) ORDER BY `c`.`id` ASC
Question 1:
No, see another question about it: Why does MySQL not have hash indices on MyISAM or InnoDB?
Question 2:
Yes and No. No, you cannot in (mainstream) MySQL but yes, you can on MariaDB, (a fork or MySQL). Read about: Virtual columns