View Single Post

   
  #4 (permalink)  
Old 02-28-2008, 10:09 AM
rob4you
 
Posts: n/a
Default Re: insert if not exist and delete old rows

> UPDATE `simple_table` SET `check` = 0;
> INSERT INTO `simple_table` (`id,`description`,`check`) VALUES
> (1,'FRED',1)
> ON DUPLICATE KEY SET `check` = 1;
> DELETE FROM `simple_table` WHERE `check` = 0


Thank you for your reply.

On an italian ng they've suggested me the following solution:

CREATE TEMPORARY TABLE `temp` (`id` int primary key, `descr` text);

INSERT INTO `temp` (`id,`descr`) VALUES (1,'FRED');
INSERT INTO `temp` (`id,`descr`) VALUES (2,'BOB');
-- ...

TRUNCATE `simple_table`;
INSERT INTO `simple_table` SELECT * FROM `temp`;

So according to them the solution does not consider any "check" field.
They told me this is better because in your solution MySQL has to
perform twice a full-scan (both with the UPDATE and with the DELETE),
and also a check on the INSERT.

What do you think about it?

--
rob4you


Reply With Quote