How can I make this more efficient? (combining DataSet results with the results of a DB lookup.)

  • Thread starter Thread starter Ken Fine
  • Start date Start date
K

Ken Fine

This is a question that someone familiar with ASP.NET and ADO.NET DataSets
and DataTables should be able to answer fairly easily. The basic question is
how I can efficiently match data from one dataset to data in a second
dataset, using a common key. I will first describe the problem in words and
then I will show my code, which has most of the solution done already.

I have built an ASP.NET that queries an Index Server and returns a DataSet
corresponding to a bunch of PDF files. I bind this DataSet to an ASP.NET
gridview, but I want to include more information than is in Index Server
alone (and no, I don't want to use IS custom properties, nor do I want to
use MSFT's great new free search server quite yet.) . I want to do a second
database lookup to gather more information and I have a good idea how to do
that. I have successfully manually added extra columns to my DataSet's
datatable and populated them with database data in an inefficient way which
I will describe in the next paragraph.

The filenames associated with the PDFs in the Index Server index include
database Primary Keys and my application parses those keys out. I use those
primary keys to subsequently do a second collection of database lookups.
Currently just for test purposes I have the application doing dozens of
lookups: it's querying the database on each iteration of a loop. This sucks
from a performance standpoint. In anticipation of doing a single query of
all necessary lookups, I have made it so that the system builds a functional
SQL query string that pulls all necessary records with additional metadata.

What I need to know is:
1) Syntax for creating a new dataset based on my SQL string. I normally use
the EntitySpaces ORM framework so my experience with Datasets is very weak.
2) How to match the parts of the new dataset into my existing dataset in an
EFFICIENT way. My code below will show a successful solution based on a very
INEFFICIENT way, using repeated lookups. I expect the solution will involve
repeated filterings of the single dataset based on the primary key.

Now to my existing, inefficient code. I will walk you through it.

// Filling a DataSet called results, and manually adding some additional
columns to it:

