Write multiple data/rows in MSSQL database without hitting the performance - C#Is it possible to for SQL Output clause to return a column not being inserted?How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement?How perform SQLite query with a data reader without locking database?Parallel Bulk Inserting with SqlBulkCopy and AzureWhen using parallel threads, application getting stuckQuery performance, indexing, and prediction of write time performance hit of a covered index?To pull data from multiple databases into one using SSIS package but want to limit staging table to oneC# fastest way to insert data into SQL databaseC# Writing in the same Data Set in a Multi Threading environmentmessage ordering and parallel processing apache camelHow make Insert Into table SQL from DataSet

Java - What do constructor type arguments mean when placed *before* the type?

Fly on a jet pack vs fly with a jet pack?

Is there a word to describe the feeling of being transfixed out of horror?

How do I repair my stair bannister?

Is it possible to have a strip of cold climate in the middle of a planet?

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Greatest common substring

Freedom of speech and where it applies

Longest common substring in linear time

Find last 3 digits of this monster number

Is it improper etiquette to ask your opponent what his/her rating is before the game?

Transformation of random variables and joint distributions

My friend sent me a screenshot of a transaction hash, but when I search for it I find divergent data. What happened?

How do ground effect vehicles perform turns?

Should I install hardwood flooring or cabinets first?

Drawing a topological "handle" with Tikz

Folder comparison

Why in book's example is used 言葉(ことば) instead of 言語(げんご)?

How to color a curve

Can a significant change in incentives void an employment contract?

Can I use my Chinese passport to enter China after I acquired another citizenship?

Global amount of publications over time

What linear sensor for a keyboard?

Is there a conventional notation or name for the slip angle?



Write multiple data/rows in MSSQL database without hitting the performance - C#


Is it possible to for SQL Output clause to return a column not being inserted?How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement?How perform SQLite query with a data reader without locking database?Parallel Bulk Inserting with SqlBulkCopy and AzureWhen using parallel threads, application getting stuckQuery performance, indexing, and prediction of write time performance hit of a covered index?To pull data from multiple databases into one using SSIS package but want to limit staging table to oneC# fastest way to insert data into SQL databaseC# Writing in the same Data Set in a Multi Threading environmentmessage ordering and parallel processing apache camelHow make Insert Into table SQL from DataSet













2















I have an API which receives a large JSON object (~7MBs at least), this JSON consists of some nested objects as follows:



[

"CategoryId": "P1",
"CategoryName": "Pizza",
"Products": [

"ProductId": "PROD700",
"ProductName": "Pepperoni Feast Large",
"ProductPrice": "5.5",
"Choices": [

"CoiceId": "CH22",
"ChoiceName": "Crust",
"Extras": [

"ExtraId": "EX1",
"ExtraName": "Classic Hand Tossed",
"ExtraPrice": "1"
,

"ExtraId": "EX2",
"ExtraName": "Crunchy Thin Crust",
"ExtraPrice": "1.25"

]
,

"CoiceId": "CH24",
"ChoiceName": "Additionals",
"Extras": [

"ExtraId": "EX3",
"ExtraName": "Extra Black Olives",
"ExtraPrice": "0.325"
,

"ExtraId": "EX4",
"ExtraName": "Extra Jalapeno",
"ExtraPrice": "0.4"

]

]

]

]


This API will receive the JSON and save it in a queue, till another background service (i.e. console app or windows service) consumes the same API for read, and get a list of PENDING requests to be written in database.



In fact, this was a very sample object, but I just wanted to share the idea and structure of this object, and I have a monolithic database which has a huge traffic per second, so, I had the below options to go through:



  • Having some nested loops to save the above data one-by-one in database, which I consider it is too bad and it will hit the database performance due to the many round-trips, in addition, it will take too long time to get it done.

  • Using the previous scenario but with parallelism, so, we can have the same scenario with something like Parallel.For or so to reduce the execution time as much as I can, but we still have the issue of many database hits.

  • To overcome the above 2 challenges (execution time & multiple database hits), I thought about using staging tables concept along with SqlBulkCopy, so, we can have some staging tables in main db or tempdb, then after insertion/bulk copy I can have a stored procedure which has MERGE statement which will insert the data from those staging tables to the main tables in database. The main challenge here if the console app is processing more than one request/object at the same time, it will be an issue as the staging table will be locked during the SqlBulkCopy, in addition, removing the indexes from that staging table will be better during the copying process to speed it up as much as possible, but before MERGE we should have indexes to speed up the process of reading from those staging tables. The challenge comes up here with the CREATE & DROP indexes, it is too hard and not recommended, especially in 2 cases: (1) staging table has big amount of data. (2) what if I started creating indexes as a preparation for MERGE, but meanwhile, there is another SqlBulkCopy in in progress in parallel for another request.

