I want optimize this select:
Select Dane1, Dane5, Dane6, Dane7 FROM Test
INNER JOIN Test2 ON Test.Id=Test2.IdTest
WHERE Dane5 > 199850
My database has 2 tables test, test2:
test design: Id int ->PRIMARY KEY, Dane1 int, Dane2 int, Dane3 int, Dane4 int, Dane5 int,
test2 design: Id int ->PRIMARY KEY, Dane6 int, Dane7 int, IdTest int,
Default index: PK__test__7C8480AE(Clustered), PK__test2__7E6CC920(Clustered)
The question is: Which indexes to attach or remove?
答案 0 :(得分:4)
It's always a good idea to define foreign-key relationships. On that way you keep data integrity and you can specify what happens if a parent record is deleted(f.e. delete children recursively).
A foreign-key is also a good candidate for an index to lookup the child-records quickly.
答案 1 :(得分:3)
创建索引时需要考虑的一些事项,例如在这种情况下:
您可以尝试这样的事情:
Create index test_xxx on test (Dane5) include (Dane1)
包含Dane1的天气取决于有多少行以及密钥查找是否导致问题
ID已包含在内,因为它是聚集索引
Create index test2_yyy on test2 (IdTest) include (Dane6, Dane7)
将Dane6和Date7作为包含列的天气也取决于需要对表进行的键查找总量以获取它们
您应该打开统计信息io以查看导致最合理读取的原因,以及天气是否需要索引中包含的列。
答案 2 :(得分:1)
As Tim pointed out, foreign keys and an index defined on the foreign key is a good call.
One additional index that could get you some additional speed - assuming your where
clause is always going to be on Dane5
- is to add a nonclustered index on Dane5