1

I have query to modify 2 million over records in postgres.
The problem is that one of the field is of type VARCHAR, and I need to do some mathematical operations on it. Right now I'm casting the field to numeric like this :-

CAST(attribute as numeric)

The whole query takes approximately 4 hours to run.
I am looking at ways to fasten then execution time. Are the any way I can change the field type from varchar to numeric first before I execute the query? I can't use alter table alter column type to do this.

2
  • 4
    You could try staging the data into a TEMPORARY table: SELECT ... INTO TEMPORARY tbl FROM ..., and then updating back to the original table. Commented Apr 7, 2011 at 4:21
  • 1
    Are you sure the CAST() is slowing down your query? Can you show us the full query and the execution plan? Commented Apr 7, 2011 at 6:38

1 Answer 1

1

If you can't block the table for reading for such long time, there's not doubt that the best approuch is create a "second" table and do a subsequent update, like @OMG Ponies said.

Also, try to disable triggers if any of them will do something with new value (eg: log's trigger, since we are not changing the "value" it self). This can increase performance a lot, depending what your triggers do.

Something like this:

-- STEP 1: CREATING SECOND TABLE
START TRANSACTION;
CREATE TABLE MY_SECOND_TABLE AS SELECT <YOURKEYFIELDNAME>, (ATTRIBUTE AS NUMERIC) AS ATTRIBUTE FROM MY_TABLE;    
CREATE UNIQUE INDEX UI_MY_SECOND_TABLE ON MY_SECOND_TABLE (<YOURKEYFIELDNAME>);
COMMIT;

-- STEP 2: UPDATING A SOURCE TABLE
START TRANSACTION;
ALTER TABLE MY_TABLE DISABLE TRIGGER ALL;
ALTER TABLE MY_TABLE DROP COLUMN ATTRIBUTE;
ALTER TABLE ATTRIBUTE ADD ATTRIBUTE INTEGER;
UPDATE MY_TABLE T SET ATTRIBUTE = (SELECT ATTRIBUTE FROM MY_SECOND_TABLE T2 WHERE T2.<YOURKEYFIELDNAME> = T.<YOURKEYFIELDNAME>);    
ALTER TABLE MY_TABLE ENABLE TRIGGER ALL;
COMMIT;

-- DROP SECOND TABLE
DROP TABLE MY_SECOND_TABLE;
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.