PL/SQL: Looping through a list string - for-loop

Please forgive me if I open a new thread about looping in PL/SQL but after reading dozens of existing ones I'm still not able to perform what I'd like to.
I need to run a complex query on a view of a table and the only way to shorten running time is to filter through a where clause based on a variable to which such table is indexed (otherwise the system ends up doing a full scan of the table which runs endlessly)
The variable the table is indexed on is store_id (string)
I can retrieve all the store_id I want to query from a separate table:
e.g select distinct store_id from store_anagraphy
Then I'd like to make a loop that iterate queries with the store_id identified above
e.g select *complex query from view_of_sales where store_id = 'xxxxxx'
and append (union) all the result returned by each of this queries
Thank you very much in advance.
Gianluca

In theory, you could write a pipelined table function that ran multiple queries in a loop and made a series of pipe row calls to return the results. That would be pretty unusual but it could be done.
It would be far, far more common, however, to simply combine the two queries and run a single query that returns all the rows you want
select something
from your_view
where store_id in (select distinct store_id
from store_anagraphy)
If you are saying that you have tried this query and Oracle is choosing to do a table scan rather than using the index then what you really have is a tuning problem. Most likely, statistics on one or more objects are inaccurate which leads Oracle to expect that this query would return more rows than it really will thus favoring the table scan. You should be able to fix that by fixing the statistics on the objects. In a pinch, you could also use hints to force an index to be used.

Related

Fastest access to collection in PLSQL for updating values associated with a record

I'm trying to cache data to memory for fast access on 3 different keys in PLSQL. The problem is not really existent in any language that has pointers, but i'm struggling with PLSQL as of there is none that I'm aware of. I need to do this because I have a very large looping function that updates data in a very fine grained way and would last quite an eternity otherwise.
The basic idea is that I have a collection in memory sorted by key_1. I'd want to make changes to a value of the first record which would influence the key_1 value of the record itself and several other specific values of records in the collection which have the same key_2 and has any key_3 values as the record I modified. After the modification I'd just bubble sort the modified first row to it's place instead of using a time consuming query.
So basically a record looks like this:
create type t_num_tbl is table of number;
create type rec_type as object
(
key_1 number,
key_2 varchar2(30),
key_3 t_num_tbl
);
and the collection is like this:
create type rec_typetbl is table of rec_type;
v_rectbl rec_typetbl := rec_typetbl();
If I modify a record I'd have to give out a select/update that looks something like this to be able to modify the associated records:
SELECT *
FROM table(v_rectbl)t
WHERE t.key_2 = modifiedrec.key_2
AND
(SELECT count(*)
FROM table(t.key_3)
JOIN table(modifiedrec.key_3) USING (column_value)) > 1;
The main problem here is that the data is not indexed in the memory and the access is just not fast enough for my purpose.
Are there any solutions in PLSQL that could compare to the performance of using a pointer array in a record to the associated elements of the collection? The associations are known beforehand since key_2, key_3 values don't change.
First, I can recommend against your design and would rather see you use the RDBMS the way it is designed (i.e. indexed access).
Having said that, every Oracle table has a rownum pseudo column that is a pointer to a row (i.e. that's how indexes internally reference specific rows in a table). If you have the record, you can save the rownum in your data structure to get back to it quickly (don't persist the rownum long term as oracle can change the rownum when tables/rows are re-organized).

How to compare table data structure

How to compare table data structure.
1. Any table added or deleted.
2. Any column in the tables added or deleted.
So my job is to verify if any table or columns are added/deleted on 1st of every month.
My plan is to run a sql query and take a copy of entire list of tables and it's data type only (NO DATA) and save it in txt file or something and use it as base line, and next month run the same sql query and get the results and compare the file. is it possible? please help with the sql query which can do this job.
This query will give you a list of all tables and their columns for a given user (just replace ABCD in this query for the user you have to audit and providing you have access to all that users tables this will work).
SELECT table_name,
column_name
FROM all_tab_columns
WHERE owner = 'ABCD'
ORDER
BY table_name,
column_id;
This answers your question but I have to agree with a_horse_with_no_name that is not a good way implement change control, most notably because the changes have already happened.
This query is very basic and doesn't give you all the information you'd need to see if a column has changed (or any information about other objects types etc), but then you only asked about additions and deletions of tables and columns and you can compare the output of this script to previous outputs to find the answer to your allotted task.

How to rebuild data combination Power Query

In Power Query, I want to use a list of distinct values from one query (e.g. list of customers present on "Sales" table), to inject it on a SQL statement on another query (e.g. "Customer" dimensional table).
To pull the list of distinct values I have a function, getDistinct() that:
Retrieves one column from a query choice,
Only keep distinct values present on that column, and
Return these distinct values separated by commas so they can be injected within an SQL statement.
This function works fine on a standalone query. However, when I try to use it on my "Customer" query it throws an error (see code and error below):
let
Source = Oracle.Database("myServer", [Query="select * from db_customer where customer_id in (" & getDistinct(Sales,"CustomerID") & ")"])
in
Source
And the error:
Formula.Firewall: Query 'Customer' (step 'Source') references
other queries or steps, so it may not directly access a data source.
Please rebuild this data combination.
I've tried creating a different query that executes the function and then referencing it on my "Customer" query, but this doesn't seem to work. I know I can "Ignore Privacy Levels" (which by the way, I've checked and works), but since I don't know the implications of it, I'm afraid of leaked data.
I don't see why a function or any hand-written code is necessary for this requirement.
I would create a Query to get the Sales table and then Group by CustomerID. I would set that to: Load To / Only Create Connection.
Then the Customers Query would just be:
Source is Oracle Customers table
Merge to Sales Query on CustomerID, with Join Kind = Inner

Is there any use to create index on all the table columns in oracle?

In our one of production database, we have 4 column table and there are no PK,UK constraints on it. only one notnull constraint on one column. The inserts are slow on this table and when I checked the indexes , there is one index which is built on all columns.
It is a normal table and not IOT. I really don't see a need of all column index, but wondering why the developers has created it?
Appreciate your thoughts?
It might be usefull, i.e. if you (mainly) query all columns oracle doesn't have to access the table at all, but can get all the data from the index. Though inserts take longer because a larger index has to be maintained by the dbms everytime.
One case where it could be useful is,
Say for example, you are trying to check the existence of records in this table and for that you have to have joins on all four columns. So in such a case if you have written a correlated query like below,
SELECT <something>
FROM table_1 t1
WHERE EXISTS
(SELECT 1 FROM table_t2 t2 where t1.c1=t2.c1 and t1.c2=t2.c2 and t1.c3=t2.c3 and t1.c4=t2.c4)
Apart from above case, it looks an error to me from developer's side.
Indexes are good to better query optimization but causes slow updates/inserts because the indexes needs to be updated at each modification.
If these tables first use is querying and inserts happens only in a specific periods like a batch at the beginning or the end of the day only, then you can remove the indexes before updating tables and then restore them.
In addition, all the queries all these tables need to be analysed to see which indexes are useful and which are not?
Anyway, You need to ask developers before removing these indexes.

Does a large number of arguments in the WHERE IN() clause affect performance in sqlite?

SELECT * FROM atable WHERE some_id IN(3,5,2)
This is ok. But what if I have to pass 1000 IDs? Will it slow down the query?
some_id is always indexed.
I know I probably shouldn't pass that number of arguments, but I need to get a large number of records from a table, then get related records from 2 other tables. If I use JOINs the returned array is huge. So I figured I should just do the main query, then two other queries for the related tables using the IN clause where I pass the IDs from the main query

Resources