473,703 Members | 3,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Selec tCommand.Comman dText =
GetUwitmPdfArch iveQuery;
DataSet ds = new DataSet("Result s");
dbAdapter.Fill( ds);
DataColumn dcolColumn = new DataColumn("Chr isSummary",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn);
DataColumn dcolColumn2 = new DataColumn("Chr isTitle",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn2);
DataColumn dcolColumn3 = new DataColumn("Pub licationTitle",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn3);
DataColumn dcolColumn4 = new DataColumn("Ori ginalArticleUrl ",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn4);

// [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().Rep lace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Rep lace(".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.LoadByPrimar yKey(Convert.To Int32(tempInt)) ;
dr["OriginalArticl eUrl"] = ci.ConUrl;
dr["ChrisSumma ry"] = ci.ConSummary;
dr["ChrisTitle "] = ci.ConTitle;
dr["PublicationTit le"] = ci.ConSource;

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

listOfIds = listOfIds.Subst ring(0, listOfIds.Lengt h - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='9999 9999' " + listOfIds;
Would very much appreciate help making the above code efficient! Thanks!!!

-KF

Jul 18 '08 #1
3 2840
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(ex pression);

// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Lengt h; i ++)
{
Console.WriteLi ne(foundRows[i][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" <ke*****@newsgr oup.nospamwrote in message
news:5F******** *************** ***********@mic rosoft.com...
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.Selec tCommand.Comman dText =
GetUwitmPdfArch iveQuery;
DataSet ds = new DataSet("Result s");
dbAdapter.Fill( ds);
DataColumn dcolColumn = new DataColumn("Chr isSummary",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn);
DataColumn dcolColumn2 = new DataColumn("Chr isTitle",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn2);
DataColumn dcolColumn3 = new DataColumn("Pub licationTitle",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn3);
DataColumn dcolColumn4 = new DataColumn("Ori ginalArticleUrl ",
typeof(string)) ;
ds.Tables[0].Columns.Add(dc olColumn4);

// [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().Rep lace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Rep lace(".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.LoadByPrimar yKey(Convert.To Int32(tempInt)) ;
dr["OriginalArticl eUrl"] = ci.ConUrl;
dr["ChrisSumma ry"] = ci.ConSummary;
dr["ChrisTitle "] = ci.ConTitle;
dr["PublicationTit le"] = ci.ConSource;

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

listOfIds = listOfIds.Subst ring(0, listOfIds.Lengt h - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='9999 9999' " + listOfIds;
Would very much appreciate help making the above code efficient! Thanks!!!

-KF
Jul 19 '08 #2
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(bot h 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(gener ated 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/libr...le.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:
ms****@microsof t.com.

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

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Ken Fine" <ke*****@newsgr oup.nospam>
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
correspondin g 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.Selec tCommand.Comman dText =
GetUwitmPdfArc hiveQuery;
DataSet ds = new DataSet("Result s");
dbAdapter.Fill( ds);
DataColumn dcolColumn = new DataColumn("Chr isSummary",
typeof(string) );
ds.Tables[0].Columns.Add(dc olColumn);
DataColumn dcolColumn2 = new DataColumn("Chr isTitle",
typeof(string) );
ds.Tables[0].Columns.Add(dc olColumn2);
DataColumn dcolColumn3 = new DataColumn("Pub licationTitle",
typeof(string) );
ds.Tables[0].Columns.Add(dc olColumn3);
DataColumn dcolColumn4 = new DataColumn("Ori ginalArticleUrl ",
typeof(string) );
ds.Tables[0].Columns.Add(dc olColumn4);

// [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.ToInt3 2(dr[4].ToString().Rep lace(".pdf", ""));
try
{

// I start concatenating a SQL string of all of the primary keys
listOfIds = listOfIds + " OR ContentID ='" +
dr[4].ToString().Rep lace(".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.LoadByPrimar yKey(Convert.To Int32(tempInt)) ;
dr["OriginalArticl eUrl"] = ci.ConUrl;
dr["ChrisSumma ry"] = ci.ConSummary;
dr["ChrisTitle "] = ci.ConTitle;
dr["PublicationTit le"] = ci.ConSource;

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

listOfIds = listOfIds.Subst ring(0, listOfIds.Lengt h - 25);

string sqlQueryIds = "Select * From Contentitems WHERE
ContentID='999 99999' " + listOfIds;
Would very much appreciate help making the above code efficient! Thanks!!!

-KF

Jul 21 '08 #3
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:
ms****@microsof t.com.

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: st*****@online. microsoft.com (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.)
>
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(bot h 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(gener ated 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/libr...le.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,

Steve>>

Jul 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1628
by: Jeff Wilson | last post by:
Am relatively new to .NET and am trying to use it to create a usage billing system that can process a million or two records each day. Am currently using a simple system written in C that uses hash tables to look up all the information necessary to bill each record. My question is, If I use a DataSet to maintain all the various lookup tables involved in billing each record (some are small, some have 100,000 records), can accessing...
3
1730
by: alwayswinter | last post by:
I currently have a form where a user can enter results from a genetic test. I also have a pool of summaries that would correspond to different results that a user would enter into the form. I ideally I would like to have the user enter their results and then have a master summary created with the different word or HTML files on submit. (condition 1 + condition 2 = combining/displaying corresponding HTML files) I've gotten as far as...
1
8120
by: Shaileen Patel | last post by:
Hi, I am trying to convert a lot of web reports from ASP/VB to ASP.NET/VB.NET. I would like to save my dataset in XML and then use XSLT to transform the XML. The catch is I would like to have the XML in the format that ADO uses versus the default format in which ADO.NET uses. Currently, I am using ADO within .NET, but I am concerned about the performance and it might improve if I used the native data providers for .NET. I am not sure if...
0
1177
by: Winterminute | last post by:
I am binding the results of a database query to a DataList using the following code: SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM _Hardware_Configurations", myConnection); DataSet myDs = new DataSet(); myCommand.Fill(myDs, "_Hardware_Configurations"); dtlConfigurations.DataSource = myDs.Tables.DefaultView; dtlConfigurations.DataBind();
19
2749
by: Andy B | last post by:
Hello, Sorry for this newbish question. Briefly, my problem: ------------------ I expect the database I'm working on to reach something in the order of 12-16 Gigabytes, and I am interested in understanding as much as I can about how I can make this go as fast as possible on a linux system. I haven't run such a large database before. The nature of the database is such that
6
1359
by: Karlo Lozovina | last post by:
Let's say I have a class with few string properties and few integers, and a lot of methods defined for that class. Now if I have hundreds of thousands (or even more) of instances of that class - is it more efficient to remove those methods and make them separate functions, or it doesn't matter? Thanks... --
2
4127
by: J055 | last post by:
Hi I need to search a number of DataTables within a DataSet (with some relationships) and then display the filtered results in a GridView. The Columns that need to be displayed come from 2 of the tables but the search needs check 3 or 4 tables. Do I need to create a new DataTable which has the columns from both the tables so I can display in the Gridview or can I get the columns somehow from the DataSet without creating a new table? I...
5
1966
by: cj2 | last post by:
This code works: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols;
3
2076
by: zufie | last post by:
I have two drop down boxes, for the fields "County" and "Category" on a form which when selected for the respective County and Category will return Agency Names of Agencies within the respective County and Category. For example, when I select Cook County and Cardiovascular disease I expect the query(ies) behind the form to return Names of Agencies in Cook County that provide services regarding Cardiovascular care. However, what is...
0
8760
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4433
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3125
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.