Parse table names from a bunch SQL statements

2024/11/16 21:50:37

I have an table with thousands of SQL statements in a column called Queries. Any ideas on how to get just the table names from the statements by using a regular expression?

Answer

Were it me, I would tend to try to approach the problem a different way. Rather than writing a SQL parser (which would require much more than a regular expression unless you can guarantee that all the SQL statements using a very tiny subset of the available SQL grammar), I would tend to generate a query plan for each object and then query PLAN_TABLE to see the objects that Oracle has to hit. You'd need to do an additional lookup for index accesses to find out what table the index is defined on but that should be reasonably straightforward.

If you go down this path, however, you'll be retrieving the base tables that your query actually touches rather than whatever views the queries may actually refer to. That is, if you have a query SELECT * FROM view_1 and view_1, in turn, is defined as a query against table_a and table_b, only table_a and table_b will be part of the plan. And you would need to disable query_rewrite for the session if you wanted to prevent the query plans from referencing materialized views if those materialized views were not specifically part of the query.

If, for each query, you do an

EXPLAIN PLAN FOR <<the query>>

you can then

SELECT DISTINCT object_owner, object_name, object_typeFROM plan_table

to get the list of objects. If OBJECT_TYPE is like INDEX%, you can then use the DBA_INDEXES view (or ALL_INDEXES or USER_INDEXES depending on who owns the objects in question and what level of privileges you have) to determine what table that index is defined on

SELECT table_owner, table_nameFROM dba_indexesWHERE owner = <<object_owner from plan_table>>AND index_name = <<object_name from plan_table>>

So, for example, if I have a view view_1

 create or replace view view_1asselect *from emp join dept using (deptno)

and a query

select * from view_1;

I can do

SQL> explain plan for select * from view_1;Explained.SQL> ed
Wrote file afiedt.buf1      SELECT distinct object_owner, object_name, object_type2*       FROM plan_table
SQL> /OBJECT_OWNER                   OBJECT_NAME               OBJECT_TYPE
------------------------------ ------------------------- -------------------------SCOTT                          DEPT                      TABLE
SCOTT                          PK_DEPT                   INDEX (UNIQUE)
SCOTT                          EMP                       TABLE

This tells me that the query is actually hitting the EMP and DEPT tables. It is also hitting the PK_DEPT index so I can look to see what table that is defined on.

SQL> ed
Wrote file afiedt.buf1      SELECT table_owner, table_name2        FROM dba_indexes3       WHERE owner = 'SCOTT'4*        AND index_name = 'PK_DEPT'
SQL> /TABLE_OWNER                    TABLE_NAME
------------------------------ ------------------------------
SCOTT                          DEPT

As it turns out, that index is defined on the DEPT table as well, so I know that only the EMP and DEPT tables in the SCOTT schema are going to be involved in the query.

https://en.xdnf.cn/q/120284.html

Related Q&A

click multiple buttons with same class names in Python

This a column in a table this column contains buttons, on pressing each buttons a pdf is downloadedThe buttons have the same class names and I want to click on all the buttons.This is what I did, but i…

Python equivalent to subset function in r [duplicate]

This question already has answers here:subsetting a Python DataFrame(6 answers)Closed 4 years ago.I dont know python at all but the project Im currently working on must be done using it, I have this r …

Force subprocess to use Python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python: Im making a simple calculator for class. Whats wrong with this code? [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 7 months ago.My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I…

FileNotFoundError Python Script

I am trying to run a python script, .py in the windows command prompt. I drag the script from my files into the command prompt window. I run it. Then, the script presents a prompt for me to enter the f…

Filtered product of lists without repetitions

I need an efficient solution to find all combinations without repetitions of a given lists. It has to work like this: l1 = [1, 2, 3] l2 = [3, 4, 5] combinations(l1, l2) = [(2, 4), (3, 4), (1, 5), (1, 4…

int object is not callable only appears randomly

Sometimes this code works just fine and runs through, but other times it throws the int object not callable error. I am not real sure as to why it is doing so.for ship in ships:vert_or_horz = randint(0…

scraping css values using scrapy framework

Is there a way to scrap css values while scraping using python scrapy framework or by using php scraping. any help will be appreaciated

Access dict via dict.key

I created a dict source = {livemode: False}. I thought its possible to access the livemode value via source.livemode. But it doesnt work. Is there a way to access it that way?As a not source[livemode]…

Function not returning anything

My viewdef login(request):c = {}c.update(csrf(request))return render_to_response(request, login.html, c)def auth_view(request):username = request.POST.get (username, )password = request.POST.get (passw…