开发者

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

开发者 https://www.devze.com 2022-12-19 23:28 出处:网络
Im getting very confused with indexes in MySQL. I have two tables:TableA1 and TableA2. I created indexes on these for the joins between them and queries run really fast.

Im getting very confused with indexes in MySQL.

I have two tables: TableA1 and 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 e.g. TableB1 and 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. TableB however seems to use a full table scan and I can't work out why. Could it be the Null values that are causing this?

I notice using EXPLAIN that on the TableA setup, I get possible keys: myindex and also ref: func; however on the TableB setup I get just possible keys: NULL and ref: 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:

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

And Tabl开发者_JAVA技巧eATwo:

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.

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.

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! thanks v much for all the replies they certainly pushed me towards the answer cheers :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号