MySQL问题与select查询和';订购人';条款

MySQL问题与select查询和';订购人';条款,mysql,mariadb,Mysql,Mariadb,针对MySQL数据库(InnoDB)的SELECT查询遇到了一个奇怪的问题 以下查询错误地返回了1条匹配记录: select `ID` from `AccessTables` where `numTableID` = 14 AND `numUserCatID` IN (7,253) AND (`numUpdateCat` = 2 OR `numUpdateItems` = 2) order by `ID` asc limit 1 而以下查询没有正确返回匹配的记录:

针对MySQL数据库(InnoDB)的
SELECT
查询遇到了一个奇怪的问题

以下查询错误地返回了1条匹配记录:

select `ID` 
from `AccessTables` 
where `numTableID` = 14 
    AND `numUserCatID` IN (7,253) 
    AND (`numUpdateCat` = 2 OR `numUpdateItems` = 2) 
order by `ID` asc 
limit 1
而以下查询没有正确返回匹配的记录:

select `ID` 
from `AccessTables` 
where `numTableID` = 14 
    AND `numUserCatID` IN (7,253) 
    AND (`numUpdateCat` = 2 OR `numUpdateItems` = 2) 
limit 1
如您所见,这些查询之间的唯一区别是“orderby”子句

查询中请求的
ID
列是表的自动生成主键列

在第一次查询中返回的记录是一条记录,如果'or'子句周围没有括号,则可以找到该记录。 但是查询的这一部分周围有括号,所以我不明白为什么会在这里返回这个记录。然后,仅当查询中存在“ORDERBY”子句时

正在使用的MySQL版本是:MySQL服务器:5.5.32-MariaDB-log

这里有人能解释一下这个问题吗?提前谢谢

(编辑:省略括号返回一行,但这是第一次查询返回的另一行)

(第2条编辑:没有,但是……)

编辑第3条,针对真实对象运行这些查询:

问题1:

EXPLAIN EXTENDED SELECT `ID` 
FROM `AccessTables` 
WHERE `numTableID` = 14 
    AND `numUserCatID` IN (7,253) 
    AND (`numUpdateCat` = 2 OR `numUpdateItems` = 2) 
ORDER BY `ID` ASC 
LIMIT 1;
输出:

"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where; Using filesort"
输出:

"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where; Using filesort"
输出:

"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where; Using filesort"
输出:

"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numTableID"    "8" \N  "136"   "11.03" "Using where"
"id"    "select_type"   "table" "type"  "possible_keys" "key"   "key_len"   "ref"   "rows"  "filtered"  "Extra"<br />
"1" "SIMPLE"    "AccessTables"  "range" "numUserCatID,numTableID,numUpdateCat,numUpdateItems"   "numUserCatID"  "8" \N  "20"    "75.00" "Using index condition; Using where; Using filesort"
“id”“选择类型”“表”“类型”“可能的密钥”“密钥”“密钥长度”“参考”“行”“过滤”“额外”
“1”“SIMPLE”“AccessTables”“range”“numUserCatID、numTableID、numUpdateCat、numUpdateItems”“numUserCatID”“8”\N“20”“75.00”“使用索引条件;使用where;使用文件排序”
因此,返回正确结果(找到0条记录)的查询似乎与numUserCatID上的索引一起工作,而返回错误结果(找到1条记录)的查询似乎与numTableID上的索引一起工作

奇怪

第5条编辑:
按另一列排序,例如dateInsert(表示将记录插入表中的时刻的日期/时间戳),也会更改查询结果。
然后,再次没有返回任何记录,并且使用的索引也是numUserCatID上的索引

我们使用的是“order by
ID
asc”,因为我们假设ID总是表示记录插入数据库的顺序。
但是dateInsert在我们的案例中基本上是一样的。


当使用常规键列而不是主键进行排序时,大型数据库中会有性能损失吗?

如果真的发生这种情况,那就是错误。使用“真的”我的意思是,这些查询正是您发送到数据库服务器的查询,而底层表在此期间尚未更新

这里有一个类似(但不完全相同)的问题:

请向MariaDB团队报告该问题


要解决眼前的问题,请尝试重写查询,例如在中不使用
,替换

AND numUserCatID IN (7,253) 
与:


并检查是否得到相同的错误结果

我在这方面与MarjaR合作过,经过测试:MariaDB 5.5中的这个错误在5.5.37中得到了解决

我们是否有可能获得该行的数据副本,或者至少是与问题相关的列?这两个查询是自动生成的(在某些应用程序中)还是在受信任的环境中运行(例如,使用mysql cli)看到那个奇怪的结果了吗?这好像是个错误。向发送错误报告。您可以向我们显示返回的行吗?(我的意思是所有列,而不仅仅是
ID
。表使用的是哪个引擎?
AccessTables
是基表还是视图?重写查询(从而丢失IN子句)似乎可以解决问题。我建议您在问题中添加
EXPLAIN EXTENDED
的输出,用于所有3个版本的查询(问题中的第2个和第3个不在。)如果你也可以在那里设置一个小脚本,它会更好(尽管错误不会显示在那里,没有问题,其他人可以在他们的机器中复制脚本并复制错误。)谢谢。请使用此链接编辑问题,并解释机器的输出。