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
3
u/Lost_Term_8080 Dec 09 '25
It doesn't prevent locking the tables, it ignores everyone else's locks. Sprinkling in nolock everywhere can actually create blocking in the write queries, it will still take shared locks out itself.
In regards to the recommendations to use RCSI - you can use this safely the vast majority of cases but not always, there are potential race conditions RCSI can introduce you have to test for. You would be extremely unlucky to experience them, but the problems could be big if you do.
For me, using isolation would be my last move to mitigate blocking. I would first look at tuning the queries and indexes and only then look to isolation level. You can use snapshot isolation at the query level. More anomalies are possible with snapshot isolation that RCSI, but for one query that is only doing reads that already has read uncommitted enabled on it, you should end up in a better position with snapshot isolation than you are in with read uncommitted. You still have to test.
By all means, if you have the ability to thoroughly test your application with RCSI, do it. It will pay off, but a lot of shops aren't prepared for that.