I am looking for a fast sql sentence for determine when a field exist or not in a table .
actually i am using this sentence
Select 1
from dual
where exists (select 1
from all_tab_columns
where table_name = 'MYTABLE'
and column_name = 'MYCOLUMN')
I think there must be a fastest way to determine whether or not a column exist in ORACLE.
UPDATE
I'm optimizing a larger software system that makes multiple calls to this Query, I can not modify the source code ;( , only i can modify the query which is stored in an external file.
the Table all_tab_columns has over a million of records.
Type a short Oracle program, using the following code as a guide: DECLARE record_exists INTEGER; BEGIN SELECT COUNT(*) INTO record_exists FROM your_table WHERE search_field = 'search value' AND ROWNUM = 1; IF record_exists = 1 THEN DBMS_OUTPUT. put_line('Record Exists') ELSE DBMS_OUTPUT.
Exist is more faster than IN because IN doesn't use indexes at the time of fetching but Exist uses Index at the time of fetching.
Checking Existence of the Column: For checking the existence we need to use the COL_LENGTH() function. COL_LENGTH() function returns the defined length of a column in bytes. This function can be used with the IF ELSE condition to check if the column exists or not.
the primary key of all_tab_columns
is owner, table_name, column_name
so looking for a particular owner will be faster (or use user_tab_columns
).
I suggest reading this AskTom article. It explains that the fastest way to check is not to check at all.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:698008000346356376
Querying the Oracle data dictionary - as you example indeed does, is probably the fastest way.
The data dictionary is cached in memory and should be able to satisfy the query pretty quickly. You may be able to get slightly faster results if you know the actual schema owner of the table - so that you don't incur the cost of searching against all schemas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With