I have a query like this:
SELECT *
FROM view_name
WHERE column1 = something AND column2 = somethingelse
The underlying table has indexes on both column1 and column2, but MySQL is using the wrong index. How do I force it to use the right index. Something like this:
SELECT *
FROM view_name USE INDEX (table_name.column2)
WHERE column1 = something AND column2 = somethingelse
Edit: Someone asked for this:
SHOW CREATE VIEW view1
CREATE ALGORITHM=UNDEFINED DEFINER=`x`@`localhost` SQL SECURITY DEFINER VIEW `view1` AS select `table1`.`id` AS `identifier`,`table1`.`col1` AS `column1`,`table1`.`col2` AS `column2` from `table1`
SHOW CREATE TABLE table1
CREATE TABLE `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`col1` varchar(100) NOT NULL,
`col2` varchar(100) NOT NULL,
`col3` varchar(100) NOT NULL,
`col4` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `col1` (`col1`),
KEY `col2` (`col2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Edit: More detailed explanation:
If I do this:
SELECT * FROM table1 WHERE col1 = x AND col2 = y
MySQL chooses the index on col1 and I get poor performance.
If I do this:
SELECT * FROM table1 USE INDEX (col2) WHERE col1 = x AND col2 = y
MySQL uses the index on col2 and I get great performance.
How do I do the same thing, but on the view? Something like this:
SELECT * FROM view1 USE INDEX (column2) WHERE column1 = x AND column2 = y
How do I tell MySQL what index to use when running the query on the view?
Edit: I just found this:
Views do not have indexes, so index hints do not apply. Use of index hints when selecting from a view is not permitted.
https://dev.mysql.com/doc/refman/5.7/en/view-restrictions.html
So, maybe what I want is not possible.