Why is DataTable.Clear() so slow?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

I fill a DataTable with search results, which take a very long time if
I first clear it. The values come from an object and I map them into
the corresponding table columns. I may have 65000 results. I iterate
through them, create a new row, fill it, and add it to the DataTable
for each interation. This works fine on the first search.

Before each search, I call DataTable.Clear() and do the above process.
Every search after the first one is extremely slow to fill the
DataTable. However, if I comment out the Clear() line, performance
doesn't suffer. Is there another way I can clean this table for each
search?

Why is .Clear() such a performance hit?

Thanks,
Brett
 
Hello Brett,

Why not just create new DataTable instead of creating existed?

BR> I fill a DataTable with search results, which take a very long time
BR> if I first clear it. The values come from an object and I map them
BR> into the corresponding table columns. I may have 65000 results. I
BR> iterate through them, create a new row, fill it, and add it to the
BR> DataTable for each interation. This works fine on the first
BR> search.
BR>
BR> Before each search, I call DataTable.Clear() and do the above
BR> process. Every search after the first one is extremely slow to fill
BR> the DataTable. However, if I comment out the Clear() line,
BR> performance doesn't suffer. Is there another way I can clean this
BR> table for each search?
BR>
BR> Why is .Clear() such a performance hit?
BR>
BR> Thanks,
BR> Brett
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi Michael,
Why not just create new DataTable instead of creating existed?

What exactly is Clear() doing?

Why is recreating the table better than just clearing it? There's a
big difference there and I'm not sure why performance suffers so
greatly after Clear()ing.

Thanks,
Brett
 
Hello Brett,

Use Reflector to look at DataTable.Clear code.
It performs a lot of job - check parent constrains, remove rows, clear indexes.

For the big tables you can end up with creating new dataTable is more quick
way than clearing existed

BR> Hi Michael,
BR>BR> What exactly is Clear() doing?
BR>
BR> Why is recreating the table better than just clearing it? There's a
BR> big difference there and I'm not sure why performance suffers so
BR> greatly after Clear()ing.
BR>
BR> Thanks,
BR> Brett
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael said:
Hello Brett,

Use Reflector to look at DataTable.Clear code.
It performs a lot of job - check parent constrains, remove rows, clear indexes.

For the big tables you can end up with creating new dataTable is more quick
way than clearing existed

It's not the Clear() that is such a problem. It's once I do the
Clear() then start adding back that is the problem.

Brett
 
Back
Top