views:

531

answers:

4

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 + SQL Server 2008 + ADO.Net. If I load a table from a database by using a DataTable of ADO.Net, and in the database table, I defined a couple of indexes on the table. My question is, whether on the ADO.Net DataTable, there is related index (the same as the indexes I created on physical database table) to improve certain operation performance on DataTable?

thanks in advance, George

+3  A: 

George,

The answer is no.

Actually, some sort of indexing may be used internally, but only as an implementation detail. For instance, if you create a foreign key constraint, maybe that's assisted by an index. But it doesn't matter to a developer.

John Saunders
I am confused. 1. Can we use/create index in DataTable to improve performance? 2. If DataTable is not using index, how could it improve performance?
George2
George, why in the world do you think that you _need_ to improve performance?
John Saunders
Hi John, I think DataTable is a (memory) mapping or equivalent of a physical database table, and since we need index on physical database table to improve performance, why we do not have index concept on DataTable? Does it mean index not improving performance for DataTable?
George2
It means it's a bad question, George. You're assuming too much and wasting your time in the process. DataSet/DataTable are meant to be a disconnected, in-memory structure that matches the model of a relational database. That's the _model_, George, not the implementation, which is what an index is. So, no, don't worry about performance problems before they happen, or you'll find you've worried about the wrong problems. And don't worry about performance problems with DataTable.
John Saunders
BTW, is this you: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f21f5149-a6c2-4980-b72f-be6821a5cfcb
John Saunders
Yes, that is mine. :-)Sorry for my stupid John, if there is no index concept in DataTable, what data access pattern should we use to access data in DataTable in order to improve performance? Data access pattern I mean like, we should order by index column in database table (or in other words, we should not order by a column which does not have index on it). If we have no index in DataTable, then for order by/group by/sort in DataTable, I am not sure the performance issue (like we met with if we order by a column with no index)?
George2
George, my advice is to spend no more thought about performace issues with DataTable or anything else until you discover a performance issue. Certainly, I do not intend to spend any more thought on the subject.
John Saunders
Thanks John, final question, I want to confirm from developer point of view there is no concept of index on DataTable and for any data access pattern, we should leave it to DataTable itself to optimize?
George2
George, optimize what? Do you have a performance problem? Also, you seem to think that DataTable is an in-memory database. It is not. There is no optimization. There is nothing to optimize.
John Saunders
John, I finally understand your points. I have marked your reply as answer.
George2
+1  A: 

John above is correct. DataTables are disconnected in memory structures. They do not map to the physical implementation of the database.

The indexes on disk are used to speed up lookups because you don't have all the rows. If you have to load every row and scan them it is slow, so an index makes sense. In a DataTable you already have all the rows, so a comparison is fast already.

Jason Short
+1  A: 

Others have made the point that a DataSet is not intended to serve as a database system--just a representation of data. If you are working under the impression that a DataSet is a database then you are mistaken and might need to reconsider your implementation.

If you need a client-side database, consider using SQL Compact or SQL Lite, both are free redistributable Database systems which can be used without requiring separate installations or services. If you need something more full-featured the SQL Express is the next step up.

To help clarify though, DataSets/Tables are used in .NET development to temporarily hold data as needed. Think of them as the results of a SELECT query against a database; they are roughly similar to CSV files or other forms of tabular data--you can pull data into them from a database, work with the data, and then push the changes back to a database--but they, on their own, are not databases.

If you have a large collection of items which you need to keep in memory for one reason or another then you might consider building a lightweight DTO (data transfer object, Google it, they're very simple) and loading them into a HashTable. HashTables won't give you any form of relational data, but are very efficient at look-ups.

STW
Thanks John and Yoooder, I am thinking of why I am confused before. I think even if DataTable's initial values are retrieved from a SELECT from database normally, but we can issue select on the DataTable to get sub-set of data from DataTable, and it is why I am confused before and it is why I think when I issue SELECT on DataTable, maybe I need to create index to facilitate query performance, any comments?
George2
A: 

DataTables have a PrimaryKey field that can serve as an index (they are fast already anyway). This field is not copied from the Primary Keys of the database (although that might be nice).

PRMan