简体   繁体   中英

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?

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM