+ Reply to Thread
Results 1 to 5 of 5

Thread: How to make inserting a million rows into MySQL faster/more efficient?

  1. #1
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    How to make inserting a million rows into MySQL faster/more efficient?

    PHP Code:
    function createChancePool($organizationID) {
    $sql "SELECT numChance FROM organizations WHERE id='$organizationID'";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {
      
    $numChances $row['numChance'];
    }
    $table "chancePool_$organizationID";
      
    $sql "DROP TABLE IF EXISTS `$table`";
      
    mysql_query($sql);
      
    $sql "CREATE TABLE `$table` (
              `chanceID` int(4) NOT NULL,
              `active` tinyint(1) NOT NULL,
              `member` int(7) NOT NULL,
              PRIMARY KEY  (`chanceID`)
           )"
    ;
      if(!
    mysql_query($sql)) {
            
    mysql_query("DROP TABLE IF EXISTS `$table`");
            return 
    false;
      }

      
    $i "0";
      while (
    $i $numChances) {
        
    $sql "INSERT INTO $table (chanceID, active, member) VALUES ('$i', '0', '0')";
          if(!
    mysql_query($sql)) {
            
    mysql_query("DROP TABLE IF EXISTS `$table`");
            return 
    false;
          }
        
    $i++;
      }
    $sql2 "UPDATE organizations SET chancepool='1' WHERE id=$organizationID";  
    mysql_query($sql2);

    return 
    true;

    I have it set so the max is 7 digits, which is more than 1million, so i need to fix that. but 1 million is the max

    and I have the script running right now, and its so far taking 5 minutes and im up to: 437,311 rows

    and it stops there.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: How to make inserting a million rows into MySQL faster/more efficient?

    Ideas:
    1.
    a. Make 'chanceID' auto increment
    b. Set defaults for 'active' and 'member' to 0
    c. $sql = "INSERT INTO $table VALUES (NULL)";

    2. Alternatively, build the query ten (or more) values at a time......

    Code:
       for ( $i=0; $i + 10 < $numChances; $i = $i + 10 ) {
    
           $sql = "INSERT INTO $table (chanceID, active, member) VALUES ('$i', '0', '0')";
    
           for( $j =$i + 1 ; $j < $i + 10; $j++ ){
    
               $sql .= ",('$j', '0', '0' )" ;
           }
           if(!mysql_query($sql)) {
               mysql_query("DROP TABLE IF EXISTS `$table`");
              return false;
          }
      }
      
    #  might be off by one, doing this quickly
    # do a dry run to 107 or so an print out $sql to make sure it is right

    Or combine the two.
    Then you just have to do the last $numChances % 10 .
    Nothing is always absolutely so.

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: How to make inserting a million rows into MySQL faster/more efficient?

    Quote Originally Posted by descalzo View Post
    Ideas:
    1.
    a. Make 'chanceID' auto increment
    b. Set defaults for 'active' and 'member' to 0
    c. $sql = "INSERT INTO $table VALUES (NULL)";

    2. Alternatively, build the query ten (or more) values at a time......

    Code:
       for ( $i=0; $i + 10 < $numChances; $i = $i + 10 ) {
    
           $sql = "INSERT INTO $table (chanceID, active, member) VALUES ('$i', '0', '0')";
    
           for( $j =$i + 1 ; $j < $i + 10; $j++ ){
    
               $sql .= ",('$j', '0', '0' )" ;
           }
           if(!mysql_query($sql)) {
               mysql_query("DROP TABLE IF EXISTS `$table`");
              return false;
          }
      }
      
    #  might be off by one, doing this quickly
    # do a dry run to 107 or so an print out $sql to make sure it is right
    Or combine the two.
    Then you just have to do the last $numChances % 10 .
    wouldn't your second method be more slower, since you have to run it though multiple loops. and what happens if an error happens midway, and it drops the table, wouldn't you lose all the time/hard work?

    but i will try the first one

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: How to make inserting a million rows into MySQL faster/more efficient?

    Quote Originally Posted by diabolo View Post
    and what happens if an error happens midway, and it drops the table, wouldn't you lose all the time/hard work?
    The error code was copied from your code.

    Combining the two methods and removing the inner loop,
    Code:
     
       $leftover = $numChances % 10 ;
       $byTen = $numChances - $leftover ;
       for ( $i=0; $i + 10 < $byTen ; $i = $i + 10 ) {
    
           $sql = "INSERT INTO $table (chanceID) VALUES (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL)";
    
           mysql_query($sql);  
       }
     
      # code to do $leftover more entries
    Another idea is to lock the table before the inserts and then unlock it afterwards.
    Last edited by descalzo; 08-19-2009 at 03:27 PM.
    Nothing is always absolutely so.

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: How to make inserting a million rows into MySQL faster/more efficient?

    You said there could be up to a million rows, then to speed up the inserts, extend descalzo's method.

    Start with % 100.
    Take the leftovers and do % 10.
    Finish up by doing the insert one by one.

    This method will put more of the stress on the server, but the queries should be faster.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Unexpected error when trying to make MySQL connection
    By fotimember in forum Free Hosting
    Replies: 6
    Last Post: 09-23-2008, 10:56 AM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. MySQL Issues Here
    By Corey in forum Service Alerts
    Replies: 304
    Last Post: 01-06-2008, 09:10 PM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers