Skip to content

Commit 981bc41

Browse files
committed
Imporve indexes
Add condition index, covering indexes, sort columns, include columns.
1 parent 35bdd03 commit 981bc41

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

Scripts/Build/INDEXES.sql

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,53 @@ EXEC sp_executesql @DropAllIndexesQuery
2121

2222
---- Loans ----
2323

24-
CREATE INDEX idx_Loans_BookID -- Foreign Key
25-
ON Circulation.Loans(BookID);
24+
CREATE NONCLUSTERED INDEX idx_Loans_BookID_DateBorrowed -- Foreign Key, Sort Column
25+
ON Circulation.Loans(BookID, DateBorrowed);
2626

27-
CREATE INDEX idx_Loans_BorrowerID -- Foreign Key
28-
ON Circulation.Loans(BorrowerID);
27+
CREATE NONCLUSTERED INDEX idx_Loans_BorrowerID_DateBorrowed -- Foreign Key, Sort Column
28+
ON Circulation.Loans(BorrowerID, DateBorrowed);
2929

30-
CREATE INDEX idx_Loans_DateBorrowed -- Queried Date
31-
ON Circulation.Loans(DateBorrowed);
30+
CREATE NONCLUSTERED INDEX idx_Loans_ActiveLoans -- Common Condition
31+
ON Circulation.Loans (BorrowerID, DateReturned) -- Sort Column
32+
INCLUDE (BookID) -- Typical selection
33+
WHERE DateReturned IS NULL; -- Loan is active
3234

33-
CREATE INDEX idx_Loans_DueDate -- Queried Date
34-
ON Circulation.Loans(DueDate);
35+
CREATE NONCLUSTERED INDEX idx_Loans_DateBorrowed -- Queried Date
36+
ON Circulation.Loans(DateBorrowed);
3537

36-
CREATE INDEX idx_Loans_DateReturned -- Queried Date
37-
ON Circulation.Loans(DateReturned);
38+
CREATE NONCLUSTERED INDEX idx_Loans_DueDate_DateReturned -- Queried Date, Typical Selection
39+
ON Circulation.Loans(DueDate, DateReturned)
40+
INCLUDE (BookID, BorrowerID, DateBorrowed);
3841

3942

4043

4144
---- BookGenres ----
4245

43-
CREATE INDEX idx_BookGenres_BookID -- Foreign Key
46+
CREATE NONCLUSTERED INDEX idx_BookGenres_BookID -- Foreign Key
4447
ON Catalog.BookGenres(BookID);
4548

46-
CREATE INDEX idx_BookGenres_GenreID -- Foreign Key
49+
CREATE NONCLUSTERED INDEX idx_BookGenres_GenreID -- Foreign Key
4750
ON Catalog.BookGenres(GenreID);
4851

4952

5053

5154
---- Borrowers ----
5255

53-
CREATE INDEX idx_Borrowers_DateOfBirth -- Queried Date
56+
CREATE NONCLUSTERED INDEX idx_Borrowers_DateOfBirth -- Queried Date
5457
ON UserManagement.Borrowers(DateOfBirth);
5558

59+
CREATE NONCLUSTERED INDEX idx_Borrowers_Covering -- Covering Index
60+
ON UserManagement.Borrowers(BorrowerID)
61+
INCLUDE (FirstName, LastName, Email); -- Typical selections
62+
63+
64+
65+
------ Books ------
66+
67+
CREATE NONCLUSTERED INDEX idx_Books_Covering -- Covering Index
68+
ON Catalog.Books(BookID, Author) -- Typical sorts
69+
INCLUDE (Title); -- Typical selections
70+
5671

5772

5873
---- AuditLog -----

0 commit comments

Comments
 (0)