2

I have a script always verifies that the row exists or not, depending on adjusts to the event.

For example:

if(exists($row)){
    // update
}else{
    // insert
}

Insert query:

INSERT INTO table (name, value) VALUES 
    ('...', '1'), 
    ('...', '2'), 
    ('...', '3'), 
    ('...', '4'), 
    ('...', '5'), 
    //...100x

UPDATE QUERY:

 UPDATE table
     SET value = CASE uuid
         WHEN 'x' THEN 1
         WHEN 'y' THEN 2
     END,
     SET value1 = CASE uuid
         WHEN 'x' THEN 1
         WHEN 'y' THEN 2
     END
     WHERE uuid IN ('x','y')

Data is quite a lot, and verify that the record exists long enough. Because performs necessary 500x or more.

Exists method:

SELECT id FROM table WHERE uuid=Y;

There is a possibility somehow speed up the script? It is possible to verify existing data added directly to the query?

Can I have just one query CREATE + UPDATE + CHECK EXISTING?

Thanks for help!

Updating and adding data with verification : 27-40 SEC
Updating and adding data WITHOUT verification: 1-5 SEC

EDIT: I'm getting data from another server like json example script:

$data = json_decode($_POST["data"]);
foreach($data as value){
    // value is an array
    if(exists($value["uuid"])){
         $this->appendUpdate($value); // building the update query
    }else{
         $this->appendInsert($value); // building the insert query
    }
}

    $this->insert();  // for example: 30 inserts in 1 query
    $this->update();  // for example: 500 updates in 1 query

In this table is just 300-2000 rows (I do not understand why it takes so long.)

EDIT2:

This is probably my solution:

INSERT INTO test (uuid, value) VALUES
    ('aaa', 1),
    ('bbb', 2),
    ('ccc', 3)
    ON DUPLICATE KEY UPDATE
    value = (
        IF(uuid='aaa', 4, IF(uuid='bbb', 5, IF(uuid='ccc', 6, value)))
    ); 

I'm going to try to run the brisk server and then touch.

4
  • make table view using select and then insert if you can. Are you taking insert data from the database itself or you have local data totally? Commented Sep 2, 2016 at 23:22
  • insert ignore ... on duplicate update stuff? Commented Sep 2, 2016 at 23:22
  • ask was updated @Motsim Commented Sep 2, 2016 at 23:29
  • I would use 1) transactions. 2) a prepared query outside the loop. I suspect it will be quite fast. Especially with the insert .. on duplicate ... query. It is easy easy to code as well. Commented Sep 3, 2016 at 0:04

2 Answers 2

1

Make sure the column uuid has a unique index and you can do the following query

INSERT INTO table (a,b) VALUES (1,2)
   ON DUPLICATE KEY UPDATE b=4;
Sign up to request clarification or add additional context in comments.

6 Comments

I can't use UUID as primary key, cause the primary key is 'ID' - UUID is just primary key in another server - not mine .
A unique index is something else than a primary key. they are pretty similar but you can have multiple unique indexes per table, so that shouldn't be a problem.
aaaaa hmmm it's probably working now (on differant test table) I'll try to make it work in the real script. @MarcHoH
But, I need just one query, not 1000x inserts like this it will be the same slow effect like verification probably. Is there way to make it on one query? Like this? gist.github.com/anonymous/bdbe2da59434adc798d0ac28a513b4ea @MarcHoH
It is possible to use multiple inserts, like so: insert into table (a,b) values (1,2), (2,4) on duplicate key update b=5 . but just like the normal update statements you only can have one on duplicate key update.
|
0

Something like this:

INSERT INTO table(name,value)
SELECT table2name as name, table2value as value FROM table2 
WHERE uuid IS NULL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.