Robert DiFalco wrote:>
> UPDATE contacts SET user_id = u.id
> FROM my_users u
> JOIN phone_numbers pn ON u.phone_significant = pn.significant
> WHERE contacts.owner_id = 7 AND contacts.user_id IS NULL
> AND contacts.id = pn.ref_contact_id;
>
> UPDATE contacts SET user_id = u.id
>
What does EXPLAIN show?
What proportion of contacts have owner_id=7 and user_id is null?
If it's a large number of contacts, I'd try the following:
create temporary table tusers as
select coalesce(p.ref_contact_id,e.ref_contact_id) as id, u.id as user_id
from my_users u
left join phone_numb