简体   繁体   English

MySQL WHERE IF条件下的查询非常慢

[英]MySQL very slow query with WHERE IF condition

Referring below queries. 参考以下查询。 The 1st query runs very slow even though the logic is same like the 2nd query. 即使逻辑与第二个查询相同,第一个查询的运行速度也非常慢。 Tables table01 and table02 have about ~15000 records each. 表table01和table02分别具有约15000条记录。 So what is exactly the problem here? 那么,这到底是什么问题呢? I already exhausted of idea. 我已经筋疲力尽了。

-- 1st. Very slow
hardcoded here for easier reference
SELECT a.eventid, d.fileno
FROM table01 a LEFT JOIN table02 d ON a.eventid=d.eventid
WHERE IF('fileno'='fileno',d.fileno = 'FIL123',0=0)
-- original: WHERE IF(search_field='fileno',d.fileno = 'BC12449',0=0)
-- search_field is passsed from stored procedure

-- 2nd. Fast
SELECT a.eventid, d.fileno
FROM table01 a LEFT JOIN table02 d ON a.eventid=d.eventid
WHERE d.fileno = 'FIL123'

Any indexes you may have on columns which names you generate inside the if clause will not be able to be used in the first example. 您可能在if子句中生成名称的列上可能具有的任何索引将无法在第一个示例中使用。

In general, wraping a column name inside any function with mysql will negate indexing benifits on the column 通常,用mysql将列名包装在任何函数中将否定列的索引受益

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

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