Queries for NOLOCK Table Hint
1. Complex Reporting with Aggregations and Joins
SQL Server Query 1
SELECT
c.CategoryName,
SUM(od.Quantity * od.UnitPrice * (1 - od.Discount)) AS TotalSalesValue
FROM Categories c WITH (NOLOCK)
JOIN Products p WITH (NOLOCK) ON c.CategoryID = p.CategoryID
JOIN [Order Details] od WITH (NOLOCK) ON p.ProductID = od.ProductID
JOIN Orders o WITH (NOLOCK) ON od.OrderID = o.OrderID
GROUP BY c.CategoryName
ORDER BY TotalSalesValue DESC;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.Select()
.Column("c.CategoryName")
.Column(new SUM(new ColumnArithmatic("od.Quantity").MULTIPLY("od.UnitPrice").MULTIPLY()
.StartBracket(1).SUBTRACT("od.Discount").EndBracket()), "TotalSalesValue")
.From("Categories", "c", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Products","p", new List<IHint>() { new NOLOCK() })
.On(new Column("c.CategoryID").Equale(new Column("p.CategoryID"))),
new INNERJOIN().TableName("[Order Details]", "od", new List<IHint>() { new NOLOCK() })
.On(new Column("p.ProductID").Equale(new Column("od.ProductID"))),
new INNERJOIN().TableName("Orders", "o", new List<IHint>() { new NOLOCK() })
.On(new Column("od.OrderID").Equale(new Column("o.OrderID")))
})
.GroupBy(new GroupBy("c.CategoryName"))
.OrderBy(new OrderBy().SetColumnDescending("TotalSalesValue"))
.Build();
Query build by SqlQueryBuilder 1
SELECT c.CategoryName,
SUM(od.Quantity * od.UnitPrice * (@pMAIN_2512060921007033930 - od.Discount)) AS TotalSalesValue
FROM Categories AS c WITH (NOLOCK)
INNER JOIN
Products AS p WITH (NOLOCK)
ON c.CategoryID = p.CategoryID
INNER JOIN
[Order Details] AS od WITH (NOLOCK)
ON p.ProductID = od.ProductID
INNER JOIN
Orders AS o WITH (NOLOCK)
ON od.OrderID = o.OrderID
GROUP BY c.CategoryName
ORDER BY TotalSalesValue DESC;
Parameters (If used)
| Name |
Value |
| @pMAIN_2512060921007033930 |
1 |
Query Results 1:
| |
CategoryName |
TotalSalesValue |
| 1 |
Beverages
|
267868.180522919
|
| 2 |
Dairy Products
|
234507.285217285
|
| 3 |
Confections
|
167357.224831581
|
| 4 |
Meat/Poultry
|
163022.359088898
|
| 5 |
Seafood
|
131261.73742485
|
| 6 |
Condiments
|
106047.084989548
|
| 7 |
Produce
|
99984.5800685882
|
| 8 |
Grains/Cereals
|
95744.587474823
|
2. Checking Product Stock and Order Status during Peak Hours
SQL Server Query 2
SELECT
s.CompanyName AS Supplier,
SUM(p.UnitsInStock) AS TotalUnitsInStock,
SUM(p.UnitsOnOrder) AS TotalUnitsOnOrder
FROM Suppliers s WITH (NOLOCK)
JOIN Products p WITH (NOLOCK) ON s.SupplierID = p.SupplierID
GROUP BY s.CompanyName
ORDER BY s.CompanyName;
Create SQL query with SqlQueryBuilder 2
var (sql2, parameters2) = new SqlQueryBuilder()
.Select()
.Column("s.CompanyName","Supplier")
.Column(new SUM(new Column("p.UnitsInStock")), "TotalUnitsInStock")
.Column(new SUM(new Column("p.UnitsOnOrder")), "TotalUnitsOnOrder")
.From("Suppliers","s", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Products","p", new List<IHint>() { new NOLOCK() })
.On(new Column("s.SupplierID").Equale(new Column("p.SupplierID")))
})
.GroupBy(new GroupBy("s.CompanyName"))
.OrderBy(new OrderBy().SetColumnAscending("s.CompanyName"))
.Build();
Query build by SqlQueryBuilder 2
SELECT s.CompanyName AS Supplier,
SUM(p.UnitsInStock) AS TotalUnitsInStock,
SUM(p.UnitsOnOrder) AS TotalUnitsOnOrder
FROM Suppliers AS s WITH (NOLOCK)
INNER JOIN
Products AS p WITH (NOLOCK)
ON s.SupplierID = p.SupplierID
GROUP BY s.CompanyName
ORDER BY s.CompanyName ASC;
Parameters (If used)
Query Results 2:
| |
Supplier |
TotalUnitsInStock |
TotalUnitsOnOrder |
| 1 |
Aux joyeux ecclésiastiques
|
86
|
0
|
| 2 |
Bigfoot Breweries
|
183
|
0
|
| 3 |
Cooperativa de Quesos 'Las Cabras'
|
108
|
30
|
| 4 |
Escargots Nouveaux
|
62
|
0
|
| 5 |
Exotic Liquids
|
69
|
110
|
| 6 |
Forêts d'érables
|
130
|
0
|
| 7 |
Formaggi Fortini s.r.l.
|
23
|
110
|
| 8 |
Gai pâturage
|
98
|
0
|
| 9 |
G'day, Mate
|
58
|
0
|
| 10 |
Grandma Kelly's Homestead
|
141
|
0
|
| 11 |
Heli Süßwaren GmbH & Co. KG
|
140
|
0
|
| 12 |
Karkki Oy
|
132
|
60
|
| 13 |
Leka Trading
|
70
|
10
|
| 14 |
Lyngbysild
|
100
|
70
|
| 15 |
Ma Maison
|
136
|
0
|
| 16 |
Mayumi's
|
98
|
0
|
| 17 |
New England Seafood Cannery
|
208
|
0
|
| 18 |
New Orleans Cajun Delights
|
133
|
100
|
| 19 |
Nord-Ost-Fisch Handelsgesellschaft mbH
|
10
|
0
|
| 20 |
Norske Meierier
|
164
|
0
|
| 21 |
Pasta Buttini s.r.l.
|
57
|
10
|
| 22 |
Pavlova, Ltd.
|
110
|
10
|
| 23 |
PB Knäckebröd AB
|
165
|
0
|
| 24 |
Plutzer Lebensmittelgroßmärkte AG
|
205
|
80
|
| 25 |
Refrescos Americanas LTDA
|
20
|
0
|
| 26 |
Specialty Biscuits, Ltd.
|
74
|
50
|
| 27 |
Svensk Sjöföda AB
|
224
|
50
|
| 28 |
Tokyo Traders
|
64
|
20
|
| 29 |
Zaanse Snoepfabriek
|
51
|
70
|
3. Using NOLOCK in a Subquery for "Approximate" Comparisons
SQL Server Query 3
SELECT
p.ProductName,
p.UnitPrice,
c.CategoryName,
(SELECT AVG(p2.UnitPrice) FROM Products p2 WITH (NOLOCK) WHERE p2.CategoryID = p.CategoryID) AS AverageCategoryUnitPrice
FROM Products p WITH (NOLOCK)
JOIN Categories c WITH (NOLOCK) ON p.CategoryID = c.CategoryID
WHERE p.UnitPrice > (SELECT AVG(p3.UnitPrice) FROM Products p3 WITH (NOLOCK) WHERE p3.CategoryID = p.CategoryID) * 1.5 -- 50% higher than category average
ORDER BY p.UnitPrice DESC;
Create SQL query with SqlQueryBuilder 3
var (sql3, parameters3) = new SqlQueryBuilder()
.Select()
.Columns("p.ProductName","p.UnitPrice","c.CategoryName")
.Column(new SqlQueryBuilder().Select()
.Column(new AVG(new Column("p2.UnitPrice"))).From("Products","p2", new List<IHint>() { new NOLOCK() })
.Where(new Where(new Column("p2.CategoryID").Equale(new Column("p.CategoryID"))))
, "AverageCategoryUnitPrice")
.From("Products", "p", new List<IHint>() { new NOLOCK() })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Categories", "c", new List<IHint>() { new NOLOCK() })
.On(new Column("p.CategoryID").Equale(new Column("c.CategoryID")))
})
.Where(new Where(new Column("p.UnitPrice").GreaterThan(
new ColumnArithmatic().StartBracket(
new SqlQueryBuilder().Select()
.Column(new AVG(new Column("p3.UnitPrice")))
.From("Products","p3", new List<IHint>() { new NOLOCK() })
.Where(new Where(new Column("p3.CategoryID").Equale(new Column("p.CategoryID")))))
.MULTIPLY(1.5)
)))
.OrderBy(new OrderBy().SetColumnDescending("p.UnitPrice"))
.Build();
Query build by SqlQueryBuilder 3
SELECT p.ProductName,
p.UnitPrice,
c.CategoryName,
(SELECT AVG(p2.UnitPrice)
FROM Products AS p2 WITH (NOLOCK)
WHERE p2.CategoryID = p.CategoryID) AS AverageCategoryUnitPrice
FROM Products AS p WITH (NOLOCK)
INNER JOIN
Categories AS c WITH (NOLOCK)
ON p.CategoryID = c.CategoryID
WHERE p.UnitPrice > (SELECT AVG(p3.UnitPrice)
FROM Products AS p3 WITH (NOLOCK)
WHERE p3.CategoryID = p.CategoryID) * @pMAIN_2512060921007226550
ORDER BY p.UnitPrice DESC;
Parameters (If used)
| Name |
Value |
| @pMAIN_2512060921007226550 |
1.5 |
Query Results 3:
| |
ProductName |
UnitPrice |
CategoryName |
AverageCategoryUnitPrice |
| 1 |
Côte de Blaye
|
263.5000
|
Beverages
|
37.9791
|
| 2 |
Thüringer Rostbratwurst
|
123.7900
|
Meat/Poultry
|
54.0066
|
| 3 |
Mishi Kobe Niku
|
97.0000
|
Meat/Poultry
|
54.0066
|
| 4 |
Sir Rodney's Marmalade
|
81.0000
|
Confections
|
25.1600
|
| 5 |
Carnarvon Tigers
|
62.5000
|
Seafood
|
20.6825
|
| 6 |
Raclette Courdavault
|
55.0000
|
Dairy Products
|
28.7300
|
| 7 |
Manjimup Dried Apples
|
53.0000
|
Produce
|
32.3700
|
| 8 |
Tarte au sucre
|
49.3000
|
Confections
|
25.1600
|
| 9 |
Schoggi Schokolade
|
43.9000
|
Confections
|
25.1600
|
| 10 |
Vegie-spread
|
43.9000
|
Condiments
|
23.0625
|
| 11 |
Northwoods Cranberry Sauce
|
40.0000
|
Condiments
|
23.0625
|
| 12 |
Gnocchi di nonna Alice
|
38.0000
|
Grains/Cereals
|
20.2500
|
| 13 |
Wimmers gute Semmelknödel
|
33.2500
|
Grains/Cereals
|
20.2500
|
4. Using multi table hints (to be used under transaction)
SQL Server Query 4
BEGIN TRANSACTION;
SELECT
OD.OrderID,
OD.ProductID,
OD.Quantity,
OD.UnitPrice,
OD.Discount
FROM [Order Details] AS OD WITH (NOLOCK, INDEX(PK_Order_Details)) -- Read uncommitted, force clustered index scan
INNER JOIN Orders O ON O.OrderID = OD.OrderID
WHERE O.OrderDate >= DATEADD(day, -10000, GETDATE()); -- Assuming OrderDate is in [Order Details] or join to Orders for it
ROLLBACK TRANSACTION
Create SQL query with SqlQueryBuilder 4
var (sql4, parameters4) = new SqlQueryBuilder()
.Select()
.Columns("OD.OrderID","OD.ProductID","OD.Quantity","OD.UnitPrice","OD.Discount")
.From("[Order Details]","OD", new List<IHint>() { new NOLOCK(), new INDEX().SetValues("PK_Order_Details") })
.Join(new List<IJoin>()
{
new INNERJOIN().TableName("Orders","O")
.On(new Column("O.OrderID").Equale(new Column("OD.OrderID")))
})
.Where(new Where(new Column("O.OrderDate").GreaterThanOrEqualeTo(new DATEADD(SqlDateInterval.day, -10000, new GETDATE()))))
.Build();
Query build by SqlQueryBuilder 4
SELECT OD.OrderID,
OD.ProductID,
OD.Quantity,
OD.UnitPrice,
OD.Discount
FROM [Order Details] AS OD WITH (NOLOCK, INDEX (PK_Order_Details))
INNER JOIN
Orders AS O
ON O.OrderID = OD.OrderID
WHERE O.OrderDate >= DATEADD(day, @pMAIN_2512060921007378670, GETDATE());
Parameters (If used)
| Name |
Value |
| @pMAIN_2512060921007378670 |
-10000 |
Query Results 4:
| |
OrderID |
ProductID |
Quantity |
UnitPrice |
Discount |