简体   繁体   English

什么是更好的方法性能明智的选择

[英]What is a better approach performance wise

Lets say I need to fetch some records from the database, and filter them based on an enumeration type property. 可以说我需要从数据库中获取一些记录,并根据枚举类型属性对其进行过滤。

  • fetch List<SomeType> 获取List<SomeType>
  • filter on SomeType.Size SomeType.SizeSomeType.Size
  • enumeration Size { Small, Medium, Large } 枚举Size { Small, Medium, Large }

when displaying records, there will be a predefined value for Size filter (ex Medium). 显示记录时,“大小”过滤器将有一个预定义的值(例如“中”)。 In most of the cases, user will select a value from filtered data by predefined value. 在大多数情况下,用户将通过预定义的值从过滤的数据中选择一个值。 There is a possibility that a user could also filter to Large, then filter to Medium, then filter to Large again. 用户可能还会过滤为大,然后过滤为中,然后再次过滤为大。

I have different situations with same scenario: 我在相同的情况下有不同的情况:

  • List contains less than 100 records and 3-5 properties 列表包含少于100条记录和3-5个属性
  • List contains 100-500 records and 3-5 properties 列表包含100-500条记录和3-5个属性
  • List contains max 2000 records with 3-5 properties 列表包含最多3-5条属性的2000条记录

What is my best approach here? 我最好的方法是什么? Should I have a tab that will contain grid for each enum, or should I have one common enum and always filter, or? 我应该有一个包含每个枚举的网格的选项卡,还是应该有一个通用枚举并始终进行过滤,或者?

I would do the filtering right on the database, if those fields are indexed I would suspect having the db filter it would be much faster than filtering with c-sharp after the fact. 我会在数据库上进行过滤,如果索引了这些字段,我怀疑拥有db过滤器比事实之后使用c-sharp过滤要快得多。

Of course you can always cache the filtered database result as to prevent multiple unnescessary database calls. 当然,您始终可以缓存过滤后的数据库结果,以防止发生多个不必要的数据库调用。

EDIT: as for storing the information in the database, suppose you had this field setup: 编辑:关于将信息存储在数据库中,假设您具有以下字段设置:

CREATE TABLE Tshirts
(
    id int not null identity(1,1),
    name nvarchar(255) not null,
    tshirtsizeid int not null,
    primary key(id)
)

CREATE TABLE TshirtSizes
(
    id int not null, -- not auto-increment
    name nvarchar(255)
)

INSERT INTO TshirtSizes(id, name) VALUES(1, 'Small')
INSERT INTO TshirtSizes(id, name) VALUES(2, 'Medium')
INSERT INTO TshirtSizes(id, name) VALUES(3, 'Large')

ALTER TABLE Tshirts ADD FOREIGN KEY(tshirtsizeid) REFERENCES tshirtsize(id)

then in your C# 然后在您的C#中

public enum TShirtSizes 
{
    Small = 1,
    Medium = 2,
    Large = 3
}

In this example, the table TshirtSizes is only used for the reader to know what the magic numbers 1, 2, and 3 mean. 在此示例中,表TshirtSizes仅用于读者了解魔术数字1、2和3的含义。 If you don't care about database read-ability you can omit those tables and just have an indexed column. 如果您不关心数据库的可读性,则可以省略这些表,而只创建一个索引列。

Memory is usually cheap. 内存通常很便宜。 Otherwise you could one-time sort all the values and retrieve based on comparison which would be O(n). 否则,您可以一次性对所有值进行排序,然后根据比较结果将其检索为O(n)。 You could keep track of the positions of things and retrieve faster that way. 您可以跟踪事物的位置并以这种方式更快地进行检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM