英文:
Indexes needed for user advanced filter?
问题
ASP.NET
MS SQL表: tblPets
ID (123456789)
AnimalType (Dog, Cat, Goldfish...)
AnimalName (Rudolf, Ben, Harold...)
CountryCode (US, AU..)
StateCode (CA, NY...)
CityCode (AK, LA...)
IsMammal (True, False)
IsFish (True, False)
HasFur (True, False)
Color (Black, Brown, Orange...)
WeightKG (34, 57, 18...)
我们考虑为用户创建一个"高级筛选"网页,以便用户可以搜索/筛选满足其用户输入条件的所有记录。
我们考虑使用一个包含10个文本输入字段的筛选弹出对话框,允许用户根据一个或多个条件进行筛选。
例如:
CountryCode: US
StateCode: CA
IsFish: True
Color: Orange
假设我们希望表tblPets尽可能索引化以获得最快的结果,索引会是什么?例如,是否需要有100个索引(10 x 10)?
假设第一列命名为c1,第二列为c2等:
索引1:
c1,c2,c3,c4,c5,c6,c7,c8,c9,c10
索引2:
c2,c3,c4,c5,c6,c7,c8,c9,c10
索引3:
c3,c4,c5,c6,c7,c8,c9,c10
索引4:
c4,c5,c6,c7,c8,c9,c10
索引5:
c5,c6,c7,c8,c9,c10
索引6:
c6,c7,c8,c9,c10
索引7:
c7,c8,c9,c10
索引8:
c8,c9,c10
索引9:
c9,c10
索引10:
c10
索引1B:
c1,c3,c4,c5,c6,c7,c8,c9,c10
索引2B:
c1,c4,c5,c6,c7,c8,c9,c10
索引3B:
c1,c5,c6,c7,c8,c9,c10
索引4B:
c1,c6,c7,c8,c9,c10
索引5B:
c1,c7,c8,c9,c10
索引6B:
c1,c8,c9,c10
索引7B:
c1,c9,c10
索引8B:
c1,c10
索引1C:
c2,c4,c5,c6,c7,c8,c9,c10
等
英文:
ASP.NET
MS SQL table: tblPets
ID (123456789)
AnimalType (Dog, Cat, Goldfish...)
AnimalName (Rudolf, Ben, Harold...)
CountryCode (US, AU..)
StateCode (CA, NY...)
CityCode (AK, LA...)
IsMammal (True, False)
IsFish (True, False)
HasFur (True, False)
Color (Black, Brown, Orange...)
WeightKG (34, 57, 18...)
We are thinking of creating an "Advanced Filter" webpage for users to search/filter for all records that match their user-inputted criteria.
We are considering having a filter popup dialog with 10 text input fields allowing the user to filter on one or more criteria.
E.g.:
CountryCode: US
StateCode: CA
IsFish: True
Color: Orange
Assuming we want the table tblPets as indexed as possible for fastest results, what would the indexes be? E.g., would there need to be 100 indexes (10 x 10)?
Assuming column one is named c1, column two c2 etc:
Index1:
c1,c2,c3,c4,c5,c6,c7,c8,c9,c10
Index2:
c2,c3,c4,c5,c6,c7,c8,c9,c10
Index3:
c3,c4,c5,c6,c7,c8,c9,c10
Index4:
c4,c5,c6,c7,c8,c9,c10
Index5:
c5,c6,c7,c8,c9,c10
Index6:
c6,c7,c8,c9,c10
Index7:
c7,c8,c9,c10
Index8:
c8,c9,c10
Index9:
c9,c10
Index10:
c10
Index1B:
c1,c3,c4,c5,c6,c7,c8,c9,c10
Index2B:
c1,c4,c5,c6,c7,c8,c9,c10
Index3B:
c1,c5,c6,c7,c8,c9,c10
Index4B:
c1,c6,c7,c8,c9,c10
Index5B:
c1,c7,c8,c9,c10
Index6B:
c1,c8,c9,c10
Index7B:
c1,c9,c10
Index8B:
c1,c10
Index1C:
c2,c4,c5,c6,c7,c8,c9,c10
Etc
答案1
得分: 1
是的,您应该在将用于where
子句的每个列上放置索引。请注意,索引会导致写入/更新性能变慢。索引还会增加表的大小。
英文:
Yes, you should put indexes
on each column that will be used in the where
clause. Pay attention that the indexes means slower write/update performance. The indexes will also increase your table size.
答案2
得分: 0
在这种情况下,最佳方法是在表上创建一个聚集的列存储索引。
如果表已经有一个聚集索引(通常是主键),请删除主键约束并创建一个具有相同列的非聚集索引:
ALTER TABLE ??? DROP CONSTRAINT PK_???;
ALTER TABLE ??? ADD PRIMARY KEY (???) NONCLUSTERED;
然后创建聚集的列存储索引:
CREATE CLUSTERED COLUMNSTORE INDEX ON ???;
英文:
In this case the best way is to create a CLUSTERED COLUMNSTORE index on the table.
If the table has already a CLUTERED index (usually PRIMARY KEY) dropt the PK constraint and create a new PK with the same columns in a NONCLUSTERED inhdex :
ALTER TABLE ??? DROP CONSTRAINT PK_???;
ALTER TABLE ??? ADD PRIMARY KEY (???) NONCLUSTERED;
Then create the CLUSTERED COLUMNSTORE index :
CREATE CLUSTERED COLUMNSTORE INDEX ON ???;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论