if (dbAdapter != null) dbAdapter.SelectCommand.CommandText =
GetUwitmPdfArchiveQuery;
DataSet ds = new DataSet("Results");
dbAdapter.Fill(ds);
DataColumn dcolColumn = new DataColumn("ChrisSummary",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn);
DataColumn dcolColumn2 = new DataColumn("ChrisTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn2);
DataColumn dcolColumn3 = new DataColumn("PublicationTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn3);
DataColumn dcolColumn4 = new DataColumn("OriginalArticleUrl",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn4);

// [empty variables are initialized]

// start looking through the datarows. There is a DB primary key in the data
one of the columns of the datarow . I grab that key and assign it the name
tempInt

foreach (DataRow dr in ds.Tables[0].Rows)
{
int tempInt =
Convert.ToInt32(dr[4].ToString().Replace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Replace(".pdf", "") + "'";

// This is the inefficient part. I'm using that key along with my
EntitySpaces ORM framework to do lookups each time this loop is iterated,
and populate my manually added rows. I want instead to do a single lookup
and use a second filtered DataSet to populate my manually added rows with DB
information. So I'm going to have to loop through once to get enough info to
build my SQL string, then do something else to build the DataSet I need.
This is where I need the help.

Contentitems ci = new Contentitems();
ci.LoadByPrimaryKey(Convert.ToInt32(tempInt));
dr["OriginalArticleUrl"] = ci.ConUrl;
dr["ChrisSummary"] = ci.ConSummary;
dr["ChrisTitle"] = ci.ConTitle;
dr["PublicationTitle"] = ci.ConSource;

// I finish my SQL statement and whack off some extraneous stuff from my SQL
string

listOfIds = listOfIds.Substring(0, listOfIds.Length - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='99999999' " + listOfIds;


Would very much appreciate help making the above code efficient! Thanks!!!

-KF
 
The easiest/fastest way is to use a filter on the datatable. I'll qualify
this by saying I haven't tested this against LINQ yet.

Roughly, you will pass in a sql like query to the datatable's select method
and it will return the rows that match the filter. Here is a complete
example from MSDN.

private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[0]);
}
}

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Ken Fine said:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets
and DataTables should be able to answer fairly easily. The basic question
is how I can efficiently match data from one dataset to data in a second
dataset, using a common key. I will first describe the problem in words
and then I will show my code, which has most of the solution done already.

I have built an ASP.NET that queries an Index Server and returns a DataSet
corresponding to a bunch of PDF files. I bind this DataSet to an ASP.NET
gridview, but I want to include more information than is in Index Server
alone (and no, I don't want to use IS custom properties, nor do I want to
use MSFT's great new free search server quite yet.) . I want to do a
second database lookup to gather more information and I have a good idea
how to do that. I have successfully manually added extra columns to my
DataSet's datatable and populated them with database data in an
inefficient way which I will describe in the next paragraph.

The filenames associated with the PDFs in the Index Server index include
database Primary Keys and my application parses those keys out. I use
those primary keys to subsequently do a second collection of database
lookups. Currently just for test purposes I have the application doing
dozens of lookups: it's querying the database on each iteration of a
loop. This sucks from a performance standpoint. In anticipation of doing a
single query of all necessary lookups, I have made it so that the system
builds a functional SQL query string that pulls all necessary records with
additional metadata.

What I need to know is:
1) Syntax for creating a new dataset based on my SQL string. I normally
use the EntitySpaces ORM framework so my experience with Datasets is very
weak.
2) How to match the parts of the new dataset into my existing dataset in
an EFFICIENT way. My code below will show a successful solution based on a
very INEFFICIENT way, using repeated lookups. I expect the solution will
involve repeated filterings of the single dataset based on the primary
key.

Now to my existing, inefficient code. I will walk you through it.

// Filling a DataSet called results, and manually adding some additional
columns to it:

if (dbAdapter != null) dbAdapter.SelectCommand.CommandText =
GetUwitmPdfArchiveQuery;
DataSet ds = new DataSet("Results");
dbAdapter.Fill(ds);
DataColumn dcolColumn = new DataColumn("ChrisSummary",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn);
DataColumn dcolColumn2 = new DataColumn("ChrisTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn2);
DataColumn dcolColumn3 = new DataColumn("PublicationTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn3);
DataColumn dcolColumn4 = new DataColumn("OriginalArticleUrl",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn4);

// [empty variables are initialized]

// start looking through the datarows. There is a DB primary key in the
data one of the columns of the datarow . I grab that key and assign it the
name tempInt

foreach (DataRow dr in ds.Tables[0].Rows)
{
int tempInt =
Convert.ToInt32(dr[4].ToString().Replace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Replace(".pdf", "") + "'";

// This is the inefficient part. I'm using that key along with my
EntitySpaces ORM framework to do lookups each time this loop is iterated,
and populate my manually added rows. I want instead to do a single lookup
and use a second filtered DataSet to populate my manually added rows with
DB information. So I'm going to have to loop through once to get enough
info to build my SQL string, then do something else to build the DataSet I
need. This is where I need the help.

Contentitems ci = new Contentitems();
ci.LoadByPrimaryKey(Convert.ToInt32(tempInt));
dr["OriginalArticleUrl"] = ci.ConUrl;
dr["ChrisSummary"] = ci.ConSummary;
dr["ChrisTitle"] = ci.ConTitle;
dr["PublicationTitle"] = ci.ConSource;

// I finish my SQL statement and whack off some extraneous stuff from my
SQL string

listOfIds = listOfIds.Substring(0, listOfIds.Length - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='99999999' " + listOfIds;


Would very much appreciate help making the above code efficient! Thanks!!!

-KF
 
Hi KF,

Based on your description, I understand that you have a DataSet/DataTable
generqted via some query from index server. And since each of the record
may have some additional data/fields in database, currently you're
performing a data access query from database server when looping each of
the DataSet/DataTable record, correct?

Yes, I can see that there does be some performance overhead when the record
count is quite large. In my opinion, I think you can consider the following
approaches to impove the query performance(both of them require more memory
space since you'll need to cache those additional fields locally in memory).

1. Before you loop the first Dataset/DataTable(generated from index server
), you can perform another query against the database which return all the
necessary records(contain the required additional fields), you can sort
them via the primary key(you want to search against later), this sort is at
T-SQL level. You can also sort it in DataTable , but I think T-SQL layer
will be more efficient:

#DataTable..::.Select Method
http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx

#Filtering and Sorting with the DataTable Select Me
http://www.developerfusion.co.uk/show/4703/2/

After that , you get a sorted record sequence in memory (based primarykey).
Later in your code which loop each record in the index server records, you
can perform a binary search like approach in the returned records set above.

2. If the data in database is quite simple(not many fields), you can even
directly store all the fields into hashtable based on the certain primary
key. Later you can directly access HashTable for the requied data. This is
extremely quick and efficient.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Ken Fine" <[email protected]>
Subject: How can I make this more efficient? (combining DataSet results
with the results of a DB lookup.)
Date: Fri, 18 Jul 2008 15:25:18 -0700
This is a question that someone familiar with ASP.NET and ADO.NET DataSets
and DataTables should be able to answer fairly easily. The basic question is
how I can efficiently match data from one dataset to data in a second
dataset, using a common key. I will first describe the problem in words and
then I will show my code, which has most of the solution done already.

I have built an ASP.NET that queries an Index Server and returns a DataSet
corresponding to a bunch of PDF files. I bind this DataSet to an ASP.NET
gridview, but I want to include more information than is in Index Server
alone (and no, I don't want to use IS custom properties, nor do I want to
use MSFT's great new free search server quite yet.) . I want to do a second
database lookup to gather more information and I have a good idea how to do
that. I have successfully manually added extra columns to my DataSet's
datatable and populated them with database data in an inefficient way which
I will describe in the next paragraph.

The filenames associated with the PDFs in the Index Server index include
database Primary Keys and my application parses those keys out. I use those
primary keys to subsequently do a second collection of database lookups.
Currently just for test purposes I have the application doing dozens of
lookups: it's querying the database on each iteration of a loop. This sucks
from a performance standpoint. In anticipation of doing a single query of
all necessary lookups, I have made it so that the system builds a functional
SQL query string that pulls all necessary records with additional metadata.

What I need to know is:
1) Syntax for creating a new dataset based on my SQL string. I normally use
the EntitySpaces ORM framework so my experience with Datasets is very weak.
2) How to match the parts of the new dataset into my existing dataset in an
EFFICIENT way. My code below will show a successful solution based on a very
INEFFICIENT way, using repeated lookups. I expect the solution will involve
repeated filterings of the single dataset based on the primary key.

Now to my existing, inefficient code. I will walk you through it.

// Filling a DataSet called results, and manually adding some additional
columns to it:

if (dbAdapter != null) dbAdapter.SelectCommand.CommandText =
GetUwitmPdfArchiveQuery;
DataSet ds = new DataSet("Results");
dbAdapter.Fill(ds);
DataColumn dcolColumn = new DataColumn("ChrisSummary",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn);
DataColumn dcolColumn2 = new DataColumn("ChrisTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn2);
DataColumn dcolColumn3 = new DataColumn("PublicationTitle",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn3);
DataColumn dcolColumn4 = new DataColumn("OriginalArticleUrl",
typeof(string));
ds.Tables[0].Columns.Add(dcolColumn4);

// [empty variables are initialized]

// start looking through the datarows. There is a DB primary key in the data
one of the columns of the datarow . I grab that key and assign it the name
tempInt

foreach (DataRow dr in ds.Tables[0].Rows)
{
int tempInt =
Convert.ToInt32(dr[4].ToString().Replace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Replace(".pdf", "") + "'";

// This is the inefficient part. I'm using that key along with my
EntitySpaces ORM framework to do lookups each time this loop is iterated,
and populate my manually added rows. I want instead to do a single lookup
and use a second filtered DataSet to populate my manually added rows with DB
information. So I'm going to have to loop through once to get enough info to
build my SQL string, then do something else to build the DataSet I need.
This is where I need the help.

Contentitems ci = new Contentitems();
ci.LoadByPrimaryKey(Convert.ToInt32(tempInt));
dr["OriginalArticleUrl"] = ci.ConUrl;
dr["ChrisSummary"] = ci.ConSummary;
dr["ChrisTitle"] = ci.ConTitle;
dr["PublicationTitle"] = ci.ConSource;

// I finish my SQL statement and whack off some extraneous stuff from my SQL
string

listOfIds = listOfIds.Substring(0, listOfIds.Length - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='99999999' " + listOfIds;


Would very much appreciate help making the above code efficient! Thanks!!!

-KF
 
Hi KF,

Have you got any further ideas on this or have you made the decision which
approach to use? If there is anything else we can help, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Mon, 21 Jul 2008 03:54:01 GMT
Subject: RE: How can I make this more efficient? (combining DataSet
results with the results of a DB lookup.)
 
Back
Top