用户高级筛选所需的索引?

huangapple go评论51阅读模式
英文:

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 ???;

huangapple
  • 本文由 发表于 2023年3月9日 15:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定