The list of values in the IN predicate is better to list from more frequently used to less. Then if the compared value is in the list, it will be selected faster. If the value is not in the list, the entire list will still be viewed.
A smart optimizer based on statistics (distribution of values) could change the order of the values, but I have not heard of that. Yes, and overhead costs need to be assessed.
UPD. Made a simple test. Since I mainly work with SQL Server, I’m on it.
Data generation:
create table Tin (id int) declare @N int=1000000 declare @i int =1 set nocount on; while @i <100 begin insert into Tin values( @i ) set @i = @i +1 end while @i <=1000000 begin insert into Tin values(999) set @i = @i +1 end
Queries:
select * from tin where ID in(100,101,102,103,104,105,106,107,108,109,999) go select * from tin where ID in(999,100,101,102,103,104,105,106,107,108,109)
Results (the second query shows consistently better results for elapsed time):
SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. (999901 row(s) affected) SQL Server Execution Times: CPU time = 1248 ms, elapsed time = 17998 ms. SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 0 ms. (999901 row(s) affected) SQL Server Execution Times: CPU time = 1264 ms, elapsed time = 15905 ms. SQL Server parse and compile time: CPU time = 0 ms, elapsed time = 57 ms.
UPD2 Must give up his assumption, at least for SQL Server. The plan has a sorted list of values in IN. Probably, the server sorts the values before comparing. So with a large number of runs, the results should be almost the same.