Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Threading and insert/update issues.

Threading and insert/update issues.

Scheduled Pinned Locked Moved Database
questiondatabaseannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    I have a database that receives weather information. The main table is (somewhat simplified) LocationID int, Hour int, HiTemp Float, LoTemp Float, TimedTemp Float Temperature data is first written to a holding table - LocationID int, Hour int, whichVariable int, value float. Then it is merged into the main table (using a MERGE statement) All this works fine when the temperature data written to the holding table goes in one variable at a time, executes the MERGE, loads the next variable, etc. If I have multiple programs adding multiple locations and different variables all at the same time, the system deadlocks. I could force one instance of the outside program inserting the data to use a mutex to guarantee undisturbed calls to MERGE but it is quite possible we will have multiple instances of the outside program running. What is the best approach to take to allow merging of the data?

    M P G 3 Replies Last reply
    0
    • M mjackson11

      I have a database that receives weather information. The main table is (somewhat simplified) LocationID int, Hour int, HiTemp Float, LoTemp Float, TimedTemp Float Temperature data is first written to a holding table - LocationID int, Hour int, whichVariable int, value float. Then it is merged into the main table (using a MERGE statement) All this works fine when the temperature data written to the holding table goes in one variable at a time, executes the MERGE, loads the next variable, etc. If I have multiple programs adding multiple locations and different variables all at the same time, the system deadlocks. I could force one instance of the outside program inserting the data to use a mutex to guarantee undisturbed calls to MERGE but it is quite possible we will have multiple instances of the outside program running. What is the best approach to take to allow merging of the data?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Why are you using merge instead of an insert?

      Never underestimate the power of human stupidity RAH

      M 1 Reply Last reply
      0
      • M Mycroft Holmes

        Why are you using merge instead of an insert?

        Never underestimate the power of human stupidity RAH

        M Offline
        M Offline
        mjackson11
        wrote on last edited by
        #3

        The routine uses the MERGE statement to determine if a record for the particular observation exists. If it does not, it inserts a new record. If a record does exist, it updates the appropriate field. Problem comes in that there are three raw import records for the particular observation. So the first merge may try to insert something for a high temperature while at the same time the system is trying to insert for the low temperature. I can use a cursor to cycle through the imported data row by row but want to avoid the speed hit. I also can't change the main table to hold a record for each individual type of temperature because of the huge number of records (~17 million per observation cycle)

        1 Reply Last reply
        0
        • M mjackson11

          I have a database that receives weather information. The main table is (somewhat simplified) LocationID int, Hour int, HiTemp Float, LoTemp Float, TimedTemp Float Temperature data is first written to a holding table - LocationID int, Hour int, whichVariable int, value float. Then it is merged into the main table (using a MERGE statement) All this works fine when the temperature data written to the holding table goes in one variable at a time, executes the MERGE, loads the next variable, etc. If I have multiple programs adding multiple locations and different variables all at the same time, the system deadlocks. I could force one instance of the outside program inserting the data to use a mutex to guarantee undisturbed calls to MERGE but it is quite possible we will have multiple instances of the outside program running. What is the best approach to take to allow merging of the data?

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Have only one thread perform the MERGE portion?

          1 Reply Last reply
          0
          • M mjackson11

            I have a database that receives weather information. The main table is (somewhat simplified) LocationID int, Hour int, HiTemp Float, LoTemp Float, TimedTemp Float Temperature data is first written to a holding table - LocationID int, Hour int, whichVariable int, value float. Then it is merged into the main table (using a MERGE statement) All this works fine when the temperature data written to the holding table goes in one variable at a time, executes the MERGE, loads the next variable, etc. If I have multiple programs adding multiple locations and different variables all at the same time, the system deadlocks. I could force one instance of the outside program inserting the data to use a mutex to guarantee undisturbed calls to MERGE but it is quite possible we will have multiple instances of the outside program running. What is the best approach to take to allow merging of the data?

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #5

            Here's an idea: Set up a table that is a queue table. Just insert all the data into this table - no merge needed. Then you have one process that reads from this table performing merges and deleting the row from the queue once the merge has been performed. As you are only performing inserts then deletes on the queue I can't see a deadlock occurring on the queue - as the process will be issuing one merge at a time you should avoids deadlocks on your merge table too.

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            M 1 Reply Last reply
            0
            • G GuyThiebaut

              Here's an idea: Set up a table that is a queue table. Just insert all the data into this table - no merge needed. Then you have one process that reads from this table performing merges and deleting the row from the queue once the merge has been performed. As you are only performing inserts then deletes on the queue I can't see a deadlock occurring on the queue - as the process will be issuing one merge at a time you should avoids deadlocks on your merge table too.

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              M Offline
              M Offline
              mjackson11
              wrote on last edited by
              #6

              I used a mutex to control the calls to the MERGE sections and it is running. If performance becomes and issue, I will split the program in two so a multi threaded section pulls data and inserts to a temporary table and a single threaded program calls for merging the data.

              G 1 Reply Last reply
              0
              • M mjackson11

                I used a mutex to control the calls to the MERGE sections and it is running. If performance becomes and issue, I will split the program in two so a multi threaded section pulls data and inserts to a temporary table and a single threaded program calls for merging the data.

                G Offline
                G Offline
                GuyThiebaut
                wrote on last edited by
                #7

                Sounds good - I always revert to the old carving code in stone method when all else fails so I hope the mutex works:thumbsup:

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups