简体   繁体   中英

Sql “IN” is slower then simple `field_name`=“some”. Why?

Why my MySQL query

SELECT ... WHERE `field_name`="some";

is processed faster then

SELECT ... WHERE `field_name` IN( "some" );

'IN' uses binary search, while the simple field_name ="some" uses the simple line-by-line searching. I am right, amn't I?

PS: it was tested in the table with < 100 lines. Maybe IN spends more time to "takeoff", but in big tables works faster?

Associated quastion, - does binary search applies either to IN values, or to lines in the table, or maby to both?

The IN statement in MySQL presumes an array of possible values that will be compared against every single row on the table/result set. While the WHERE clause uses single value comparison, uses indexes (if available) and mysql is optimizing the query in most of the cases.

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