简体   繁体   English

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

[英]Simple MySQL WHERE query not using Index


Quick Answer: Query condition needs to be same type as column to use index. 快速解答:查询条件需要与使用索引的列相同。 I was trying to search a CHAR column with a numeric condition. 我试图用数字条件搜索CHAR列。


I have a table with 15 million rows. 我有一张1500万行的表。 I have a column called 'ticket' that can occur multiple times but it wouldn't occur very many times . 我有一个名为'ticket'的列可以多次出现,但不会发生很多次。 . . probably less than 10. I have created an index on this column but the explain command says that when I am using a simple 'WHERE ticket =' query that it is not using the index. 可能少于10.我在这个列上创建了一个索引,但是explain命令说当我使用一个简单的'WHERE ticket ='查询时它没有使用索引。 Obviously this confuses me as I am asking a question here. 显然这让我感到困惑,因为我在这里问一个问题。

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$$

My query: 我的查询:

SELECT *
FROM   company1.rTable
WHERE  ticket = 3478421;

Maybe there is a way to change the query to use the index? 也许有办法改变查询使用索引? I tried things like 'AND ticket > . 我试过像'AND ticket>这样的东西。 . .' “。 as a guess but that didn't help. 作为猜测,但这没有帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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