What is the question? it is about app architecture ...
Simply, I wanna perform the writing process in database without hitting database performance, and without consuming a lot of resources of both app & db servers. In addition, I wanna resolve the challenges which I have mentioned above in suggested scenarios like the execution time & many round-trips on db & locking tables while SqlBulkCopy in case of having concurrent requests.



I am just sharing my thoughts about that, but I am totally opened to hear from you if you have a better idea/implementation for this scenario.



NOTES:



  • I am using ADO .NET along with stored procedures to speed up the whole process.

  • Each request should be written into the database within 5-10 minutes at max since its creation/posting time.

  • Sometimes, we can have multiple requests which should be written on parallel, and the sequential processing will not be good from business perspective.









share|improve this question



















  • 1





    You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

    – GarethD
    Mar 7 at 9:08












  • Will edit it now with more clarification ...

    – Ahmed Negm
    Mar 7 at 9:10











  • @GarethD I just edited the question

    – Ahmed Negm
    Mar 7 at 9:18






  • 1





    If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

    – GarethD
    Mar 7 at 9:46







  • 1





    Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

    – GarethD
    Mar 7 at 10:42















2















I have an API which receives a large JSON object (~7MBs at least), this JSON consists of some nested objects as follows:



[

"CategoryId": "P1",
"CategoryName": "Pizza",
"Products": [

"ProductId": "PROD700",
"ProductName": "Pepperoni Feast Large",
"ProductPrice": "5.5",
"Choices": [

"CoiceId": "CH22",
"ChoiceName": "Crust",
"Extras": [

"ExtraId": "EX1",
"ExtraName": "Classic Hand Tossed",
"ExtraPrice": "1"
,

"ExtraId": "EX2",
"ExtraName": "Crunchy Thin Crust",
"ExtraPrice": "1.25"

]
,

"CoiceId": "CH24",
"ChoiceName": "Additionals",
"Extras": [

"ExtraId": "EX3",
"ExtraName": "Extra Black Olives",
"ExtraPrice": "0.325"
,

"ExtraId": "EX4",
"ExtraName": "Extra Jalapeno",
"ExtraPrice": "0.4"

]

]

]

]


This API will receive the JSON and save it in a queue, till another background service (i.e. console app or windows service) consumes the same API for read, and get a list of PENDING requests to be written in database.



In fact, this was a very sample object, but I just wanted to share the idea and structure of this object, and I have a monolithic database which has a huge traffic per second, so, I had the below options to go through:



  • Having some nested loops to save the above data one-by-one in database, which I consider it is too bad and it will hit the database performance due to the many round-trips, in addition, it will take too long time to get it done.

  • Using the previous scenario but with parallelism, so, we can have the same scenario with something like Parallel.For or so to reduce the execution time as much as I can, but we still have the issue of many database hits.

  • To overcome the above 2 challenges (execution time & multiple database hits), I thought about using staging tables concept along with SqlBulkCopy, so, we can have some staging tables in main db or tempdb, then after insertion/bulk copy I can have a stored procedure which has MERGE statement which will insert the data from those staging tables to the main tables in database. The main challenge here if the console app is processing more than one request/object at the same time, it will be an issue as the staging table will be locked during the SqlBulkCopy, in addition, removing the indexes from that staging table will be better during the copying process to speed it up as much as possible, but before MERGE we should have indexes to speed up the process of reading from those staging tables. The challenge comes up here with the CREATE & DROP indexes, it is too hard and not recommended, especially in 2 cases: (1) staging table has big amount of data. (2) what if I started creating indexes as a preparation for MERGE, but meanwhile, there is another SqlBulkCopy in in progress in parallel for another request.

What is the question? it is about app architecture ...
Simply, I wanna perform the writing process in database without hitting database performance, and without consuming a lot of resources of both app & db servers. In addition, I wanna resolve the challenges which I have mentioned above in suggested scenarios like the execution time & many round-trips on db & locking tables while SqlBulkCopy in case of having concurrent requests.



I am just sharing my thoughts about that, but I am totally opened to hear from you if you have a better idea/implementation for this scenario.



NOTES:



  • I am using ADO .NET along with stored procedures to speed up the whole process.

  • Each request should be written into the database within 5-10 minutes at max since its creation/posting time.

  • Sometimes, we can have multiple requests which should be written on parallel, and the sequential processing will not be good from business perspective.









