+ Reply to Thread
Results 1 to 8 of 8

Thread: insert thousands of records

  1. #1
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    insert thousands of records

    Hi
    Sorry for the confusing title

    I have this table structure

    tbluser
    -user_id.....

    tblmessages
    -message_id
    -receiver_id
    -message....

    Now i have to send mass messages to these users
    So first question is if i have say 30K users, can i insert so many records in 1 go or will face a problem??

    Second question is query related.
    Can i get this done in 1 query, i mean something like this
    Code:
    insert into tblmessages values (1, (select User_Id From idate_tblusermaster order by User_Id), 'test message')
    
    Although this query returns error
    Subquery returns more than 1 row
    OR will i have to first get the user id, then insert using it??

    VB.NET 2005
    MySql 5.0

    Thanx

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Regarding the second part of your post, the following query should work:
    Code:
    insert into tblmessages 
     Select 1, User_ID, 'test message'
       From idate_tblusermaster
    
    The ORDER BY is not necessary order is established by the index on the table or when the data is retrieved.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    IMO, you should do this in batches. I believe MySQL's LIMIT could help you with this.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  4. #4
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by Wolffy View Post
    Regarding the second part of your post, the following query should work:
    Code:
    insert into tblmessages 
     Select 1, User_ID, 'test message'
       From idate_tblusermaster
    
    The ORDER BY is not necessary order is established by the index on the table or when the data is retrieved.
    Thanx Wolffy, it works

    Quote Originally Posted by jmurrayhead View Post
    IMO, you should do this in batches. I believe MySQL's LIMIT could help you with this.
    ya, i was also thinking that doing all at one go might create problem.

    I'll see how i get it done in batches.......... thanx J

  5. #5
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by jmurrayhead View Post
    IMO, you should do this in batches. I believe MySQL's LIMIT could help you with this.
    Well.......... i cant think of a good way to do it J

    Can you suggest how to do it??

  6. #6
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by micky View Post
    Well.......... i cant think of a good way to do it J

    Can you suggest how to do it??
    Ok, i managed to do it....... inserting 1000 records at a time.

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Share the solution, M
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  8. #8
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by jmurrayhead View Post
    Share the solution, M
    yes J, i was out of time yesterday..... so didnt post my code.

    I added 2 things in .aspx part, first a text box to save my limit count for records to be shown
    Code:
    <asp:TextBox Visible="false" runat="server" ID="hdPageCount">
    
    Then i had 2 submit buttons, one for 1st 1000 records, and second for next 1000 records.
    Code:
    <asp:button id="btnSubmit" runat="server" Width="125px" Text="First 1000 >>" TabIndex="2"></asp:button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:button id="btnNext" runat="server" Width="125px" Text="Next 1000 >>" TabIndex="3" Enabled="false"></asp:button>
    
    Now i have a textbox to enter message to be saved in db. I needed this hidden textbox as i wasnt redirecting to this page after each insertion as then i would loose this text and i dont want the text in querystring.

    Next is .vb part
    Code:
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            SendMessage(Trim(txtMessage.Text), 0)
        End If
    End Sub
    
    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If Page.IsValid Then
            SendMessage(Trim(txtMessage.Text), hdPageCount.Text)
        End If
    End Sub
    
        Function SendMessage(ByVal message As String, ByVal limit As Integer)
            Dim nPage As Integer = 1000
            Dim strSQL As String = "Insert Into tblmessagemaster (Receiver_Id, Message, Msg_DateTime) " & _
                "Select User_Id, @Message, now() From tblusermaster Limit @Limit, @Page"
    
            Using Con As New MySqlConnection(sConn)
                'GET TOTAL USERS
                Dim cmdTotal As New MySqlCommand("Select Count(User_Id) From tblusermaster", Con)
    
                Con.Open()
                Dim nTotal As Integer = cmdTotal.ExecuteScalar
    
                If nTotal > limit Then
                    Dim cmd As New MySqlCommand(strSQL, Con)
                    cmd.Parameters.AddWithValue("@Message", message)
                    cmd.Parameters.AddWithValue("@Limit", limit)
                    cmd.Parameters.AddWithValue("@Page", nPage)
    
                    cmd.ExecuteNonQuery()
                End If
                Con.Close()
    
                If limit = 0 Then
                    'FIRST 1000
                    If nTotal - nPage > 0 Then
                        'MORE RECORDS
                        hdPageCount.Text = nPage
                        btnNext.Enabled = True
                        btnSubmit.Enabled = False
                        lblError.Text = "&nbsp;Inserted " & nPage & " records out of " & nTotal & " records."
    
                    Else
                        'NO MORE RECORDS
                        Response.Redirect("Message.aspx")
                    End If
    
                Else
                    'NEXT 1000
                    Dim nNext As Integer = nPage + limit
    
                    If nTotal - nNext > 0 Then
                        'MORE RECORDS
                        hdPageCount.Text = nNext
                        btnNext.Enabled = True
                        btnSubmit.Enabled = False
                        lblError.Text = "&nbsp;Inserted " & nNext & " records out of " & nTotal & " records."
    
                    Else
                        'NO MORE RECORDS
                        Response.Redirect("Message.aspx")
                    End If
                End If
            End Using
        End Function
    

+ Reply to Thread

Similar Threads

  1. INSERT in 2 different tables?
    By Centurion in forum SQL Development
    Replies: 15
    Last Post: November 24th, 2009, 11:57 AM
  2. Show/hide insert
    By dtz in forum JavaScript Programming
    Replies: 5
    Last Post: March 23rd, 2009, 12:38 PM
  3. Help with handling sql insert error
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: August 29th, 2008, 02:20 PM
  4. can not insert duplicate key
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: August 20th, 2008, 10:27 AM
  5. format currency to display by thousands
    By Rebelle in forum ASP Development
    Replies: 2
    Last Post: April 29th, 2008, 11:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO