简单的MySQL WHERE查询不使用索引

时间:2013-05-14 19:05:16

标签: mysql indexing


快速回答:查询条件需要与列类型相同才能使用索引。我试图用数字条件搜索CHAR列。


我有一张包含1500万行的表格。我有一个名为'ticket'的列可以多次出现,但不会发生很多次。 。 。可能少于10.我在这个列上创建了一个索引,但是explain命令说当我使用一个简单的'WHERE ticket ='查询时它没有使用索引。显然这让我感到困惑,因为我在这里问一个问题。

CREATE TABLE company1.rTable (
  `aDate` date DEFAULT NULL,
  `fromD` date DEFAULT NULL,
  `pNo` int(10) DEFAULT NULL,
  `ticket` char(11) DEFAULT NULL,
  `r` int(11) unsigned NOT NULL DEFAULT '0',
  `line` tinyint(3) unsigned DEFAULT NULL,
  `nNum` char(11) NOT NULL,
  `pKey` char(7) DEFAULT NULL,
  `modNum` char(4) DEFAULT NULL,
  `dnum` smallint(5) unsigned NOT NULL,
  `rdNum` smallint(5) unsigned DEFAULT NULL,
  `pType` int(10) DEFAULT NULL,
  `lNum` decimal(9,2) DEFAULT NULL,
  `lineAmount` decimal(9,2) DEFAULT NULL,
  `amount` decimal(9,2) DEFAULT NULL,
  `amount1` decimal(9,2) DEFAULT NULL,
  `amount2` decimal(9,2) DEFAULT NULL,
  `amount3` decimal(9,2) DEFAULT NULL,
  `amount4` decimal(9,2) DEFAULT NULL,
  `amount5` decimal(9,2) DEFAULT NULL,
  `amount6` decimal(9,2) DEFAULT NULL,
  `amount7` decimal(9,2) DEFAULT NULL,
  `amount8` decimal(9,2) DEFAULT NULL,
  `amount9` decimal(9,2) DEFAULT NULL,
  `amount10` decimal(9,2) DEFAULT NULL,
  `tType` tinyint(4) DEFAULT NULL,
  `lineB` decimal(9,2) DEFAULT NULL,
  `lineD` char(1) DEFAULT NULL,
  `lineP` decimal(9,2) DEFAULT NULL,
  `lineI` decimal(9,2) DEFAULT NULL,
  `lineC` decimal(9,2) DEFAULT NULL,
  `lineW` decimal(9,2) DEFAULT NULL,
  `lineR` decimal(9,2) DEFAULT NULL,
  `lineM` decimal(9,2) DEFAULT NULL,
  KEY `rADate` (`aDate`),
  KEY `rTType` (`tType`),
  KEY `rTicket` (`ticket`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

我的查询:

SELECT *
FROM   company1.rTable
WHERE  ticket = 3478421;

也许有办法更改查询以使用索引?我试过像'AND ticket>这样的东西。 。 “。作为猜测,但这没有帮助。

1 个答案:

答案 0 :(得分:2)

ticket是一个char列,您正在使用数字条件(3478421)。

相关问题