How can I design this job stable and optimised

Discussion in 'PHP' started by puya4ever, Jan 2, 2011.

  1. #1
    Hi to everyone. I need some advices.

    I need to build an application with PHP and I'm at the next part:

    I have an array with data and I need to check if the ID from this array is already inserted in my database:
    - If it's already inserted in DB, I need to get the unique ID of row
    - If it is not inserted in DB, I need to insert all data of array and get the new unique ID of inserted row.

    I need to do this job optimized as possible! The table with content where this array will be inserted may have +500.000 rows and I must do this job for 20 arrays, not one (these arrays are data got from a feed source)

    So, using a foreach loop my pseudocode will be something like:

    foreach($feed_array as $feed_id){
    -check if $feed_id already is in DB
    - TRUE: get the ID of the row where $feed_id is in
    - FALSE: insert into db some data (included $feed_id);
    - get the ID of the row where $feed_id is in
    } end foreach loop;

    So this is what I want to do optimized as possible.

    Thanks you.
     
    puya4ever, Jan 2, 2011 IP
  2. tprinty

    tprinty Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    //Assumes the rowid is the primary key of the table

    foreach ($idarray as $id){
    $result = mysql_query("SELECT * from feeds WHERE feedid=$id");
    if (mysql_num_rows ($result ) > 0){
    $row = mysql_fetch_assoc($result);
    $rowid = $row['rowid'];
    }else{
    mysql_query("INSERT into table() values()");
    $rowid = $row['rowid'];
    $rowid = mysql_insert_id();
    }
    }
     
    tprinty, Jan 2, 2011 IP