Avoid to use "IN", "NOT IN" in SQL statement, try to use "LEFT JOIN" instead.

When ever you write any SQL statements, try to avoid use "IN", "NOT IN" keyword, even "EXISTS", "NOT EXISTS"

Most of people think "EXISTS" is better than "IN", becuase it will use index, but the truth is no matter you're using "EXISTS" or "IN", SQL Server will use index to boost performance automatically, at least in SQL Server 2008 R2.

Well, "LEFT JOIN" is better than "IN" because... check the picture below, saw that "NESTED LOOPS" activity? that's a big nono~~~The first query takes less than 1 seconds, and the second query takes almost 4 minutes, and I am only talking about only 7000 records join 139526 records.

SQL

Happy Coding Everyday~ 快乐编码,享受生活~
原文地址:https://www.cnblogs.com/tedzhang/p/2982526.html