开发者

C# Data Collection v Database lookup - which is the more efficient?

I was wondering if I could pick the brains of the community...

In a project I am working on, there is a need to look up a value from a key-value list. This list will not change throughout the life of the software. Lets say as an example the list is thus:

ID       Name

1        Apple
2        Orange
3        Pear
4        Banana

...and so on.

I am considering two methods of implementing this. The first is to store the list 开发者_开发问答in some sort of (as yet undecided) C# data collection, and then look the required value up at runtime as required. The second method I am considering is storing the list within a database table (SQL Server 2008, since you asked). The application can then access the database at runtime via a stored procedure.

The lookup will occur twice in quick succession following a request made by the user from a web form.

My query is this: Which of these two methods would be the most efficient in terms of processing time?

I realise that there might not be a definitive answer to this question, but I would welcome any comments or thoughts.


What is expensive in term of performances is usually disk access and network access.

if you have the collection available in memory (RAM) in the web server or application server this would be faster than a query to the database.

if the data is not likely to change often or at all, you can go for in memory data structure, if it changes sometimes you can query from db and store it in the cache so following accesses to that object will not require database query until cache expires or is reset depending on your needs.


Use a Dictionary<>, it's orders of magnitude faster than a roundtrip to the database.


As you state -

This list will not change throughout the life of the software.

I'd hard code the list into the program. It's not worth the database overhead for a list that will never change.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