Skip to content
Advertisement

One large DB request plus many Casts vs. many single DB requests – which is faster for a lot of Data [closed]

let’s say you have 5 concrete classes implementing the interface IResult. And you want to work with many diffrent concrete objects that are stored in a database. I am working with C# and a SQL database. I have 2 diffrent ways in Mind:

Approach1:
You perform one large database request where you get data in their concrete form that you can put in a List because saving it in 5 diffrent lists is not great in case you want to add mor concrete classes. At least in my opinion. If you now need a concrete Object you have to use one cast when ever you need the concrete instance wich results in many many casts at the end.

Approach2:
You perform a database request everytime you need one concrete object and after doing something with it you throw it away. This way you dont need to cast when ever you need a concrete instance but you have to perform many many database requests.

can anyone please tell me wich way is faster performance wise?

Thank you

Advertisement

Answer

Always, without exception, one large properly built query. Every time you hit the DB server you introduce your network latency twice in your operations (plus the query time itself), which adds up to ridiculous amounts of wait time.

I see the close votes and that guy commenting with the witty “did you profile it?”, but you can safely ignore them, the answer is absolutely clear cut: one query.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement