Insert Performance

Discussion in 'MySQL' started by deriklogov, Aug 30, 2009.

  1. #1
    Which of those 2 models are faster ? and how much faster ?


    1)
    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3);
    INSERT INTO tbl_name (a,b,c) VALUES(4,5,6);
    INSERT INTO tbl_name (a,b,c) VALUES(7,8,9);

    2)
    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);


    and how bid multiple query (2) could be in size ?
     
    deriklogov, Aug 30, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    of course, the 2nd variant
    also add for even faster

    LOCK TABLES tbl_name WRITE;
    INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    UNLOCK TABLES;
     
    crivion, Aug 30, 2009 IP
  3. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #3
    but the thing is , I specially switch to innodb so the table is not going to lock

    another issue I found out - how I can use :

    I am using mysql_insert_id(); to get id from single insert, but if I will do multiple insert how does its going to work ?
     
    deriklogov, Aug 30, 2009 IP
  4. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #4
    hmmm good question
    the only thing which comes in my mind is doing a select
    SELECT id
    FROM tbl_name
    WHERE name IN ('value1','v2','vx')
     
    crivion, Aug 30, 2009 IP
  5. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #5
    well select will kill all performance which I will save with multiple query insert
     
    deriklogov, Aug 30, 2009 IP
  6. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #6
    well not sure, wtb trying to test the execution time
     
    crivion, Aug 30, 2009 IP
  7. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #7
    how I can play around with it to get most performance but leave mysql_insert_id
     
    deriklogov, Aug 30, 2009 IP
  8. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #8
    What do you think about joining 2 tables and make multiple insert ? is that going to be faster ?
     
    deriklogov, Aug 30, 2009 IP
  9. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #9
    I think it wont be too much different
     
    crivion, Aug 30, 2009 IP