Skip to content

Commit d6cca98

Browse files
authored
Merge pull request #2 from atrakic/feature/add-indexes
Add Dim table indexes
2 parents 385cab7 + 99bd7e3 commit d6cca98

File tree

9 files changed

+60
-30
lines changed

9 files changed

+60
-30
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
]
1010
}
1111
}
12-
}
12+
}

DbProject/dbo/Tables/DimCustomer.sql

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
CREATE TABLE [dbo].[DimCustomer] (
2-
[CustomerKey] INT NOT NULL PRIMARY KEY,
3-
[FirstName] VARCHAR (50) NOT NULL,
4-
[LastName] VARCHAR (50) NULL,
5-
[AddressLine1] VARCHAR (200) NULL,
6-
[City] VARCHAR (50) NULL,
7-
[PostalCode] VARCHAR (20) NULL
1+
CREATE TABLE [dbo].[DimCustomer]
2+
(
3+
[CustomerKey] INT NOT NULL PRIMARY KEY,
4+
[FirstName] VARCHAR (50) NOT NULL,
5+
[LastName] VARCHAR (50) NOT NULL,
6+
[AddressLine1] VARCHAR (200) NOT NULL,
7+
[City] VARCHAR (50) NOT NULL,
8+
[PostalCode] VARCHAR (20) NOT NULL
89
);
10+
GO
911

12+
CREATE INDEX IX_DimCustomer_Name ON [dbo].[DimCustomer] ([FirstName], [LastName]);
13+
GO
14+
CREATE INDEX IX_DimCustomer_Address ON [dbo].[DimCustomer] ([AddressLine1], [City], [PostalCode]);
15+
GO
1016

17+
ALTER TABLE [dbo].[DimCustomer]
18+
ADD CONSTRAINT AK_DimCustomer UNIQUE ([FirstName], [LastName], [AddressLine1], [City], [PostalCode]);
1119
GO

DbProject/dbo/Tables/DimDate.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
CREATE TABLE [dbo].[DimDate] (
2-
[DateKey] INT NOT NULL PRIMARY KEY,
3-
[Date] DATE NOT NULL
1+
CREATE TABLE [dbo].[DimDate]
2+
(
3+
[DateKey] INT NOT NULL PRIMARY KEY,
4+
[Date] DATE NOT NULL
45
);
6+
GO
57

6-
8+
CREATE INDEX IX_DimDate_DateKey ON [dbo].[DimDate] ([DateKey]);
79
GO

DbProject/dbo/Tables/DimProduct.sql

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
CREATE TABLE [dbo].[DimProduct] (
2-
[ProductKey] INT NOT NULL PRIMARY KEY,
3-
[ProductName] VARCHAR (50) NOT NULL,
4-
[Category] VARCHAR (50) NULL,
5-
[ListPrice] DECIMAL (18) NULL
1+
CREATE TABLE [dbo].[DimProduct]
2+
(
3+
[ProductKey] INT NOT NULL PRIMARY KEY,
4+
[ProductName] VARCHAR (50) NOT NULL,
5+
[Category] VARCHAR (50) NOT NULL,
6+
[ListPrice] DECIMAL (18, 2) NOT NULL
67
);
8+
GO
9+
10+
CREATE INDEX IX_DimProduct_Name ON [dbo].[DimProduct] ([ProductName]);
11+
GO
712

13+
CREATE INDEX IX_DimProduct_Category ON [dbo].[DimProduct] ([Category]);
14+
GO
815

16+
ALTER TABLE [dbo].[DimProduct]
17+
ADD CONSTRAINT AK_DimProduct UNIQUE ([ProductName], [Category]);
918
GO
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
CREATE TABLE [dbo].[FactSalesOrder]
22
(
3-
[SalesOrderKey] INT NOT NULL,
3+
[SalesOrderKey] INT NOT NULL PRIMARY KEY,
44
[SalesOrderDateKey] INT NOT NULL,
55
[ProductKey] INT NOT NULL,
66
[CustomerKey] INT NOT NULL,
7-
[Quantity] INT NULL,
8-
[SalesTotal] DECIMAL (18) NULL
7+
[Quantity] INT NOT NULL DEFAULT 0,
8+
[SalesTotal] DECIMAL (18, 2) NOT NULL DEFAULT 0.00,
99

10-
CONSTRAINT [PK_FactSalesOrder] PRIMARY KEY CLUSTERED ([SalesOrderKey] ASC),
1110
CONSTRAINT [FK_FactSalesOrder_DimDate] FOREIGN KEY ([SalesOrderDateKey]) REFERENCES [dbo].[DimDate] ([DateKey]),
1211
CONSTRAINT [FK_FactSalesOrder_DimProduct] FOREIGN KEY ([ProductKey]) REFERENCES [dbo].[DimProduct] ([ProductKey]),
1312
CONSTRAINT [FK_FactSalesOrder_DimCustomer] FOREIGN KEY ([CustomerKey]) REFERENCES [dbo].[DimCustomer] ([CustomerKey])
1413
);
14+
GO
1515

16+
CREATE INDEX IX_FactSalesOrder_DateKey ON [dbo].[FactSalesOrder] ([SalesOrderDateKey]);
17+
GO
18+
CREATE INDEX IX_FactSalesOrder_ProductKey ON [dbo].[FactSalesOrder] ([ProductKey]);
19+
GO
20+
CREATE INDEX IX_FactSalesOrder_CustomerKey ON [dbo].[FactSalesOrder] ([CustomerKey]);
1621
GO

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ build:
99
dotnet outdated --upgrade
1010
dotnet build
1111

12+
test:
13+
#dotnet test
14+
./tests/test.sh
15+
1216
clean:
1317
dotnet clean
1418
docker compose down --remove-orphans --volumes

SQL/dw-seed.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
MERGE INTO DimCustomer AS target
33
USING (VALUES
44
(1, 'John', 'Doe', '123 Main St', 'Springfield', '12345'),
5-
(2, 'Jane', 'Smith', '456 Elm St', 'Springfield', '12345'),
6-
(3, 'Bob', 'Jones', '789 Oak St', 'Springfield', '12345')
5+
(2, 'Jane', 'Doe', '456 Elm St', 'Springfield', '12345'),
6+
(3, 'Alice', 'Smith', '789 Oak St', 'Springfield', '12345')
77
) AS source (CustomerKey, FirstName, LastName, AddressLine1, City, PostalCode)
88
ON target.CustomerKey = source.CustomerKey
99
WHEN NOT MATCHED BY TARGET THEN

compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ services:
99
- .env
1010
environment:
1111
ConnectionString: "Server=tcp:db,1433;Initial Catalog=demo;UID=sa;Password=${MSSQL_SA_PASSWORD};TrustServerCertificate=true;Connection Timeout=3;"
12+
# keep the container alive for debugging
13+
DEBUG: "${DEBUG:-}"
1214
volumes:
1315
- $PWD/SQL/dw-seed.sql:/seed.sql
1416
entrypoint:
1517
- /bin/sh
1618
- -c
1719
- |
1820
./SqlClient.ConsoleApp /seed.sql
19-
# keep the container running (for debugging)
20-
#tail -f /dev/null
21+
if [ -n "$DEBUG" ];then tail -f /dev/null; fi
2122
depends_on:
2223
sqlpackage:
2324
condition: service_completed_successfully

tests/test.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
#!/bin/bash
12

2-
# select all tables and views from sql server:
3+
set -e
34

4-
QUERY="
5-
SELECT * FROM INFORMATION_SCHEMA.TABLES
6-
"
5+
DB="demo"
6+
QUERY="SELECT * FROM vSalesByCityAndCategory"
77

8-
docker exec -it --env-file .env db /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$MSSQL_SA_PASSWORD" -Q "$QUERY" -b
8+
docker exec -it --env-file .env db /opt/mssql-tools/bin/sqlcmd \
9+
-S localhost -U sa -P "$MSSQL_SA_PASSWORD" -d "$DB" -Q "$QUERY" -b

0 commit comments

Comments
 (0)