share|improve this question



















  • 1





    You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

    – GarethD
    Mar 7 at 9:08












  • Will edit it now with more clarification ...

    – Ahmed Negm
    Mar 7 at 9:10











  • @GarethD I just edited the question

    – Ahmed Negm
    Mar 7 at 9:18






  • 1





    If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

    – GarethD
    Mar 7 at 9:46







  • 1





    Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

    – GarethD
    Mar 7 at 10:42













2












2








2








I have an API which receives a large JSON object (~7MBs at least), this JSON consists of some nested objects as follows:



[

"CategoryId": "P1",
"CategoryName": "Pizza",
"Products": [

"ProductId": "PROD700",
"ProductName": "Pepperoni Feast Large",
"ProductPrice": "5.5",
"Choices": [

"CoiceId": "CH22",
"ChoiceName": "Crust",
"Extras": [

"ExtraId": "EX1",
"ExtraName": "Classic Hand Tossed",
"ExtraPrice": "1"
,

"ExtraId": "EX2",
"ExtraName": "Crunchy Thin Crust",
"ExtraPrice": "1.25"

]
,

"CoiceId": "CH24",
"ChoiceName": "Additionals",
"Extras": [

"ExtraId": "EX3",
"ExtraName": "Extra Black Olives",
"ExtraPrice": "0.325"
,

"ExtraId": "EX4",
"ExtraName": "Extra Jalapeno",
"ExtraPrice": "0.4"

]

]

]

]


This API will receive the JSON and save it in a queue, till another background service (i.e. console app or windows service) consumes the same API for read, and get a list of PENDING requests to be written in database.



In fact, this was a very sample object, but I just wanted to share the idea and structure of this object, and I have a monolithic database which has a huge traffic per second, so, I had the below options to go through:



  • Having some nested loops to save the above data one-by-one in database, which I consider it is too bad and it will hit the database performance due to the many round-trips, in addition, it will take too long time to get it done.

  • Using the previous scenario but with parallelism, so, we can have the same scenario with something like Parallel.For or so to reduce the execution time as much as I can, but we still have the issue of many database hits.

  • To overcome the above 2 challenges (execution time & multiple database hits), I thought about using staging tables concept along with SqlBulkCopy, so, we can have some staging tables in main db or tempdb, then after insertion/bulk copy I can have a stored procedure which has MERGE statement which will insert the data from those staging tables to the main tables in database. The main challenge here if the console app is processing more than one request/object at the same time, it will be an issue as the staging table will be locked during the SqlBulkCopy, in addition, removing the indexes from that staging table will be better during the copying process to speed it up as much as possible, but before MERGE we should have indexes to speed up the process of reading from those staging tables. The challenge comes up here with the CREATE & DROP indexes, it is too hard and not recommended, especially in 2 cases: (1) staging table has big amount of data. (2) what if I started creating indexes as a preparation for MERGE, but meanwhile, there is another SqlBulkCopy in in progress in parallel for another request.

What is the question? it is about app architecture ...
Simply, I wanna perform the writing process in database without hitting database performance, and without consuming a lot of resources of both app & db servers. In addition, I wanna resolve the challenges which I have mentioned above in suggested scenarios like the execution time & many round-trips on db & locking tables while SqlBulkCopy in case of having concurrent requests.



I am just sharing my thoughts about that, but I am totally opened to hear from you if you have a better idea/implementation for this scenario.



NOTES:



  • I am using ADO .NET along with stored procedures to speed up the whole process.

  • Each request should be written into the database within 5-10 minutes at max since its creation/posting time.

  • Sometimes, we can have multiple requests which should be written on parallel, and the sequential processing will not be good from business perspective.









share|improve this question
















I have an API which receives a large JSON object (~7MBs at least), this JSON consists of some nested objects as follows:



[

"CategoryId": "P1",
"CategoryName": "Pizza",
"Products": [

"ProductId": "PROD700",
"ProductName": "Pepperoni Feast Large",
"ProductPrice": "5.5",
"Choices": [

"CoiceId": "CH22",
"ChoiceName": "Crust",
"Extras": [

"ExtraId": "EX1",
"ExtraName": "Classic Hand Tossed",
"ExtraPrice": "1"
,

"ExtraId": "EX2",
"ExtraName": "Crunchy Thin Crust",
"ExtraPrice": "1.25"

]
,

"CoiceId": "CH24",
"ChoiceName": "Additionals",
"Extras": [

"ExtraId": "EX3",
"ExtraName": "Extra Black Olives",
"ExtraPrice": "0.325"
,

"ExtraId": "EX4",
"ExtraName": "Extra Jalapeno",
"ExtraPrice": "0.4"

]

]

]

]


This API will receive the JSON and save it in a queue, till another background service (i.e. console app or windows service) consumes the same API for read, and get a list of PENDING requests to be written in database.



