问题
I want to copy one table's column to another table's column with where condition that both employee id should be equal, more than 300k records.
below is my query
UPDATE emp SET emp.address = (
SELECT emp_address
FROM address
WHERE emp.emp_id=address.emp_id LIMIT 1
);
I have executed this query since last 1 hour but still process is going on, I have executed this query on "DB Browser for SQLite".
Am I making any mistake? or there is problem with sqlite browser?
回答1:
For this query:
UPDATE emp
SET emp.address = (SELECT a.emp_address
FROM address a
WHERE e.emp_id = a.emp_id
LIMIT 1
);
You want an index on address(emp_id, emp_address)
.
An index on emp(emp_id)
should not be used by this query and is not useful. If you have an index on emp(address)
, you might want to drop it and recreate it after the update.
All that said, you are updating all records in the table. This is expensive.
It is often faster to recreate the table. Something like this:
create table temp_emp as
select . . . , -- all columns except address
(select a.emp_address
from address a
where e.emp_id = a.emp_id
limit 1
) as address
from emp;
truncate table emp; -- backup the data first!
insert into emp ( . . . ) -- list the columns here
select . . .
from temp_temp;
You can create the temporary table and see how long that takes (with the above mentioned index). The truncate and insert
should not take very long. But do backup the data first!
来源:https://stackoverflow.com/questions/59064755/copy-column-from-one-table-to-another-table-but-it-hangs-db-browser-for-sqlite