Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to determine if an field exist in a ORACLE table

Tags:

sql

oracle

exists

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.

like image 899
RRUZ Avatar asked Feb 16 '10 21:02

RRUZ


People also ask

How do you check if data exists in a table Oracle?

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.

Which is faster in or exists in Oracle?

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.

How do you check if a column exists in SQL?

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.


3 Answers

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).

like image 188
Vincent Malgrat Avatar answered Sep 22 '22 19:09

Vincent Malgrat


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

like image 43
Joseph Bui Avatar answered Sep 24 '22 19:09

Joseph Bui


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.

like image 41
LBushkin Avatar answered Sep 24 '22 19:09

LBushkin