Saving DataTable to session vs saving a Custom object.

J

John Kandell

Hi,

I posted this in the asp.net group, but didn't get a response. Maybe someone
here can help me with this...

---

Would someone be able to shed some light on what is the cost of saving a
DataTable to session vs saving a custom object of the same data.

For example, let's say I had a DataTable with 1000 records and each record
had 10 columns, how much extra cost is involved in saving that vs, a custom
object with 10 get/set properties in a ArrayList holding 1000 instances of
the object with the same data?


Looking forward to your response.
 
B

Bruce Johnson

Short answer: It depends.

Longer answer: It depends on what type of session storage you're using.
If you're using in process sessuion, then the difference between the two
is simply the size of the object. If your using a state server or a SQL
server to maintain session information, then you need to look at how the
objects get serialized.

Just so you know, the DataTable will serialize as a diffgram, so the
size used will depend on whether the rows are freshly added (only the
new row is included) or modified (both the new and old rows are
included). ArrayLists, on the other hand serialize as little more than
the aggregated serialization of the objects that are contained.

Hope that helps.

Bruce Johnson [.NET MVP]
http://www.objectsharp.com/blogs/bruce
 
J

John Wood

I don't think the session you refer to will actually result in any
serialization occurring, so the difference will just be in terms of the
memory usage. The biggest overhead you'd see in memory usage would be the
fact that you could have typed classes containing your data in an ArrayList
and you avoid the boxing overhead. DataRows, while quite optimized for
storing data, do end up boxing the data before it is stored so end up with
an extra object for each column in each row that otherwise wouldn't need to
be allocated.
 
J

John Kandell

Thanks for you reply...,

I guess what I'm really trying to figure out is what is the best pratice...,
here's the exact scenario.

The web app we are developing is a portal to a large database.
There will be approximately 1000-1500 hits per day.

The user executes a query which can return at most 1000 records (rowcount is
set to 1000). The results are displayed on a grid with pagination enabled to
100 records per page. Therefore, if the query returns 1000 records, the page
will show the grid with 100 records and links to the other 10 pages. (sorta
like your typical search engine.)

The paging is controlled by the webgrid control, not the stored proc used to
get the data. (ie the stored proc returns the complete result set.)

We are using SQL Server to store our session variables, and the app is
running on a webfarm with 4 servers.

*The requirements are to have the HTML as small as possible, and the limit
the roundtrips to the database.* Therefore, we opted to storing the results
in a session variable, so, when the user clicks on the "page 2" to see more
data, the data is read from the session, instead of being read again from
the database.

So, the question is then, is it better to use a DataTable to keep the data
in session, or use the datareader to populate an array of a custom object?
What are the cost and benefits of either approach? Is there a better
solution?


Looking forward to your response.
 
B

Bruce Johnson

First off, to answer Mr. Wood, there *are* instances where an object
needs to be serialized. Specifically, that is when a state server or a
SQL Server is being used to maintain state. And that is the situation
described here.

Now on to the particulars of this situation. My first thought is
question whether storing the query results in a session are going to
result in that much of a performance gain. After all, you need to hit
SQL Server in order to retrieve the previously saved session
information. Is that really going to be that much better than rerunning
the original query? That is probably something I'd want to benchmark.

If we assume that storing the results in session is a good thing, then I
suspect that the ArrayList of custom objects will be the better choice.
My rationale is because the DataTable, like I said, serializes as a
DiffGram-style XML document. Which means that to rehydrate the
DataTable means that, basically, the ReadXml method is called using the
DiffGram type. And XML documents are relatively slow to operate with.

That having been said, it would probably be best (and relatively easy)
to benchmark the two techniques just to be sure

Bruce Johnson [.NET MVP]
http://www.objectsharp.com/blogs/bruce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top