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.
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 TABLEstatement using the appropriate data types rather than creating the table based on aSELECTstatement. You can’t do things like define constraints on a table that you’re creating using aCREATE TABLE AS SELECTstatement 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_DDLprocedure to fetch the DDL for the table and then manually modify that DDL to extract just what you want to retain.