Using MySQL 5.6.23, I recently changed a prepared statement from:
SELECT * FROM table WHERE BINARY column = :boundValue
to:
SELECT * FROM TABLE WHERE CAST(column AS BINARY) = :boundValue
According to the MySQL docs on BINARY:
BINARY str is shorthand for CAST(str AS BINARY)
So I expected these two prepared statements to have the same run-time performance. However, the CAST version was significantly slower. I mean orders of magnitude difference.
The query plans are identical:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: table
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 6
Extra: Using where
CREATE TABLE `session_detail_binary` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`column` varchar(64) NOT NULL DEFAULT '',
`foo` varchar(64) DEFAULT NULL,
`bar` varbinary(4096) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_column` (`column`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
There are about 260k rows in this table.
What's going on here?
UPDATE: I believe this was a red herring. At the same time this SELECT .. WHERE x runs, there were a lot of UPDATE .. WHERE x on the same table and under the same where condition, where "a lot" means 100x more. This suggests to me the row locks were handicapping the selection.
SELECT * FROM table WHERE column = BINARY :boundValuecreate tablestatement? Any chance for the column already beingBINARYso the first case being no-op whereas the second one being explicit function call?varchar. I believe, though, there were other factors at play and this was just a red herring -- see the OP update.