7

I wrote a query to update entire table. How can I improve this query to take less time:

update page_densities set density = round(density - 0.001, 2)

Query returned successfully: 628391 rows affected, 1754179 ms (29 minutes) execution time.

EDIT: By setting work memory..

set work_mem = '500MB';
update page_densities set density = round(density - 0.001, 2)

Query returned successfully: 628391 rows affected, 731711 ms (12 minutes) execution time.

19
  • 1
    Any constraints involved (on density) that could be switched off temporarily? Did you try to execute the update in several steps? Commented May 12, 2015 at 11:14
  • 1
    @Trinimon: that is going to be a lot slower that using a single update statement. Commented May 12, 2015 at 11:28
  • 3
    @G.B: updating 628391 in 29 minutes is awfully slow. My guess is that your harddisk is too slow - maybe your database is located on a network drive? Or an USB drive? I have a relatively slow server that is running inside a VM - it takes about 8 seconds to update 5 million rows. Commented May 12, 2015 at 11:30
  • 1
    can we get an output of "\d page_densities" Commented May 12, 2015 at 11:43
  • 2
    Not sure if the same applies to PostgreSQL too, but in Oracle, this query runs significantly faster if you issue LOCK TABLE page_densities IN EXCLUSIVE MODE before the UPDATE statement. You might also consider: a) disabling all triggers on the table (if there are some); b) dropping all indexes and rebuilding them after the update Commented May 12, 2015 at 12:35

1 Answer 1

1

Assuming density is not an index, you may be able to improve performance with a different fillfactor. See this question/answer or the PostgreSQL docs for more info:

http://www.postgresql.org/docs/9.4/static/sql-createtable.html

Slow simple update query on PostgreSQL database with 3 million rows

Although you cannot modify a table's fillfactor, you can create a new table with a different fill factor and copy the data over. Here is some sample code.

--create a new table with a different fill factor
CREATE TABLE page_densities_new
(
 ...some fields here
)
WITH (
  FILLFACTOR=70
);

--copy all of the records into the new table
insert into page_densities_new select * from page_densities;

--rename the original/old table
ALTER TABLE page_densities RENAME TO page_densities_old;

--rename the new table
ALTER TABLE page_densities_new RENAME TO page_densities;

After this you have a table with the same name and data as the original, but it has a different fill factor. I set it to 70, but it can be any value 10 to 100. (100 is the default)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.