Why mysql in query is faster than or

in the absence of an index, it is true that using in in mysql is faster than using or, but I don"t understand why. Please give me some advice.

Jul.11,2021

it's like you go to the construction site to move bricks

< H2 > IN < / H2 >

foreman IN said: if you go to construction site A today, 1000 will be enough.

< H2 > OR < / H2 >

foreman OR said: I don't know where to move or how much I should move today. I'll tell you what, you go to construction site A, then to construction site B, and then to construction site N. go quickly and move more, the boss is happy.

who will be tired to death in the end.


SELECT * FROM tblName WHERE key IN(a1, a2, ..., an);

if the index is not needed, traverse the key, of the whole table to match A1, a2,., an

  • OR, is a match from A1, failed to match, to match a2, until the match is successful or none of them are matched, and the time complexity is O (n)
  • IN, first changes A1, a2,., an into a binary tree, and searches it through the binary tree. The time complexity is O (log n)
  • .

therefore, the efficiency of IN is higher than that of OR,. With the increase of the range of parameter sets, the performance of IN will not decline too much, while the performance of OR will decline very much.


in the absence of an index, the efficiency of in will not decline much with the increase of the amount of data behind in or or, but the performance of or will deteriorate greatly as there are more records and more fields connected (1or2or3or4or.).

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c304-2c2ec.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c304-2c2ec.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?