1

I need to create a table that has some fields that another one has but I don't want to fill it with the value the other one has. I.E.:

Say we have a table A with millions of records and I want to create a table B (that is not a view, it's going to be a table) that contains 3 columns of table A with the same type.

I have done it the following way:

CREATE TABLE B AS (
            SELECT column1,column2,column3 from A where 1=0;
        );

But I'm really concerned about perform about performance and I'm afraid about that method will fetch every row(remember the table has so many rows) and check the absurd condition.

Therefore my questions are:

  • Is the condition being checked for every row or the sql optimizer will discard it automatically?

  • Is there a better way to do it?

Thanks in advance.

2 Answers 2

4

The optimizer will evaluate the condition at compile time and will not fetch all the data from the table just to check a predicate that does not depend on the data being fetched.

Generally, however, you'd simply build the CREATE TABLE statement using the appropriate data types rather than creating the table based on a SELECT statement. You can't do things like define constraints on a table that you're creating using a CREATE TABLE AS SELECT statement which is something you'd really want if this is really going to be a permanent table.

If you really wanted to, you could use the DBMS_METADATA.GET_DDL procedure to fetch the DDL for the table and then manually modify that DDL to extract just what you want to retain.

1

There is nothing wrong with your method. Oracle will first create an empty table then try to populate it. Because the where criteria returns false, no data will be fetched therefore no performance issue; I did try it on 10 Mil large table, no problem with performance.

Michel.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.