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. SQL Server. Insert into table whiles querying

SQL Server. Insert into table whiles querying

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
12 Posts 8 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.
  • E Eli Nurman

    Hi. I have a stored procedure that queries a table that takes about 35 seconds to return the results, meanwhile, from other stored procedures, i cannot insert new rows into that table, how is this possible to do? (even if that mean that on the large results the new records will not appear)? Thanks.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #3

    Is your SP speed acceptable? Or should it really execute much faster when done properly? Did you look into your indexing scheme? :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    E 1 Reply Last reply
    0
    • G Gerben Jongerius

      Depends on what you are trying to achieve. From what I hear your select statement is locking most of the rows in the table, which could cause a full table lock (read level). That would mean your insert would fail as it cannot get an exclusive lock on the table. The other way around is also possible as you have experienced. If the insert starts first the select will fail. You could potentially fix this by hinting to the SQL Server to use no locking on the select (add with nolock after the from part from the select), but this will cause you to get dirty data and uncommited data.

      E Offline
      E Offline
      Eli Nurman
      wrote on last edited by
      #4

      Hi, Thanks for your answer, i have tried the (WITH NOLOCK) on the select query but it still takes time to insert the new rows when running the select. it will wait until the select finishes.

      G 1 Reply Last reply
      0
      • L Luc Pattyn

        Is your SP speed acceptable? Or should it really execute much faster when done properly? Did you look into your indexing scheme? :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        E Offline
        E Offline
        Eli Nurman
        wrote on last edited by
        #5

        The SP are fine and tabled are well indexed, just the search results are very complicated calculations running on millions of records , and the problem is that when running the select for client to see in application, no inserts are allowed.

        L 1 Reply Last reply
        0
        • E Eli Nurman

          The SP are fine and tabled are well indexed, just the search results are very complicated calculations running on millions of records , and the problem is that when running the select for client to see in application, no inserts are allowed.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          I guess Gerben's answer applies then. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          • E Eli Nurman

            Hi. I have a stored procedure that queries a table that takes about 35 seconds to return the results, meanwhile, from other stored procedures, i cannot insert new rows into that table, how is this possible to do? (even if that mean that on the large results the new records will not appear)? Thanks.

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

            I would certainly look into ways of speeding up the query. Perhaps copy the relavent data to another table and do the calculations there.

            D 1 Reply Last reply
            0
            • E Eli Nurman

              Hi. I have a stored procedure that queries a table that takes about 35 seconds to return the results, meanwhile, from other stored procedures, i cannot insert new rows into that table, how is this possible to do? (even if that mean that on the large results the new records will not appear)? Thanks.

              S Offline
              S Offline
              SilimSayo
              wrote on last edited by
              #8

              Revisit you code..... Are you using cursors? Also, it may be better to get the raw data into an intermediary table, then select from that table and perform computations, then insert into the destination table. Of course you should always clear the intermediate table before you start putting data into it.

              1 Reply Last reply
              0
              • E Eli Nurman

                Hi, Thanks for your answer, i have tried the (WITH NOLOCK) on the select query but it still takes time to insert the new rows when running the select. it will wait until the select finishes.

                G Offline
                G Offline
                Gerben Jongerius
                wrote on last edited by
                #9

                Like I stated the nolock is a hint given to the database, and the database server might choose to ignore it. From what I know, for at least for MS SQL, is that the nolock should solve the issue. But you could try to combine it with a ROWLOCK or PAGELOCK instruction for the insert statement, see locking hints for more on this. Keep in mind that large sets of indexes could also cause the locking you are experiencing, as an insert will update the index and could trigger a re-index.

                1 Reply Last reply
                0
                • E Eli Nurman

                  Hi. I have a stored procedure that queries a table that takes about 35 seconds to return the results, meanwhile, from other stored procedures, i cannot insert new rows into that table, how is this possible to do? (even if that mean that on the large results the new records will not appear)? Thanks.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Your SELECT query is locking the whole table that is forcing the database engine to dishonor exclusive lock requests by INSERT statements. This could happen if the query is fetching most of the rows in the tables. 1. Revisit your SELECT query and make sure your select only those rows/columns required. 2. Index your table(s).

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    I would certainly look into ways of speeding up the query. Perhaps copy the relavent data to another table and do the calculations there.

                    D Offline
                    D Offline
                    David Mujica
                    wrote on last edited by
                    #11

                    I agree that you might want to build a "reporting" table that is refreshed nightly, hourly, whatever and have the users query against that table. Sometimes the data doesn't need to be up to the minute ... just make sure the end user knows that the data may be somewhat stale. I've used this method for a few reports and the first user who initiates the report takes the hit and creates the reporting view for the day. everyone request for the report goes against that dataset; each day a new dataset is created only if someone requests it. Each time a dataset is requested, all previous datasets are deleted. Just a thought. :rose: Good luck

                    1 Reply Last reply
                    0
                    • E Eli Nurman

                      Hi. I have a stored procedure that queries a table that takes about 35 seconds to return the results, meanwhile, from other stored procedures, i cannot insert new rows into that table, how is this possible to do? (even if that mean that on the large results the new records will not appear)? Thanks.

                      S Offline
                      S Offline
                      S Douglas
                      wrote on last edited by
                      #12

                      Eli Nurman wrote:

                      i cannot insert new rows into that table, how is this possible to do

                      How are you trying to update (insert) the table? By default SSIS for example trys to lock the table before doing any data changes, that behavior can be changed.


                      Common sense is admitting there is cause and effect and that you can exert some control over what you understand.

                      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