简体   繁体   English

两组带有在MySQL中使用索引进行连接的相同表; 一个需要全表扫描

[英]Two sets of indentical tables with joins using indexes in MySQL; one requires full-table scan

Im getting very confused with indexes in MySQL. 我对MySQL中的索引感到非常困惑。

I have two tables: TableA1 and TableA2. 我有两个表:TableA1和TableA2。

I created indexes on these for the joins between them and queries run really fast. 我为它们之间的连接创建了索引,查询运行得非常快。

I have another 2 tables with an identical setup eg TableB1 and TableB2. 我还有另外两个具有相同设置的表,例如TableB1和TableB2。 The only difference is that these tables have a few null values. 唯一的区别是这些表具有一些空值。

For some reason the same query on TableA is about 5 times faster and doesn't require a full-table scan. 由于某种原因,在TableA上执行相同的查询大约要快5倍,并且不需要全表扫描。 TableB however seems to use a full table scan and I can't work out why. 但是,TableB似乎使用了全表扫描,我不知道为什么。 Could it be the Null values that are causing this? 可能是Null值导致了这一点吗?

I notice using EXPLAIN that on the TableA setup, I get possible keys: myindex and also ref: func ; 我注意到使用EXPLAIN可以在TableA设置上获得possible keys: myindexref: func ; however on the TableB setup I get just possible keys: NULL and ref: NULL . 但是在TableB设置上,我只得到possible keys: NULLref: NULL

I have searched around for quite a while on this, and cannot seem to find a relevant answer. 我已经搜索了很长时间,似乎找不到相关的答案。 Would appreciate if someone can point me in the right direction. 如果有人能指出我正确的方向,将不胜感激。

(Sorry, now added to the original question.) (对不起,现在已添加到原始问题。)

Here is TableAOne: 这是TableAOne:

CREATE TABLE `TableAOne` (
  `field1` varchar(255) DEFAULT NULL,
  `field2` varchar(255) DEFAULT NULL,
  KEY `myindex` (`field1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And TableATwo: 和TableATwo:

CREATE TABLE `TableATwo` (
  `Field3` varchar(255) ,
  `Field4` varchar(255) ,
  `Field5` varchar(255) ,
  `id` int(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15522 DEFAULT CHARSET=utf8;

I am joining Field3 with Field1. 我正在将Field3与Field1一起加入。

The tables that get the full-table scan are identical to the above tables. 获得全表扫描的表与上述表相同。 The only difference is they are called TableB instead of TableA. 唯一的区别是它们被称为TableB而不是TableA。

Cheers, 干杯,
Ke

I just deleted the table and re-installed it to the db (i did change utf8 to latin1 not sure if this made a difference),however it works now, really strange! 我只是删除了表,然后将其重新安装到数据库(我确实将utf8更改为latin1,不确定这是否有所不同),但是现在可以正常工作了,真的很奇怪! thanks v much for all the replies they certainly pushed me towards the answer cheers :) 非常感谢所有答复,他们肯定使我朝着答案欢呼:)

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

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