UPDATE only in the last inserted rows

Asked

Viewed 49 times

1

I’m importing some Excel spreadsheets via ETL into a relational database. The problem is that in order for me to achieve the relationship of the tables, I am using queries for the insertion of foreign keys. However, as the bank grows, these consultations are taking an absurdly long time. Is there any way to update only the last records that were included? For example:

UPDATE estrutura q
SET q.id_horizonte = (SELECT h.id_horizonte FROM horizonte h 
WHERE q.observacao = h.observacao AND q.nome_horizonte = h.nome_horizonte AND q.amostra_codigo = h.amostra_codigo)
LIMIT 50

It’s not working that way, but my question is just this: is there any way that this UPDATE is not applied in all the records of the table and only in the last 50 included? This would help a lot with the performance of the queries.

2 answers

2

Try

UPDATE estrutura q
SET q.id_horizonte = 
(SELECT h.id_horizonte 
FROM horizonte h
WHERE q.observacao = h.observacao 
AND q.nome_horizonte = h.nome_horizonte 
AND q.amostra_codigo = h.amostra_codigo) 
WHERE q.id_horizonte  
IN 
(SELECT id_horizonte 
FROM estrutura 
ORDER BY id_horizonte DESC Limit 50)
  • Hi Mauro, I got the following error: #1235 - This Mysql version does not support 'LIMIT & IN/ALL/ANY/SOME subquery yet'

0

I managed to solve by putting a simple condition in my UPDATE. In my case it worked because the fields I want to update are always null.

UPDATE estrutura q
SET q.id_horizonte = (SELECT h.id_horizonte FROM horizonte h 
WHERE q.observacao = h.observacao AND q.nome_horizonte = h.nome_horizonte AND q.amostra_codigo = h.amostra_codigo)
WHERE id_horizonte IS NULL

Browser other questions tagged

You are not signed in. Login or sign up in order to post.