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
33
u/IndependentTrouble62 Dec 06 '25
If your DBA lets you use no lock he is an idiot. No lock is an absolute terrible practice for a myriad of reasons. Dirty reads not even being the worst. Please google SQL Server no lock and then stop using it.
10
u/Chris_PDX Dec 06 '25
I'd be more concerned that your DBA didn't get an eye twitch from using the nolock hint.
Unless you're in an environment where integrity doesn't matter, nolock can lead to dirty reads and inconsistent results if it's a high transaction system.
To your primary question, there's no difference at all in the two queries. Query plan will probably show an extra select and sort but the primary work is the same. Query 2 is just harder to read and maintain for zero gain.
8
u/SQLBek 1 Dec 06 '25
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."
They are wrong. Everything about those statements are utterly WRONG. They know NOTHING about how the query optimizer or the storage engine works.
7
u/slash_gnr3k Dec 06 '25
I'd be more worried about the mindless nolock
As for the other spurious claims, use SET STATISTICS IO, TIME ON as well as the actual execution plan to compare IO, CPU, elapsed times and memory grants which should provide you the evidence you need
6
u/Khmerrr Dec 06 '25
Always is not a word a professional would put into a phrase.
And please stop using nolock...
5
u/heeero__ Dec 06 '25
I would just show your dba the execution plan between the two methods. I agree nolock should be avoided.
4
u/IanYates82 Dec 06 '25
I'm reading on my phone so maybe am missing something, but your dba is wrong. The queries are the same. There's no "security" thing at work here. Show the query plan for both and you'll see they're identical
7
u/No-Adhesiveness-6921 Dec 06 '25
You should ask your DBA to come tell us why he thinks NOLOCK and dirty reads are ok.
2
u/jwk6 Dec 06 '25
No lock = I don't give a fuck (about existing locks)
Your DBA needs to study why it's almost always a bad idea, and read about locks and Isolation Levels.
Locks are how RDBMSs maintain the integrity and consistency of your data.
There are a few legit use cases, like checking an error log table in a highly concurrent system with many users/sessions. Or, if you want a quick row count from a busy production database. Otherwise, do not use it! Ever!
1
u/CPDRAGMEISH Dec 06 '25 edited Dec 06 '25
ZERO DIFF
USE WideWorldImportersGOSELECTsctt.CustomerTransactionID, sctt.TransactionDate, sc.CreditLimitFROMSales.CustomerTransactions sctt JOIN Sales.Customers sc ON sctt.CustomerID = sc.CustomerIDSELECTsctt.CustomerTransactionID, sctt.TransactionDate, sc.CreditLimitFROM(SELECT CustomerTransactionID, TransactionDate, CustomerID FROM Sales.CustomerTransactions) scttJOIN(SELECT CreditLimit, CustomerID FROM Sales.Customers) scON sctt.CustomerID = sc.CustomerIDPlease compare
StmtText Q1---------------------------------------------------------------------------------------------------------------------------------------|--Hash Match(Inner Join, HASH:([sc].[CustomerID])=([sctt].[CustomerID]))|--Clustered Index Scan(OBJECT:([WideWorldImporters].[Sales].[Customers].[PK_Sales_Customers] AS [sc]))|--Clustered Index Scan(OBJECT:([WideWorldImporters].[Sales].[CustomerTransactions].[CX_Sales_CustomerTransactions] AS [sctt]))(3 rows affected)StmtText Q2-------------------------------------------------------------------------------------------------------------------------------------------------------------|--Hash Match(Inner Join, HASH:([WideWorldImporters].[Sales].[Customers].[CustomerID])=([WideWorldImporters].[Sales].[CustomerTransactions].[CustomerID]))|--Clustered Index Scan(OBJECT:([WideWorldImporters].[Sales].[Customers].[PK_Sales_Customers]))|--Clustered Index Scan(OBJECT:([WideWorldImporters].[Sales].[CustomerTransactions].[CX_Sales_CustomerTransactions]))1
u/ometecuhtli2001 Dec 07 '25 edited Dec 07 '25
In addition to what’s already been said here, if you want a case in point as far as NOLOCK, my company has 180+ warehouses, 6000 users, and the company that wrote the warehouse management system we use has a policy of using NOLOCK in their code. It’s all over all the views, functions, and stored procedures. Guess what? There’s still major blocking about 25% of the time during regular business hours. So NOLOCK doesn’t actually accomplish anything useful. Add to that the potential problems it can create and you can see why everyone here recommends against using it.
I noticed you mentioned this DBA has 20 years of experience. Where did this experience come from? Have they kept up with developments in SQL Server? If you take execution plans for both the queries you have in your post and they come out identical, show those to him and the developers and ask them to explain how this happened. IF they can explain it, it’s probably going to be a bullshit answer. Just because this person has 20 years of experience doesn’t mean they’re any good at their job! So if they’re wrong about this, what else are they wrong about?
Also, what version and edition of SQL Server is this? How big (row count, data size, index size) is this table? What kind of storage is used for the database (mechanical hard disks, SSDs, SAN)?
1
u/dfintegra Dec 07 '25
I won’t mention the nolock thing because I see you got your ass kicked enough on that. For your original question he is incorrect for more modern SQL Servers. Maybe this was a technique back in SQL 7.0 but if you look at the execution plan or the statistics IO, you are going to receive the same results. The non-combative way to approach this would probably be to ask if he can show you an example of the results in numbers.
1
u/alissa914 Dec 07 '25
The statement about "we can hide the information of other columns associated in that table" seems odd. Like, if you have the ability to run the query and see the tables, then you can just SELECT * that whole table... if you're that tight on RAM, maybe the answer is to get more RAM since you don't have enough or have it set too low.
Once I worked in a job where DBAs decided that they'd rather pay for the number of connections available instead of a core license. When it went live, they had query issues all day because they had to go around counting the number of connections open... and it got so tedious and cost prohibitive that they went for core licensing where the number of connections are unlimited (in theory). Never had a problem since.
If you're focused on low RAM usage and not on performance.... you don't have enough RAM.
1
u/srussell705 Dec 09 '25
How old is this version of SQL? If it has a "year" as a version, the implied locking is no longer present.