r/SQLServer • u/sanjay-kumar_ • Dec 06 '25
Discussion Sql server good query pratice
I have a query like
--Query 1: Select a.column1 , a.column2 , b.column1 From table1 as a with(nolock) Inner join Table2 as b with(nolock) on a.column3 = b.column3
My co-worker and dba in my company saying that this is not good practice and the query should be like
--Query 2: Select a.column1 , a.column2 , b.column1 From (Select column1 , column2 , column3 from table1 with(nolock)) As a Inner join (Select column1 , column3 from table2 with(nolock)) As b on a.column3 = b.column3
What they are saying is "Direct join will consume more memory or Ram but derived will take only required memory.
Derived query is always best. We can hide the information of other columns associated in that table. Which means security."
Is this true? Advance thanks for the information and Forgive me if any miss information is there , typo mistake, and any grammatical mistakes
Edit: You can see nolock in the select query This is because the table is constantly updating and may be selected by UI query to get the data ( this is what dba says) And also he says that dirty reads is ok when compared to blocks in the database with out using nolock
So we use read with (nolock) to avoid block in the database
View all comments
Show parent comment
8
u/Naive_Moose_6359 Dec 06 '25
This somewhat made sense in SQL 7.0 but is outdated. Please look up read committed snapshot. There is no modern reason to use nolock for any real purpose and certainly not as a "best practice" everywhere.
Also, the other statement from this person is also complete nonsense. Without looking at the query plan, no statement like that makes any sense. Second, the compile and optimize phases in SQL normalize more syntactic thing like this and you have to try hard to find a way to "defeat" it.
Don't listen to this person, honestly.