In fact, this was a very sample object, but I just wanted to share the idea and structure of this object, and I have a monolithic database which has a huge traffic per second, so, I had the below options to go through:



  • Having some nested loops to save the above data one-by-one in database, which I consider it is too bad and it will hit the database performance due to the many round-trips, in addition, it will take too long time to get it done.

  • Using the previous scenario but with parallelism, so, we can have the same scenario with something like Parallel.For or so to reduce the execution time as much as I can, but we still have the issue of many database hits.

  • To overcome the above 2 challenges (execution time & multiple database hits), I thought about using staging tables concept along with SqlBulkCopy, so, we can have some staging tables in main db or tempdb, then after insertion/bulk copy I can have a stored procedure which has MERGE statement which will insert the data from those staging tables to the main tables in database. The main challenge here if the console app is processing more than one request/object at the same time, it will be an issue as the staging table will be locked during the SqlBulkCopy, in addition, removing the indexes from that staging table will be better during the copying process to speed it up as much as possible, but before MERGE we should have indexes to speed up the process of reading from those staging tables. The challenge comes up here with the CREATE & DROP indexes, it is too hard and not recommended, especially in 2 cases: (1) staging table has big amount of data. (2) what if I started creating indexes as a preparation for MERGE, but meanwhile, there is another SqlBulkCopy in in progress in parallel for another request.

What is the question? it is about app architecture ...
Simply, I wanna perform the writing process in database without hitting database performance, and without consuming a lot of resources of both app & db servers. In addition, I wanna resolve the challenges which I have mentioned above in suggested scenarios like the execution time & many round-trips on db & locking tables while SqlBulkCopy in case of having concurrent requests.



I am just sharing my thoughts about that, but I am totally opened to hear from you if you have a better idea/implementation for this scenario.



NOTES:



  • I am using ADO .NET along with stored procedures to speed up the whole process.

  • Each request should be written into the database within 5-10 minutes at max since its creation/posting time.

  • Sometimes, we can have multiple requests which should be written on parallel, and the sequential processing will not be good from business perspective.






c# sql-server parallel-processing architecture database-performance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 9:17







Ahmed Negm

















asked Mar 7 at 8:55









Ahmed NegmAhmed Negm

400318




400318







  • 1





    You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

    – GarethD
    Mar 7 at 9:08












  • Will edit it now with more clarification ...

    – Ahmed Negm
    Mar 7 at 9:10











  • @GarethD I just edited the question

    – Ahmed Negm
    Mar 7 at 9:18






  • 1





    If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

    – GarethD
    Mar 7 at 9:46







  • 1





    Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

    – GarethD
    Mar 7 at 10:42












  • 1





    You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

    – GarethD
    Mar 7 at 9:08












  • Will edit it now with more clarification ...

    – Ahmed Negm
    Mar 7 at 9:10











  • @GarethD I just edited the question

    – Ahmed Negm
    Mar 7 at 9:18






  • 1





    If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

    – GarethD
    Mar 7 at 9:46







  • 1





    Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

    – GarethD
    Mar 7 at 10:42







1




1





You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

– GarethD
Mar 7 at 9:08






You have done a good enough job of describing your scenario, but I don't think there is a single question contained within it (certainly no question marks). You may elicit a better response if you break this down into more specific questions, otherwise there is a good chance this will be closed as too broad. Also, what version of SQL Server are you using?

– GarethD
Mar 7 at 9:08














Will edit it now with more clarification ...

– Ahmed Negm
Mar 7 at 9:10





Will edit it now with more clarification ...

– Ahmed Negm
Mar 7 at 9:10













@GarethD I just edited the question

– Ahmed Negm
Mar 7 at 9:18





@GarethD I just edited the question

– Ahmed Negm
Mar 7 at 9:18




1




1





If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

– GarethD
Mar 7 at 9:46






If it is just the processing, I would be inclined to process the JSON outside of your database and parse it into tables that are similar to your database structure, then save the data into your database by passing these datatables to stored procedures using table-valued parameters.

– GarethD
Mar 7 at 9:46





1




1





Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

– GarethD
Mar 7 at 10:42





Capturing autogenerated IDs is fairly trivial if you use OUTPUT. If you use OUTPUT along with MERGE this will allow you to capture rows from both the source and the destination, and can help avoid duplicating work (e.g. inserting in one statement, then looking up against the table to get the ID in another statement)

– GarethD
Mar 7 at 10:42












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039667%2fwrite-multiple-data-rows-in-mssql-database-without-hitting-the-performance-c-s%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55039667%2fwrite-multiple-data-rows-in-mssql-database-without-hitting-the-performance-c-s%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко