Improving predominance for updating 40000 records using entity framework [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-1
down vote

favorite












I have Two tables Circuit and Tag,



Circuit 
------------------
Id Name Path

Tag
-----------
Id Name Circuit_Id


Now I have to loop through all the tags and update each tag where there is a matching path and name (circuit path and Tag name).



I am doing this and it works fine , but it's really slow. Is there a way I could improve the speed.



foreach (var tag in allPDUTags)

var removedUnderScoreLast = tag.Path.Remove(tag.Path.LastIndexOf('_'));
var removedUnderScoreLastButOne = removedUnderScoreLast.Remove(removedUnderScoreLast.LastIndexOf('_'));

var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;

tag.Circuit_Id = circuitId;


_counter++;

if ((_counter % 1000) == 0)

Console.WriteLine($"Updated _counter of totalsTags Tags");










share|improve this question













closed as off-topic by t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch Mar 23 at 14:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
    – t3chb0t
    Mar 22 at 7:56










  • @t3chb0t, the C# code is the actual code, SQL table us just the column names
    – Simsons
    Mar 22 at 11:11










  • What is allPDUTags? Please show the code that creates it.
    – Gert Arnold
    Mar 22 at 16:28







  • 2




    I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
    – Hosch250
    Mar 22 at 20:27






  • 1




    Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
    – paparazzo
    Mar 23 at 11:56
















up vote
-1
down vote

favorite












I have Two tables Circuit and Tag,



Circuit 
------------------
Id Name Path

Tag
-----------
Id Name Circuit_Id


Now I have to loop through all the tags and update each tag where there is a matching path and name (circuit path and Tag name).



I am doing this and it works fine , but it's really slow. Is there a way I could improve the speed.



foreach (var tag in allPDUTags)

var removedUnderScoreLast = tag.Path.Remove(tag.Path.LastIndexOf('_'));
var removedUnderScoreLastButOne = removedUnderScoreLast.Remove(removedUnderScoreLast.LastIndexOf('_'));

var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;

tag.Circuit_Id = circuitId;


_counter++;

if ((_counter % 1000) == 0)

Console.WriteLine($"Updated _counter of totalsTags Tags");










share|improve this question













closed as off-topic by t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch Mar 23 at 14:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
    – t3chb0t
    Mar 22 at 7:56










  • @t3chb0t, the C# code is the actual code, SQL table us just the column names
    – Simsons
    Mar 22 at 11:11










  • What is allPDUTags? Please show the code that creates it.
    – Gert Arnold
    Mar 22 at 16:28







  • 2




    I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
    – Hosch250
    Mar 22 at 20:27






  • 1




    Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
    – paparazzo
    Mar 23 at 11:56












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have Two tables Circuit and Tag,



Circuit 
------------------
Id Name Path

Tag
-----------
Id Name Circuit_Id


Now I have to loop through all the tags and update each tag where there is a matching path and name (circuit path and Tag name).



I am doing this and it works fine , but it's really slow. Is there a way I could improve the speed.



foreach (var tag in allPDUTags)

var removedUnderScoreLast = tag.Path.Remove(tag.Path.LastIndexOf('_'));
var removedUnderScoreLastButOne = removedUnderScoreLast.Remove(removedUnderScoreLast.LastIndexOf('_'));

var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;

tag.Circuit_Id = circuitId;


_counter++;

if ((_counter % 1000) == 0)

Console.WriteLine($"Updated _counter of totalsTags Tags");










share|improve this question













I have Two tables Circuit and Tag,



Circuit 
------------------
Id Name Path

Tag
-----------
Id Name Circuit_Id


Now I have to loop through all the tags and update each tag where there is a matching path and name (circuit path and Tag name).



I am doing this and it works fine , but it's really slow. Is there a way I could improve the speed.



foreach (var tag in allPDUTags)

var removedUnderScoreLast = tag.Path.Remove(tag.Path.LastIndexOf('_'));
var removedUnderScoreLastButOne = removedUnderScoreLast.Remove(removedUnderScoreLast.LastIndexOf('_'));

var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;

tag.Circuit_Id = circuitId;


_counter++;

if ((_counter % 1000) == 0)

Console.WriteLine($"Updated _counter of totalsTags Tags");












share|improve this question












share|improve this question




share|improve this question








edited Mar 22 at 7:54









t3chb0t

32.1k54195




32.1k54195









asked Mar 22 at 4:22









Simsons

3931314




3931314




closed as off-topic by t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch Mar 23 at 14:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch Mar 23 at 14:42


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Raystafarian, Sam Onela, Dannnno, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.











  • Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
    – t3chb0t
    Mar 22 at 7:56










  • @t3chb0t, the C# code is the actual code, SQL table us just the column names
    – Simsons
    Mar 22 at 11:11










  • What is allPDUTags? Please show the code that creates it.
    – Gert Arnold
    Mar 22 at 16:28







  • 2




    I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
    – Hosch250
    Mar 22 at 20:27






  • 1




    Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
    – paparazzo
    Mar 23 at 11:56
















  • Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
    – t3chb0t
    Mar 22 at 7:56










  • @t3chb0t, the C# code is the actual code, SQL table us just the column names
    – Simsons
    Mar 22 at 11:11










  • What is allPDUTags? Please show the code that creates it.
    – Gert Arnold
    Mar 22 at 16:28







  • 2




    I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
    – Hosch250
    Mar 22 at 20:27






  • 1




    Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
    – paparazzo
    Mar 23 at 11:56















Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
– t3chb0t
Mar 22 at 7:56




Have you posted edited code? There are so many empty lines that it looks like it wasn't your real code.
– t3chb0t
Mar 22 at 7:56












@t3chb0t, the C# code is the actual code, SQL table us just the column names
– Simsons
Mar 22 at 11:11




@t3chb0t, the C# code is the actual code, SQL table us just the column names
– Simsons
Mar 22 at 11:11












What is allPDUTags? Please show the code that creates it.
– Gert Arnold
Mar 22 at 16:28





What is allPDUTags? Please show the code that creates it.
– Gert Arnold
Mar 22 at 16:28





2




2




I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
– Hosch250
Mar 22 at 20:27




I'd certainly use EF's capability to build a SQL script and run it directly against the DB. Then you can write a single, optimal query and offload everything to the DB, which should be able to do this in milliseconds. If it can't, add an index to a relevant column or two-- (probably Path and Name).
– Hosch250
Mar 22 at 20:27




1




1




Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
– paparazzo
Mar 23 at 11:56




Yes there is a way to improve speed. Use TSQL and do this as a set based operation.
– paparazzo
Mar 23 at 11:56










2 Answers
2






active

oldest

votes

















up vote
1
down vote













var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;


This line is effectively a join inside of your loop meaning that you are going to query the database for every tag in your list. So you have 40k extra queries, which seems likely to be the biggest cause of slowness.



I would focus on moving that join out of your loop. There are a couple of ways you can do that.



  1. Change the allPDUTags to be a the result of a join between Circuit and Tag.

  2. If you can drop out of entity framework writing SQL to update the rows in an efficient manner should be relatively simple given they are both in database tables. I am unsure of the DBMS you are using so I can't provide sample code for that.

  3. Quick and dirty would be to put the list of circuits in memory. Calling _context.Circuits.ToList() would put them all in memory and then compare against that list in your loop. Assumes that circuits is small enough to fit into memory. This option won't scale well and I wouldn't really recommend it.

I would look closely at why Circuit.Path and tag.Name need to be synced in the first place. There might be a historical reason or they might be in 2 different databases/systems but if they are in the same application I would strongly consider normalizing the database and or adding constraints to avoid having to sync in the first place.






share|improve this answer





















  • Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
    – Hosch250
    Mar 22 at 20:25











  • Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
    – Simsons
    Mar 23 at 3:40






  • 1




    @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
    – Gert Arnold
    Mar 23 at 7:02










  • @GertArnold so I was right about edited code...
    – t3chb0t
    Mar 23 at 7:22










  • @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
    – stoj
    Mar 23 at 15:30

















up vote
0
down vote













This is a TSQL approach. It should be pretty fast.



declare @Circuit table (id int identity primary key, name varchar(20));
declare @Tag table (id int identity primary key, path varchar(40), circuitID int);
insert into @tag (path) values ('asld_aslkjf_alskd'), ('asxx_askjf_alsyd_ lasdj');
insert into @Circuit (name) values ('asld'), ('asxx_askjf');

select * from @tag;

update t
set t.circuitID = c.id
from @tag t
join @Circuit c
on c.name = reverse(SUBSTRING(REVERSE(t.path), CHARINDEX('_', REVERSE(t.path), CHARINDEX('_', REVERSE(t.path)) + 1) + 1, 100))
where t.circuitID <> c.id or t.circuitID is null;

select * from @tag





share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;


    This line is effectively a join inside of your loop meaning that you are going to query the database for every tag in your list. So you have 40k extra queries, which seems likely to be the biggest cause of slowness.



    I would focus on moving that join out of your loop. There are a couple of ways you can do that.



    1. Change the allPDUTags to be a the result of a join between Circuit and Tag.

    2. If you can drop out of entity framework writing SQL to update the rows in an efficient manner should be relatively simple given they are both in database tables. I am unsure of the DBMS you are using so I can't provide sample code for that.

    3. Quick and dirty would be to put the list of circuits in memory. Calling _context.Circuits.ToList() would put them all in memory and then compare against that list in your loop. Assumes that circuits is small enough to fit into memory. This option won't scale well and I wouldn't really recommend it.

    I would look closely at why Circuit.Path and tag.Name need to be synced in the first place. There might be a historical reason or they might be in 2 different databases/systems but if they are in the same application I would strongly consider normalizing the database and or adding constraints to avoid having to sync in the first place.






    share|improve this answer





















    • Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
      – Hosch250
      Mar 22 at 20:25











    • Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
      – Simsons
      Mar 23 at 3:40






    • 1




      @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
      – Gert Arnold
      Mar 23 at 7:02










    • @GertArnold so I was right about edited code...
      – t3chb0t
      Mar 23 at 7:22










    • @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
      – stoj
      Mar 23 at 15:30














    up vote
    1
    down vote













    var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;


    This line is effectively a join inside of your loop meaning that you are going to query the database for every tag in your list. So you have 40k extra queries, which seems likely to be the biggest cause of slowness.



    I would focus on moving that join out of your loop. There are a couple of ways you can do that.



    1. Change the allPDUTags to be a the result of a join between Circuit and Tag.

    2. If you can drop out of entity framework writing SQL to update the rows in an efficient manner should be relatively simple given they are both in database tables. I am unsure of the DBMS you are using so I can't provide sample code for that.

    3. Quick and dirty would be to put the list of circuits in memory. Calling _context.Circuits.ToList() would put them all in memory and then compare against that list in your loop. Assumes that circuits is small enough to fit into memory. This option won't scale well and I wouldn't really recommend it.

    I would look closely at why Circuit.Path and tag.Name need to be synced in the first place. There might be a historical reason or they might be in 2 different databases/systems but if they are in the same application I would strongly consider normalizing the database and or adding constraints to avoid having to sync in the first place.






    share|improve this answer





















    • Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
      – Hosch250
      Mar 22 at 20:25











    • Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
      – Simsons
      Mar 23 at 3:40






    • 1




      @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
      – Gert Arnold
      Mar 23 at 7:02










    • @GertArnold so I was right about edited code...
      – t3chb0t
      Mar 23 at 7:22










    • @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
      – stoj
      Mar 23 at 15:30












    up vote
    1
    down vote










    up vote
    1
    down vote









    var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;


    This line is effectively a join inside of your loop meaning that you are going to query the database for every tag in your list. So you have 40k extra queries, which seems likely to be the biggest cause of slowness.



    I would focus on moving that join out of your loop. There are a couple of ways you can do that.



    1. Change the allPDUTags to be a the result of a join between Circuit and Tag.

    2. If you can drop out of entity framework writing SQL to update the rows in an efficient manner should be relatively simple given they are both in database tables. I am unsure of the DBMS you are using so I can't provide sample code for that.

    3. Quick and dirty would be to put the list of circuits in memory. Calling _context.Circuits.ToList() would put them all in memory and then compare against that list in your loop. Assumes that circuits is small enough to fit into memory. This option won't scale well and I wouldn't really recommend it.

    I would look closely at why Circuit.Path and tag.Name need to be synced in the first place. There might be a historical reason or they might be in 2 different databases/systems but if they are in the same application I would strongly consider normalizing the database and or adding constraints to avoid having to sync in the first place.






    share|improve this answer













    var circuitId = _context.Circuits.Where(c => c.Name == removedUnderScoreLastButOne).FirstOrDefault().Id;


    This line is effectively a join inside of your loop meaning that you are going to query the database for every tag in your list. So you have 40k extra queries, which seems likely to be the biggest cause of slowness.



    I would focus on moving that join out of your loop. There are a couple of ways you can do that.



    1. Change the allPDUTags to be a the result of a join between Circuit and Tag.

    2. If you can drop out of entity framework writing SQL to update the rows in an efficient manner should be relatively simple given they are both in database tables. I am unsure of the DBMS you are using so I can't provide sample code for that.

    3. Quick and dirty would be to put the list of circuits in memory. Calling _context.Circuits.ToList() would put them all in memory and then compare against that list in your loop. Assumes that circuits is small enough to fit into memory. This option won't scale well and I wouldn't really recommend it.

    I would look closely at why Circuit.Path and tag.Name need to be synced in the first place. There might be a historical reason or they might be in 2 different databases/systems but if they are in the same application I would strongly consider normalizing the database and or adding constraints to avoid having to sync in the first place.







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Mar 22 at 20:06









    stoj

    1112




    1112











    • Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
      – Hosch250
      Mar 22 at 20:25











    • Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
      – Simsons
      Mar 23 at 3:40






    • 1




      @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
      – Gert Arnold
      Mar 23 at 7:02










    • @GertArnold so I was right about edited code...
      – t3chb0t
      Mar 23 at 7:22










    • @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
      – stoj
      Mar 23 at 15:30
















    • Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
      – Hosch250
      Mar 22 at 20:25











    • Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
      – Simsons
      Mar 23 at 3:40






    • 1




      @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
      – Gert Arnold
      Mar 23 at 7:02










    • @GertArnold so I was right about edited code...
      – t3chb0t
      Mar 23 at 7:22










    • @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
      – stoj
      Mar 23 at 15:30















    Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
    – Hosch250
    Mar 22 at 20:25





    Actually, EF is smart enough to turn that into the following SQL query select top 1 from Foo f where f.Whatever = 'whatever'.
    – Hosch250
    Mar 22 at 20:25













    Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
    – Simsons
    Mar 23 at 3:40




    Yes, _context.Circuits takes around 5 mins amount of time and so does the savechanges method!
    – Simsons
    Mar 23 at 3:40




    1




    1




    @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
    – Gert Arnold
    Mar 23 at 7:02




    @Simsons Now you also talk about SaveChanges. You're not showing all code necessary to do any decent review. See also my comment on your question.
    – Gert Arnold
    Mar 23 at 7:02












    @GertArnold so I was right about edited code...
    – t3chb0t
    Mar 23 at 7:22




    @GertArnold so I was right about edited code...
    – t3chb0t
    Mar 23 at 7:22












    @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
    – stoj
    Mar 23 at 15:30




    @Hosch250 Yes but unless I am misunderstanding your comment and EF that command is being run ~40k times. Which of course is going to be slow. It's still effectively a join, just instead of doing a join on all of the data we do it one item at a time in a loop.
    – stoj
    Mar 23 at 15:30












    up vote
    0
    down vote













    This is a TSQL approach. It should be pretty fast.



    declare @Circuit table (id int identity primary key, name varchar(20));
    declare @Tag table (id int identity primary key, path varchar(40), circuitID int);
    insert into @tag (path) values ('asld_aslkjf_alskd'), ('asxx_askjf_alsyd_ lasdj');
    insert into @Circuit (name) values ('asld'), ('asxx_askjf');

    select * from @tag;

    update t
    set t.circuitID = c.id
    from @tag t
    join @Circuit c
    on c.name = reverse(SUBSTRING(REVERSE(t.path), CHARINDEX('_', REVERSE(t.path), CHARINDEX('_', REVERSE(t.path)) + 1) + 1, 100))
    where t.circuitID <> c.id or t.circuitID is null;

    select * from @tag





    share|improve this answer



























      up vote
      0
      down vote













      This is a TSQL approach. It should be pretty fast.



      declare @Circuit table (id int identity primary key, name varchar(20));
      declare @Tag table (id int identity primary key, path varchar(40), circuitID int);
      insert into @tag (path) values ('asld_aslkjf_alskd'), ('asxx_askjf_alsyd_ lasdj');
      insert into @Circuit (name) values ('asld'), ('asxx_askjf');

      select * from @tag;

      update t
      set t.circuitID = c.id
      from @tag t
      join @Circuit c
      on c.name = reverse(SUBSTRING(REVERSE(t.path), CHARINDEX('_', REVERSE(t.path), CHARINDEX('_', REVERSE(t.path)) + 1) + 1, 100))
      where t.circuitID <> c.id or t.circuitID is null;

      select * from @tag





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is a TSQL approach. It should be pretty fast.



        declare @Circuit table (id int identity primary key, name varchar(20));
        declare @Tag table (id int identity primary key, path varchar(40), circuitID int);
        insert into @tag (path) values ('asld_aslkjf_alskd'), ('asxx_askjf_alsyd_ lasdj');
        insert into @Circuit (name) values ('asld'), ('asxx_askjf');

        select * from @tag;

        update t
        set t.circuitID = c.id
        from @tag t
        join @Circuit c
        on c.name = reverse(SUBSTRING(REVERSE(t.path), CHARINDEX('_', REVERSE(t.path), CHARINDEX('_', REVERSE(t.path)) + 1) + 1, 100))
        where t.circuitID <> c.id or t.circuitID is null;

        select * from @tag





        share|improve this answer















        This is a TSQL approach. It should be pretty fast.



        declare @Circuit table (id int identity primary key, name varchar(20));
        declare @Tag table (id int identity primary key, path varchar(40), circuitID int);
        insert into @tag (path) values ('asld_aslkjf_alskd'), ('asxx_askjf_alsyd_ lasdj');
        insert into @Circuit (name) values ('asld'), ('asxx_askjf');

        select * from @tag;

        update t
        set t.circuitID = c.id
        from @tag t
        join @Circuit c
        on c.name = reverse(SUBSTRING(REVERSE(t.path), CHARINDEX('_', REVERSE(t.path), CHARINDEX('_', REVERSE(t.path)) + 1) + 1, 100))
        where t.circuitID <> c.id or t.circuitID is null;

        select * from @tag






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Mar 23 at 12:50


























        answered Mar 23 at 12:33









        paparazzo

        4,8131730




        4,8131730












            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation