How does SQL engines differ when we use equal sign and IN operator have same value? Does execution time changes?
1st one using equality check operator
WHERE column_value = 'All'
2nd one using OR operator and single value
WHERE column_value IN ('All')
Does SQL engine changes IN
to =
if only one value is there?
Is there any difference for same in MySQL and PostgreSQL?
There is no difference between those two statements, and the optimiser will transform the
IN
to the=
whenIN
have just one element in it.Though when you have a question like this, just run both statements, run their execution plan and see the differences. Here - you won't find any.
After a big search online, I found a document on SQL to support this(I assume it applies to all DBMS):
Here is the link to the document.
Here is the execution plan of both queries in Oracle (Most DBMS will process this the same) :
And for
IN()
:As you can see, both are identical. This is on an indexed column. Same goes for an unindexed column (just full table scan) .
You will need to run execution plan on both, and see the results.
I believe they will have the same execution plan as it will be performed the same as a normal
=
sign when only one value is placed inside theIN()
statement.There is no reason for the optimizer to behave any differently on a query like this.
Just to add a different perspective, one of the main points of rdbms systems is that they will rewrite your query for you, and pick the best execution plan for that query and all equivalent ones. This means that as long as two queries are logically identical, the should always generate the same execution plan on a given rdbms.
That being said, many queries are equivalent (same result set) but only because of constraints the database itself is unaware of, so be careful about those cases (E.g for a flag field with numbers 1-6, the db doesn't know
<3
is the same asin (1,2)
). But at the end of the day, if you're just thinking about legibility ofand
andor
statements it won't make a difference for performance which way you write them.For single IN Clause,there is no difference..below is demo using an EMPS table i have..
Predicate for First Query in execution plan:
Predicate for second query in execution plan:
If you have multiple values in IN Clause,its better to convert them to joins
Teach a man to fish, etc. Here's how to see for yourself what variations on your queries will do:
And let's try it the other way:
You can read here about how to interpret the results of a mysql
EXPLAIN
request. For now, note that we got identical output for both queries: exactly the same "execution plan" is generated. Thetype
row tells us that the query uses a non-unique index (a foreign key, in this case), and theref
row tells us that the query is executed by comparing a constant value against this index.There is no difference when you are using it with a single value. If you will check the table scan, index scan, or index seek for the above two queries you will find that there is no difference between the two queries.
No it would not have any difference on the two engines(Infact it would be same for most of the databases including SQL Server, Oracle etc). Both engines will convert
IN
to=