Skip to content

Commit e3e3083

Browse files
committed
test main.py
1 parent 200c35b commit e3e3083

File tree

1 file changed

+178
-6
lines changed

1 file changed

+178
-6
lines changed

main.py

Lines changed: 178 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,192 @@
22
from mssql_python import setup_logging
33
import os
44
import decimal
5+
import time
6+
import pyodbc
7+
from typing import ClassVar
8+
import string
59

6-
setup_logging('stdout')
10+
# setup_logging('stdout')
711

8-
conn_str = os.getenv("DB_CONNECTION_STRING")
9-
conn = connect(conn_str)
12+
# conn_str = os.getenv("DB_CONNECTION_STRING")
13+
# conn_str = "Server=Saumya;DATABASE=master;UID=sa;PWD=HappyPass1234;Trust_Connection=yes;TrustServerCertificate=yes;"
14+
conn_str= "DRIVER={ODBC Driver 18 for SQL Server};Server=tcp:sqlsumitsardb.database.windows.net,1433;Database=AdventureWorks2022;Uid=sqladmin;Pwd=SoftMicro$123;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
15+
16+
17+
18+
# conn.autocommit = True
19+
COMPLEX_JOIN_AGGREGATION = """
20+
SELECT
21+
p.ProductID,
22+
p.Name AS ProductName,
23+
pc.Name AS Category,
24+
psc.Name AS Subcategory,
25+
COUNT(sod.SalesOrderDetailID) AS TotalOrders,
26+
SUM(sod.OrderQty) AS TotalQuantity,
27+
SUM(sod.LineTotal) AS TotalRevenue,
28+
AVG(sod.UnitPrice) AS AvgPrice
29+
FROM Sales.SalesOrderDetail sod
30+
INNER JOIN Production.Product p ON sod.ProductID = p.ProductID
31+
INNER JOIN Production.ProductSubcategory psc ON p.ProductSubcategoryID = psc.ProductSubcategoryID
32+
INNER JOIN Production.ProductCategory pc ON psc.ProductCategoryID = pc.ProductCategoryID
33+
GROUP BY p.ProductID, p.Name, pc.Name, psc.Name
34+
HAVING SUM(sod.LineTotal) > 10000
35+
ORDER BY TotalRevenue DESC;
36+
"""
37+
38+
#query 2
39+
LARGE_DATASET = """
40+
SELECT
41+
soh.SalesOrderID,
42+
soh.OrderDate,
43+
soh.DueDate,
44+
soh.ShipDate,
45+
soh.Status,
46+
soh.SubTotal,
47+
soh.TaxAmt,
48+
soh.Freight,
49+
soh.TotalDue,
50+
c.CustomerID,
51+
p.FirstName,
52+
p.LastName,
53+
a.AddressLine1,
54+
a.City,
55+
sp.Name AS StateProvince,
56+
cr.Name AS Country
57+
FROM Sales.SalesOrderHeader soh
58+
INNER JOIN Sales.Customer c ON soh.CustomerID = c.CustomerID
59+
INNER JOIN Person.Person p ON c.PersonID = p.BusinessEntityID
60+
INNER JOIN Person.BusinessEntityAddress bea ON p.BusinessEntityID = bea.BusinessEntityID
61+
INNER JOIN Person.Address a ON bea.AddressID = a.AddressID
62+
INNER JOIN Person.StateProvince sp ON a.StateProvinceID = sp.StateProvinceID
63+
INNER JOIN Person.CountryRegion cr ON sp.CountryRegionCode = cr.CountryRegionCode
64+
WHERE soh.OrderDate >= '2013-01-01';
65+
"""
66+
# Query 3: Very Large Dataset with CROSS JOIN
67+
VERY_LARGE_DATASET = """
68+
SELECT
69+
sod.SalesOrderID,
70+
sod.SalesOrderDetailID,
71+
sod.ProductID,
72+
sod.OrderQty,
73+
sod.UnitPrice,
74+
sod.LineTotal,
75+
p.Name AS ProductName,
76+
p.ProductNumber,
77+
p.Color,
78+
p.ListPrice,
79+
n1.number AS RowMultiplier1
80+
FROM Sales.SalesOrderDetail sod
81+
CROSS JOIN (SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS number
82+
FROM Sales.SalesOrderDetail) n1
83+
INNER JOIN Production.Product p ON sod.ProductID = p.ProductID;
84+
"""
85+
# Query 4: CTE
86+
SUBQUERY_WITH_CTE = """
87+
WITH SalesSummary AS (
88+
SELECT
89+
soh.SalesPersonID,
90+
YEAR(soh.OrderDate) AS OrderYear,
91+
SUM(soh.TotalDue) AS YearlyTotal
92+
FROM Sales.SalesOrderHeader soh
93+
WHERE soh.SalesPersonID IS NOT NULL
94+
GROUP BY soh.SalesPersonID, YEAR(soh.OrderDate)
95+
),
96+
RankedSales AS (
97+
SELECT
98+
SalesPersonID,
99+
OrderYear,
100+
YearlyTotal,
101+
RANK() OVER (PARTITION BY OrderYear ORDER BY YearlyTotal DESC) AS SalesRank
102+
FROM SalesSummary
103+
)
104+
SELECT
105+
rs.SalesPersonID,
106+
p.FirstName,
107+
p.LastName,
108+
rs.OrderYear,
109+
rs.YearlyTotal,
110+
rs.SalesRank
111+
FROM RankedSales rs
112+
INNER JOIN Person.Person p ON rs.SalesPersonID = p.BusinessEntityID
113+
WHERE rs.SalesRank <= 10
114+
ORDER BY rs.OrderYear DESC, rs.SalesRank;
115+
"""
116+
117+
118+
print("Using pyodbc now")
119+
start_time = time.perf_counter()
120+
conn = pyodbc.connect(conn_str)
10121

