简体   繁体   中英

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. 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.

In general, wraping a column name inside any function with mysql will negate indexing benifits on the column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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