Copy column from one table to another table but it hangs DB Browser for SQLite

给你一囗甜甜゛ 提交于 2020-01-16 02:26:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!