user2363025
user2363025

Reputation: 6505

simple select query optimise

I have a table defined as follows:

   CREATE TABLE IF NOT EXISTS `cards` (
`ID` int(11) NOT NULL,
  `Name` varchar(200) NOT NULL,
  `WorkerID` varchar(20) NOT NULL,
  `pic` varchar(200) NOT NULL,
  `expDate` bigint(20) NOT NULL,
  `reminderSent` tinyint(4) NOT NULL,
  `regNum` varchar(8) NOT NULL,
  `cardType` varchar(200) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=92 DEFAULT CHARSET=latin1;


ALTER TABLE `cards`
 ADD PRIMARY KEY (`ID`), ADD KEY `cardsWorkerID_idx` (`WorkerID`);

But running:

explain
SELECT pic, expDate, Name, ID, cardType, regNum FROM cards WHERE workerID= 18

tells me it is scanning the entire table, even though I added an index to the workerID field. Can anyone explain what I'm missing?

Upvotes: 1

Views: 46

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269453

The use of indexes depends on the size of the data. It also depends on the types used for the comparison. If you have a small table, then the SQL engine might decide that a scan is more efficient than using the index. This is particularly true if the table fits on a single data page.

In your case, though, the problem is might be data conversion. Use the appropriate typed constant for the comparison:

SELECT pic, expDate, Name, ID, cardType, regNum
FROM cards
WHERE workerID = '18';

Upvotes: 1

Related Questions