我有一个名为“还款”的表,其中以borrower_id为主键。我需要决定需要在此表上创建的索引。我有以下查询,我已经提出了索引。我需要知道这些索引是否正确或是否有更好的方法。
SELECT DISTINCT name, address
FROM Repayment
WHERE borrower_id = 4;
create index idxrep on repayment(borrower_id) include(name,address);
SELECT *
FROM Repayment
WHERE borrower_id = ? AND repayment_date > '11-12-2014';
create idxborrrep on repayment(borrower_id,repayment_date);
SELECT borrower_id, loanamount
FROM Repayment
WHERE loanamount BETWEEN ? AND ?;
create indxloanamount on repayment(loanamount) include(borrower_id,loanamount);
答案 0 :(得分:2)
您的索引看起来很适合您的查询。您可以将三个索引减少到两个主要覆盖索引:
create idxborrrep on repayment(borrower_id, repayment_date) include (name, address);
create indxloanamount on repayment(loanamount) include(borrower_id,loanamount);
我会质疑索引中是否包含name
和address
列。这些可能相当大,因此在数据页中查找它们的开销可能小于索引上的大小增加。然而,无论哪种方式,这很可能是一个非常小的问题。