11122
# conn.autocommit = True
12123

13124
cursor = conn.cursor()
14-
cursor.execute("SELECT database_id, name from sys.databases;")
125+
cursor.execute(COMPLEX_JOIN_AGGREGATION)
126+
127+
rows = cursor.fetchall()
128+
129+
# for row in rows:
130+
# print(f"Database ID: {row[0]}, Name: {row[1]}")
131+
132+
end_time = time.perf_counter()
133+
elapsed_time = end_time - start_time
134+
print(f"Elapsed time in pyodbc for query 1: {elapsed_time:.4f} seconds")
135+
136+
start_time = time.perf_counter()
137+
cursor.execute(LARGE_DATASET)
138+
rows = cursor.fetchall()
139+
end_time = time.perf_counter()
140+
elapsed_time = end_time - start_time
141+
print(f"Elapsed time in pyodbc for query 2: {elapsed_time:.4f} seconds")
142+
143+
start_time = time.perf_counter()
144+
cursor.execute(VERY_LARGE_DATASET)
15145
rows = cursor.fetchall()
146+
end_time = time.perf_counter()
147+
elapsed_time = end_time - start_time
148+
print(f"Elapsed time in pyodbc for query 3: {elapsed_time:.4f} seconds")
16149

17-
for row in rows:
18-
print(f"Database ID: {row[0]}, Name: {row[1]}")
150+
start_time = time.perf_counter()
151+
cursor.execute(SUBQUERY_WITH_CTE)
152+
rows = cursor.fetchall()
153+
end_time = time.perf_counter()
154+
elapsed_time = end_time - start_time
155+
print(f"Elapsed time in pyodbc for query 4: {elapsed_time:.4f} seconds")
156+
157+
cursor.close()
158+
conn.close()
159+
160+
print("Using mssql-python now")
161+
start_time = time.perf_counter()
162+
conn = connect(conn_str)
163+
cursor = conn.cursor()
164+
cursor.execute(COMPLEX_JOIN_AGGREGATION)
165+
rows = cursor.fetchall()
166+
167+
end_time = time.perf_counter()
168+
elapsed_time = end_time - start_time
169+
print(f"Elapsed time in mssql-python for query 1: {elapsed_time:.4f} seconds")
170+
171+
start_time = time.perf_counter()
172+
cursor.execute(LARGE_DATASET)
173+
rows = cursor.fetchall()
174+
end_time = time.perf_counter()
175+
elapsed_time = end_time - start_time
176+
print(f"Elapsed time in mssql-python for query 2: {elapsed_time:.4f} seconds")
177+
178+
start_time = time.perf_counter()
179+
cursor.execute(VERY_LARGE_DATASET)
180+
rows = cursor.fetchall()
181+
end_time = time.perf_counter()
182+
elapsed_time = end_time - start_time
183+
print(f"Elapsed time in mssql-python for query 3: {elapsed_time:.4f} seconds")
184+
185+
start_time = time.perf_counter()
186+
cursor.execute(SUBQUERY_WITH_CTE)
187+
rows = cursor.fetchall()
188+
end_time = time.perf_counter()
189+
elapsed_time = end_time - start_time
190+
print(f"Elapsed time in mssql-python for query 4: {elapsed_time:.4f} seconds")
19191

20192
cursor.close()
21193
conn.close()

0 commit comments

Comments
 (0)