Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's faster IN or OR?

In T-SQL what's faster?

DELETE * FROM ... WHERE A IN (x,y,z)

Or

DELETE * FROM ... WHERE A = x OR A = y OR A = z

In my case x, y and z are input parameters for the stored procedure. And I'm trying to get the performance of my DELETE and INSERT statements to the best of my abilities.

like image 842
Zyphrax Avatar asked Jul 13 '09 14:07

Zyphrax


People also ask

Which is faster in or exists in SQL?

The EXISTS clause is faster than IN when the subquery results are very large. The IN clause is faster than EXISTS when the subquery results are very small.

WHERE or having Which is faster?

"Having" is slower if we compare with large amount of data because it works on group of records and "WHERE" works on number of rows.. Save this answer.

Is in faster than or in MySQL?

As MongoDB stores a large volume of unstructured data and follows a document-based storage approach, it's relatively faster than MySQL. It means MongoDB stores data in a single document for an entity and helps in faster data read or write. Features like replication can be a big reason for this.

Which is faster not exists or not in?

NOT IN vs NOT EXISTS performance in SQL Server Regarding performance aspects, SQL NOT EXISTS would be a better choice over SQL NOT IN. NOT EXISTS is significantly faster than NOT IN especially when the subquery result is very large.


3 Answers

Don't think; profile.

I urge you not to rely on intuition, yours or anyone else's, when considering questions of speed. Instead, try both options, with some kind of profiling/run time measurement, and find out which is faster in your circumstances.

like image 140
chaos Avatar answered Oct 21 '22 12:10

chaos


"IN" will be translated to a series of "OR"s...if you look at the execution plan for a query with "IN", you'll see it has expanded it out.

Much cleaner to use "IN" in my opinion, especially in larger queries it makes it much more readable.

like image 12
AdaTheDev Avatar answered Oct 21 '22 12:10

AdaTheDev


Write two stored procedures, one using IN, the other using OR, on a test server. Run each procedure 10,000 (or 1,000,000, or whatever) times, and compare the timings.

In general, this is pretty much the "only" way to have a good answer to the question of which approach is faster: write simple timing test cases, and run them many, many times.

like image 4
Chris Jester-Young Avatar answered Oct 21 '22 10:10

Chris Jester-Young