简体   繁体   English

为什么MySQL查询返回零行,但是在进行碎片整理后仍然有效?

[英]Why does MySQL query return zero rows, but after defrag it works?

I have an InnoDB, MySQL table and this query returns zero rows: 我有一个InnoDB,MySQL表,此查询返回零行:

SELECT id, display FROM ra_table WHERE parent_id=7266 AND display=1;

However, there are actually 17 rows that should match: 但是,实际上有17行应该匹配:

SELECT id, display FROM ra_itable1 WHERE parent_id=7266;

ID    display
------------------
1748  1
5645  1
...

There is an index on display (int 1), and ID is the primary key. 显示屏上有一个索引(整数1),ID是主键。 The table also has several other fields which I'm not pulling in this query. 该表还具有其他几个字段,我不在此查询中提取。

After noticing this query wasn't working, I defragmented the table and then the first query started working correctly, but only for a time. 注意到此查询无法正常工作后,我对表进行了碎片整理,然后第一个查询开始正常工作,但只持续了一段时间。 It seems after a few days, the query stops working again and I have to defragment to fix it. 几天后,查询似乎再次停止工作,我必须进行碎片整理才能修复它。

My question is, why does the fragmented table break this query? 我的问题是,为什么碎片表会中断此查询?

Additional info: MySQL 5.6.27 on Amazon RDS. 附加信息:Amazon RDS上的MySQL 5.6.27。

CREATE TABLE `ra_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(6) NOT NULL,
`display` int(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
KEY `display` (`display`),
) ENGINE=InnoDB AUTO_INCREMENT=13302 DEFAULT CHARSET=latin1 
ROW_FORMAT=DYNAMIC

There may be a bug in the version you are running. 您正在运行的版本中可能存在错误。

Meanwhile, change 同时,改变

INDEX(parent_id),
INDEX(display)

to

INDEX(parent_id, display)

By combining them, the query will run faster (and hopefully correctly). 通过组合它们,查询将运行得更快(并有望正确运行)。 An index on a flag ( display ) is likely to never be used. 标志( display )上的索引可能永远不会使用。

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

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