By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.