lundi 16 février 2015

Change all dup-references to references to MAX(id)

This is a question about sqlite.


I want to remove all duplicate entries from customers (keeping the MAX(id) entry). But before that, I want to change all references in orders to the corresponding MAX(id) value.


This is what I think does the right thing:



UPDATE orders
SET cid = (SELECT MAX(c.id)
FROM customers c
JOIN (SELECT name, id
FROM customers
WHERE id = orders.cid) q
ON c.name = q.name
GROUP BY c.name);

DELETE FROM customers
WHERE id NOT IN (SELECT MAX(id)
FROM customers
GROUP BY name);


But especially the first command is extremely slow already with some 10,000 customers. Is there a faster way to do this?


Aucun commentaire:

Enregistrer un commentaire