views:

50

answers:

1

My website uses linq-to-sql to load about 50k rows of data from a database. This data is static and never changes. It works similar to a spam filter and all 50k rows of patterns need to be loaded.

What's the best way to program this? for optimal performance?

+1  A: 

Loading the entire thing into a single static readonly data-structure (it being immutable means once constructed it can be safely used from many threads) would give the greatest overall performance per lookup.

However, it would lead to a long start-up time, which may not be acceptable. In that case you may consider loading each item as accessed, but that brings concurrency issues (since you are mutating a data-structure used by multiple threads).

In between is the option of loading all indices on start-up, and then adding the rest of the information on a per-access basis, with finer-grained locks to reduce lock contention.

Or you can ignore the lot and just load from the database as needed. This does have some advantages in terms of performance just because memory is not used for rarely-used information. It'll be a whole lot easier if you ever suddenly find that you do have to allow the data to change.

No one comes out as the only reasonable way to go in general, it'll depend on specifics of application, data, and usage patterns.

Jon Hanna
The single static readonly structure (singleton) won't scale well however, other than that it's perfect IMO
PostMan
Can you provide an example of how to create this static readonly variable? Normally I just use List<DataRow> ...
Rana
Depends on the way you'll want to find items and the way you'll want to use them. I would start thinking in terms of Dictionary<KeyType, CustomObject> or Dictionary<KeyType, List<CustomObject>> where KeyType is a way to look up relevant items (int and string being common) and CustomObject's are built from the data and contain the relevant information in a way that is easy to use when the data is needed.
Jon Hanna