473,491 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Most efficient way to insert 1,000,000 records?

Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.

I created a stored procedure and called it from ADO to insert one by one. It
is kind of slow (seems slower than using a DTS package to import from a file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.

Thanks a lot

Chris
Jan 4 '06 #1
13 2676
Chris,

I would send the parameters over in batches... Unfortunately, ADO.NET
doesn't get command batching for SQL Server until .NET 2.0.

Because of this, I would try and use DTS. What is the format of the
file to begin with? You might have much faster results doing that...

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.

I created a stored procedure and called it from ADO to insert one by one.
It
is kind of slow (seems slower than using a DTS package to import from a
file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.

Thanks a lot

Chris

Jan 4 '06 #2

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.

I created a stored procedure and called it from ADO to insert one by one.
It
is kind of slow (seems slower than using a DTS package to import from a
file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.

Thanks a lot

Chris


The most efficient and fastest way is using Sqlserver's bcp utility, you can
also use T-SQL BULK INSERT, but anything else will be much slower.

Willy.
Jan 4 '06 #3
Hi,
"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.
In a C# app or in a DB used by a C# app?
I created a stored procedure and called it from ADO to insert one by one.
It
is kind of slow (seems slower than using a DTS package to import from a
file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.


In short, none, ADO.NET is not intented for this. You have two options, use
bulk copy (bcp.exe ) that comes with SQL server or as Paldino suggested
(that is also my recommended solution) using DTS, DTS can be scripted from
C# so you will have flexibility to change it as needed.
Just create a DTS from enterprise manager, select write package to disk and
later you can load and execute it.

Let me know if you need code, I do this in a couple of deployments.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 4 '06 #4
Thanks. I do not know that I can create DTS script from C#. Can you tell me
know how to do that?
One common thing I often need is to import data file with customized name,
like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
Manager. If I write a C# app to do this job, do you think that I can convert
the app to a DTS script?

Chris

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.


In a C# app or in a DB used by a C# app?
I created a stored procedure and called it from ADO to insert one by one.
It
is kind of slow (seems slower than using a DTS package to import from a
file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.


In short, none, ADO.NET is not intented for this. You have two options, use
bulk copy (bcp.exe ) that comes with SQL server or as Paldino suggested
(that is also my recommended solution) using DTS, DTS can be scripted from
C# so you will have flexibility to change it as needed.
Just create a DTS from enterprise manager, select write package to disk and
later you can load and execute it.

Let me know if you need code, I do this in a couple of deployments.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jan 4 '06 #5

I am still wondering whether I should ask the firm to convert our
development system from .NET 1.1 to .NET 2.0. It is costly considering the
licences related to studio and third party packages.

In .NET 2.0 ADO, can I gain much better performance using "command
batching"? WHat does it exactly mean of command batching in .NET 2? COuld
you give me an example?

Thanks

Chris
"Nicholas Paldino [.NET/C# MVP]" wrote:
Chris,

I would send the parameters over in batches... Unfortunately, ADO.NET
doesn't get command batching for SQL Server until .NET 2.0.

Because of this, I would try and use DTS. What is the format of the
file to begin with? You might have much faster results doing that...

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.

I created a stored procedure and called it from ADO to insert one by one.
It
is kind of slow (seems slower than using a DTS package to import from a
file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.

Thanks a lot

Chris


Jan 4 '06 #6
Chris,

If you use command batching, then internally, the SqlCommand will create
a multiple statement command, with the appropriate parameters, and then pass
that to SQL Server. I think that the maximum number of parameters that can
be handled in this way is 2100.

The benefit of this is that it will cause less round trips between the
server and your code, which always improves performance.

However, if you have your data in a file already that is
computer-readable, chances are the suggestion by Willy to use the BULK
INSERT command would work. Either that, or use DTS.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...

I am still wondering whether I should ask the firm to convert our
development system from .NET 1.1 to .NET 2.0. It is costly considering the
licences related to studio and third party packages.

In .NET 2.0 ADO, can I gain much better performance using "command
batching"? WHat does it exactly mean of command batching in .NET 2? COuld
you give me an example?

Thanks

Chris
"Nicholas Paldino [.NET/C# MVP]" wrote:
Chris,

I would send the parameters over in batches... Unfortunately,
ADO.NET
doesn't get command batching for SQL Server until .NET 2.0.

Because of this, I would try and use DTS. What is the format of the
file to begin with? You might have much faster results doing that...

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
> Hi,
>
> I need to insert more than 500,000 records at the end of the day in a
> C#
> application. I need to finish it as soon as possible.
>
> I created a stored procedure and called it from ADO to insert one by
> one.
> It
> is kind of slow (seems slower than using a DTS package to import from a
> file).
>
> Just a general question, in ADO, what will be the MOST efficient way to
> do
> this work. I normally do it as I described. I am using .NET framework
> 1.1
> and SQL Server 2000.
>
> Thanks a lot
>
> Chris


Jan 4 '06 #7
If your data file is a simple csv file, it takes two minutes to build a
commandline script that executes the bcp command using the datafile as is as
input file. Really, there is nothing in the world of SQL server that is
faster than bcp, it's specially designed for bulk inserts.
Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
Thanks. I do not know that I can create DTS script from C#. Can you tell
me
know how to do that?
One common thing I often need is to import data file with customized name,
like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
Manager. If I write a C# app to do this job, do you think that I can
convert
the app to a DTS script?

Chris

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
> Hi,
>
> I need to insert more than 500,000 records at the end of the day in a
> C#
> application. I need to finish it as soon as possible.


In a C# app or in a DB used by a C# app?
> I created a stored procedure and called it from ADO to insert one by
> one.
> It
> is kind of slow (seems slower than using a DTS package to import from a
> file).
>
> Just a general question, in ADO, what will be the MOST efficient way to
> do
> this work. I normally do it as I described. I am using .NET framework
> 1.1
> and SQL Server 2000.


In short, none, ADO.NET is not intented for this. You have two options,
use
bulk copy (bcp.exe ) that comes with SQL server or as Paldino suggested
(that is also my recommended solution) using DTS, DTS can be scripted
from
C# so you will have flexibility to change it as needed.
Just create a DTS from enterprise manager, select write package to disk
and
later you can load and execute it.

Let me know if you need code, I do this in a couple of deployments.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jan 4 '06 #8
Thanks for the advice. I rarely use bcp. If I have a file with name
unchnaged, I usually use DTS. However, if I have file name changed daily, for
example, for today, as 01_04_2006_Data.csv, and 01_05_2006_data.csv for
tomorrow, it is hard for me to handle in either way.

I do not want to rename the file to a fixed name using a script then import,
since it could import stale data. However, I do not know how to script a
changing name like this for a bcp script. That is why I am wondering whether
I can translate a C# app to a DTS or script, if possible. At least, I can
code it in C#.

Any suggestions?
Thanks

Chris

"Willy Denoyette [MVP]" wrote:
If your data file is a simple csv file, it takes two minutes to build a
commandline script that executes the bcp command using the datafile as is as
input file. Really, there is nothing in the world of SQL server that is
faster than bcp, it's specially designed for bulk inserts.
Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
Thanks. I do not know that I can create DTS script from C#. Can you tell
me
know how to do that?
One common thing I often need is to import data file with customized name,
like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
Manager. If I write a C# app to do this job, do you think that I can
convert
the app to a DTS script?

Chris

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:20**********************************@microsof t.com...
> Hi,
>
> I need to insert more than 500,000 records at the end of the day in a
> C#
> application. I need to finish it as soon as possible.

In a C# app or in a DB used by a C# app?

> I created a stored procedure and called it from ADO to insert one by
> one.
> It
> is kind of slow (seems slower than using a DTS package to import from a
> file).
>
> Just a general question, in ADO, what will be the MOST efficient way to
> do
> this work. I normally do it as I described. I am using .NET framework
> 1.1
> and SQL Server 2000.

In short, none, ADO.NET is not intented for this. You have two options,
use
bulk copy (bcp.exe ) that comes with SQL server or as Paldino suggested
(that is also my recommended solution) using DTS, DTS can be scripted
from
C# so you will have flexibility to change it as needed.
Just create a DTS from enterprise manager, select write package to disk
and
later you can load and execute it.

Let me know if you need code, I do this in a couple of deployments.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Jan 4 '06 #9
Nothing stops you from launching the bcp.exe command from C# using
Process.Start.
That way you could supply all command line arguments you need like the in
file name.

Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
Thanks for the advice. I rarely use bcp. If I have a file with name
unchnaged, I usually use DTS. However, if I have file name changed daily,
for
example, for today, as 01_04_2006_Data.csv, and 01_05_2006_data.csv for
tomorrow, it is hard for me to handle in either way.

I do not want to rename the file to a fixed name using a script then
import,
since it could import stale data. However, I do not know how to script a
changing name like this for a bcp script. That is why I am wondering
whether
I can translate a C# app to a DTS or script, if possible. At least, I can
code it in C#.

Any suggestions?
Thanks

Chris

"Willy Denoyette [MVP]" wrote:
If your data file is a simple csv file, it takes two minutes to build a
commandline script that executes the bcp command using the datafile as is
as
input file. Really, there is nothing in the world of SQL server that is
faster than bcp, it's specially designed for bulk inserts.
Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
> Thanks. I do not know that I can create DTS script from C#. Can you
> tell
> me
> know how to do that?
> One common thing I often need is to import data file with customized
> name,
> like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
> Manager. If I write a C# app to do this job, do you think that I can
> convert
> the app to a DTS script?
>
> Chris
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> Hi,
>>
>>
>> "chrisben" <ch******@discussions.microsoft.com> wrote in message
>> news:20**********************************@microsof t.com...
>> > Hi,
>> >
>> > I need to insert more than 500,000 records at the end of the day in
>> > a
>> > C#
>> > application. I need to finish it as soon as possible.
>>
>> In a C# app or in a DB used by a C# app?
>>
>> > I created a stored procedure and called it from ADO to insert one by
>> > one.
>> > It
>> > is kind of slow (seems slower than using a DTS package to import
>> > from a
>> > file).
>> >
>> > Just a general question, in ADO, what will be the MOST efficient way
>> > to
>> > do
>> > this work. I normally do it as I described. I am using .NET
>> > framework
>> > 1.1
>> > and SQL Server 2000.
>>
>> In short, none, ADO.NET is not intented for this. You have two
>> options,
>> use
>> bulk copy (bcp.exe ) that comes with SQL server or as Paldino
>> suggested
>> (that is also my recommended solution) using DTS, DTS can be scripted
>> from
>> C# so you will have flexibility to change it as needed.
>>
>>
>> Just create a DTS from enterprise manager, select write package to
>> disk
>> and
>> later you can load and execute it.
>>
>> Let me know if you need code, I do this in a couple of deployments.
>>
>>
>>
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>>
>>


Jan 4 '06 #10
On Wed, 4 Jan 2006 09:24:02 -0800, "chrisben"
<ch******@discussions.microsoft.com> wrote:
Hi,

I need to insert more than 500,000 records at the end of the day in a C#
application. I need to finish it as soon as possible.

I created a stored procedure and called it from ADO to insert one by one. It
is kind of slow (seems slower than using a DTS package to import from a file).

Just a general question, in ADO, what will be the MOST efficient way to do
this work. I normally do it as I described. I am using .NET framework 1.1
and SQL Server 2000.

Thanks a lot

Chris


One thing I have found helps with some large inserts is to turn off
all the indexing on the relevant tables before doing ths insert and
then rebuilding all the indexes afterwards. That way each record
insertion is just a record insertion rather than a record insertion
and an index update.

Obviously you need to take a backup if you want to go this route, but
with a large insertion you should probably take a backup first anyway.

rossum

--

The ultimate truth is that there is no ultimate truth
Jan 5 '06 #11
Hi,

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
Thanks. I do not know that I can create DTS script from C#. Can you tell
me
know how to do that?
No idea either :)
It can be done though, it's just that IMO it is not worth, it's easier doing
it in enterprise manager
One common thing I often need is to import data file with customized name,
like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
Manager. If I write a C# app to do this job, do you think that I can
convert
the app to a DTS script?


You did not understood me, the idea is to make like a template in
enterprise manager and later reconfigure it in your code, I create the DTS ,
make ALL the transformations I need and later at runtime I change the
connection properties , here is the code:

void RunPackage( string packSource, string packName, string dataSource)
{
try
{
DTS.Package2Class package = new Package2Class();
object pVarPersistStgOfHost = null;

// if you need to load from file
package.LoadFromStorageFile(
packSource,
null,
null,
null,
packName,
ref pVarPersistStgOfHost);

//This is the source connection
package._Package_Connections.Item(1).DataSource = dataSource;
package.Execute();
package.UnInitialize();

// force Release() on COM object
//
System.Runtime.InteropServices.Marshal.ReleaseComO bject(package);
package = null;
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 5 '06 #12
good idea. will give a try. thank you

"Willy Denoyette [MVP]" wrote:
Nothing stops you from launching the bcp.exe command from C# using
Process.Start.
That way you could supply all command line arguments you need like the in
file name.

Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
Thanks for the advice. I rarely use bcp. If I have a file with name
unchnaged, I usually use DTS. However, if I have file name changed daily,
for
example, for today, as 01_04_2006_Data.csv, and 01_05_2006_data.csv for
tomorrow, it is hard for me to handle in either way.

I do not want to rename the file to a fixed name using a script then
import,
since it could import stale data. However, I do not know how to script a
changing name like this for a bcp script. That is why I am wondering
whether
I can translate a C# app to a DTS or script, if possible. At least, I can
code it in C#.

Any suggestions?
Thanks

Chris

"Willy Denoyette [MVP]" wrote:
If your data file is a simple csv file, it takes two minutes to build a
commandline script that executes the bcp command using the datafile as is
as
input file. Really, there is nothing in the world of SQL server that is
faster than bcp, it's specially designed for bulk inserts.
Willy.

"chrisben" <ch******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
> Thanks. I do not know that I can create DTS script from C#. Can you
> tell
> me
> know how to do that?
> One common thing I often need is to import data file with customized
> name,
> like mm_dd_yy_file.csv. I cannot figure out how to do that in Enerprise
> Manager. If I write a C# app to do this job, do you think that I can
> convert
> the app to a DTS script?
>
> Chris
>
> "Ignacio Machin ( .NET/ C# MVP )" wrote:
>
>> Hi,
>>
>>
>> "chrisben" <ch******@discussions.microsoft.com> wrote in message
>> news:20**********************************@microsof t.com...
>> > Hi,
>> >
>> > I need to insert more than 500,000 records at the end of the day in
>> > a
>> > C#
>> > application. I need to finish it as soon as possible.
>>
>> In a C# app or in a DB used by a C# app?
>>
>> > I created a stored procedure and called it from ADO to insert one by
>> > one.
>> > It
>> > is kind of slow (seems slower than using a DTS package to import
>> > from a
>> > file).
>> >
>> > Just a general question, in ADO, what will be the MOST efficient way
>> > to
>> > do
>> > this work. I normally do it as I described. I am using .NET
>> > framework
>> > 1.1
>> > and SQL Server 2000.
>>
>> In short, none, ADO.NET is not intented for this. You have two
>> options,
>> use
>> bulk copy (bcp.exe ) that comes with SQL server or as Paldino
>> suggested
>> (that is also my recommended solution) using DTS, DTS can be scripted
>> from
>> C# so you will have flexibility to change it as needed.
>>
>>
>> Just create a DTS from enterprise manager, select write package to
>> disk
>> and
>> later you can load and execute it.
>>
>> Let me know if you need code, I do this in a couple of deployments.
>>
>>
>>
>> --
>> Ignacio Machin,
>> ignacio.machin AT dot.state.fl.us
>> Florida Department Of Transportation
>>
>>
>>


Jan 5 '06 #13

Thank you all for your time and the helpful suggestions. I think I have a
much better idea what I can try.

Have a nice day

Chris
Jan 5 '06 #14

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

Similar topics

7
2575
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
2
13366
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. ...
6
3237
by: John | last post by:
Just a general question... I'm currently using a combobox that when updated, opens a form with its recordset based on a query using the combo box value as the criteria. I'm I correct in...
11
3585
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
14
1816
by: Roy | last post by:
Apologies for the cross-post, but this truly is a two-sided question. Given the option of creating Looping statements within a stored proc of sql server or in the code-behind of an .net webpage,...
20
18305
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
1
1647
by: Michael Bray | last post by:
I have a table that stores data points for several different data sources. The general format is: DECLARE @Data TABLE ( DataID int, TimeCollected int, DataValue decimal(9,9) )
3
1849
by: manny | last post by:
Problem: how to have query show only most recent records. This query shows all exams in 2005 for particular individual (grades not shown to avoid embarrassing John Slacker!): SELECT...
21
1972
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
0
7118
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,...
0
7157
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,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
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...
0
5452
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,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
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 ...
1
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.