First off, I’m using ASP.NET MVC with Entity Framework Code First 5.0 using SQL Server. I have an application that requires me to save the status (Working/Off) for every half hour of the day every day of the week. If you do the math that’s 336 rows of nothing but (id, day, time, status, user_id) for every user (potentially thousands of users).
I was wondering which one these scenarios would be more optimum in terms of storage size and the performance of the application (in your opinion).
- Store these as 1 row per status (
id, day, time, status, user_id) so 336 rows for every user. - Save it all in a comma delimited field in the database (one very large row) and parse them out using C#.
- Is there a better way that I haven’t looked into or haven’t considered?
I’m currently leaning towards option 1.
These are separate pieces of data so you should absolutely not munge them together as comma-separated values. This makes searching, reporting and other things a major pain.
Table-valued parameters are the most efficient way to do pass multiple rows to SQL Server. However, I do not know if EF is going to paint you into a corner – I hear that there are many normal things you can do in SQL Server that EF/CF still can’t do.
How it works, you create a table type (I have to guess on data types here):
This should match the structure of your table where you plan to store this information (and again, no, you shouldn’t store separate facts in the same column).
Now you can add this as a parameter to your stored procedure:
Now you can just pass a
DataTableor other collection of values from C# into a single stored procedure parameter (useSqlDbType.Structured).