mysql - alternative / efficient / optimized query
I want alternative / efficient / optimized query for following query:
Table:
CREATE TABLE `bartco_web_vms_studio`.`table_name` (
`index` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`id1` INT( 10 ) NOT NULL DEFAULT '0',
`id2` V开发者_StackOverflow社区ARCHAR( 10 ) NOT NULL,
`f3` TINYINT( 4 ) NOT NULL DEFAULT '0',
PRIMARY KEY ( `index` )
) ENGINE = MYISAM ;
Composite index:
CREATE INDEX id1_id2 ON tablename (id1, id2);
Number of rows = 7891
Update query:
UPDATE table_name
SET f3=1
WHERE id1=1 AND id2='a'
OR id1=2 AND id2='b'
OR id1=3 AND id2='c'
Basically I have to update a field's value based on 2 fields (id1, id2) values. These 2 fields can be in more than 1 pair.
Output from EXPLAIN SELECT f3 FROM table_name WHERE ...
:
id -> 1,
select type -> SIMPLE,
table -> table_name,
type -> range,
possible_keys -> id1_id2,
key -> id1_id2,
key_len -> 261,
ref -> NULL,
rows -> 2,
Extra -> Using where
Thanks a lot for help
Regards
Composite index id1 + id2
and query
WHERE (id1, id2) IN ((1, 'a'), (2, 'b'), (3, 'c'))
should help
Also, when you're even testing - add more rows into the table.
Since you're selecting 3 rows of 9 - mysql can decide never to use index at all.
UPD:
if nothing from our answers will help - you can always split your query to 3:
SELECT * FROM table WHERE id1 .. AND id2 ..
UNION
SELECT * FROM table WHERE id1 .. AND id2 ..
...
精彩评论