Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do MERGE using Linq to SQL

SQL Server 2008 Ent ASP.NET MVC 2.0 Linq-to-SQL

I am building a gaming site, that tracks when a particular player (toon) had downed a particular monster (boss). Table looks something like:

int ToonId
int BossId
datetime LastKillTime

I use a 3d party service that gives me back latest information (toon,boss,time).
Now I want to update my database with that new information.
Brute force approach is to do line-by-line upsert. But It looks ugly (code-wise), and probably slow too.

I think better solution would be to insert new data (using temp table?) and then run MERGE statement.

Is it good idea? I know temp tables are "better-to-avoid". Should I create a permanent "temp" table just for this operation?
Or should I just read entire current set (100 rows at most), do merge and put it back from within application?

Any pointers/suggestions are always appreciated.

like image 986
THX-1138 Avatar asked Jul 06 '10 16:07

THX-1138


People also ask

What is the use of linq2db merge?

This API available since linq2db 1.9.0. It superseeds previous version of API with very limited functionality. For migration from old API check link below. Merge is an atomic operation to update table (target) content using other table (source). Merge API provides methods to build Merge command and execute it.

How to use LINQ join like SQL join?

LINQ has a JOIN query operator that gives you SQL JOIN like behavior and syntax. The JOIN query operator compares the specified properties/keys of two collections for equality by using the EQUALS keyword. By default, all join queries written by the JOIN keyword are treated as equijoins. Let's understand the LINQ Joins using Venn diagram.

Can LINQ be used to query a SQL Server database?

LINQ really shines when used to query remote data sources. Queries on remote data sources such as relational databases are known as integrated queries. In this article, I explore integrated queries with a SQL Server database. First I create the SQL Server database instance with Docker and then query it using LINQ.

What is join clause in SQL Server?

As we know the JOIN clause is very useful when merging more than two table or object data into a single unit. It combines different source elements into one and also creates the relationship between them. Using the join, you can grab the data based on your conditions.


1 Answers

An ORM is the wrong tool for performing batch operations, and Linq-to-SQL is no exception. In this case I think you have picked the right solution: Store all entries in a temporary table quickly, then do the UPSERT using merge.

The fastest way to store the data to the temporary table is to use SqlBulkCopy to store all data to a table of your choice.

like image 58
Panagiotis Kanavos Avatar answered Oct 14 '22 07:10

Panagiotis Kanavos