How does Oracle implement an SQL query over a VIEW - oracle

We have a VIEW defined over a base table using an SQL Queries ( SQL query1 )
Im trying to understand how Querying ( SQL query2 ) over a view work
When I run an SQL over a VIEW, does Oracle first execute query1 to create a temp table and then run query2 over the temp table ?
Or does it create a single composite query by combining query1 and query2 in order to give the result
( my query2 has high selectivity if run directly over the base table and a composite query should run much faster than executing query1 first )

Or does it create a single composite query by combining query1 and query2 in order to give the result
Yes, CBO (oracle cost-based optimizer) expands final query and transforms it and build an execution plan and you can check final query after transformation in trace 10053(optimizer trace) or using DBMS_UTILITY.EXPAND_SQL_TEXT
NB. DBMS_UTILITY.EXPAND_SQL_TEXT has appeared in 12.1, but you tagged Oracle 11g, so you need to use dbms_sql2.expand_sql_text, an example: https://github.com/xtender/xt_scripts/blob/master/expand_11.sql

Related

Getting data from an Oracle database in Power BI desktop keeps forever

I am trying to get data from a single Oracle database table in Power BI Desktop. This table has around 2 million rows of data.
The following Query works just fine:
let
Source = Oracle.Database("PPM_PROD", [HierarchicalNavigation=true]),
PPM_DEV = Source{[Schema="PPM_DEV"]}[Data],
Timesheets = PPM_DEV{[Name="BI_TIMESHEET"]}[Data]
in
Timesheets
However, if I add an SQL statement as below, I run into problems:
let
Source = Oracle.Database("PPM_PROD", [HierarchicalNavigation=true, Query="SELECT * FROM PPM_DEV.BI_TIMESHEET#(lf)WHERE (ACTUAL_TIME > 0) OR (OVERTIME > 0) "]),
PPM_DEV = Source{[Schema="PPM_DEV"]}[Data],
Timesheets = PPM_DEV{[Name="BI_TIMESHEET"]}[Data]
in
Timesheets
This query will not get any data and data load/refresh will be stuck at "Evaluating" phase forever. Any suggestions will be greatly appreciated.
In this case, I'd recommend not using the SQL statement but rather filtering it in the Power Query Editor. In the most common filtering scenarios, Power Query will transform the filter steps applied into a native query similar to your SQL statement.
You can still use a SQL statement if you prefer but the steps
PPM_DEV = Source{[Schema="PPM_DEV"]}[Data],
Timesheets = PPM_DEV{[Name="BI_TIMESHEET"]}[Data]
don't make sense if you've already selected and filtered the Timesheets table. Those steps are there to navigate to the schema and table you intend to query but you've already handled that in your SQL.

Generated group by sql query does not make sense

I'm using entity framework core for mysql, and i've been running a complex linq query which i'm trying to optimise.
I turned on logging in the mysql server to view the resulting queries from the linq queries.
Oddly, none of it made sense as my complex query that joined 5 tables and performed multiple group bys, where, and order by clause was registered in the logs as 5 separate select all columns from table statements.
So, I tried a simple group by statement for one table. The resulting sql log produced "Select all_columns from table_name order by groupbyid".
Can anyone explain what happened here?
Thanks in advance.
More info as requested:
Sql query:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonId);
queryCommand.ToList();
Resulting mysql log after:
SELECT .... [very long list of column names]
FROM TableExtract AS p
ORDER BY p.tableExtractPersonId
I've tried two different entity framework libraries: MySql.Data.EntityFrameworkCore(v8.0.17) and Pomelo.EntityFrameworkCore.MySql (v2.2.20) with the same results. I've tried .net core 3.0 and also received the same results. I'm going to try .net standard next.
Ok. I found it:
var queryCommand = (from p in _context.TableExtract group p by p.tableExtractPersonIdinto g select g.Key)
Forces linq to evaluate as a SQL group by. Otherwise apparently it does it's own thing with the group by.

How to execute select query on oracle database using pi spark?

I have written a program using pyspark to connect to oracle database and fetch data. Below command works fine and returns the contents of the table:
sqlContext.read.format("jdbc")
.option("url","jdbc:oracle:thin:user/password#dbserver:port/dbname")
.option("dbtable","SCHEMA.TABLE")
.option("driver","oracle.jdbc.driver.OracleDriver")
.load().show()
Now I do not want to load the entire table data. I want to load selected records. Can I specify select query as part of this command? If yes how?
Note: I can use dataframe and execute select query on the top of it but I do not want to do it. Please help!!
You can use subquery in dbtable option
.option("dbtable", "(SELECT * FROM tableName) AS tmp where x = 1")
Here is similar question, but about MySQL
In general, the optimizer SHOULD be able to push down any relevant select and where elements so if you now do df.select("a","b","c").where("d<10") then in general this should be pushed down to oracle. You can check it by doing df.explain(true) on the final dataframe.

MongoDb Java with query : Select ....From ... Where field in (Select ......)

My database has 2 collections : Cell(_id, segment_id, cell_id) and SegmentSpeed(_id, segment_id, speed)
And I want to perform the query below (sql format) in Java :
"Select * From SegmentSpeed Where segment_id in (Select segment_id From Cell Where cell_id>5)"
That is Mysql query, and the problem is I want to execute this query with MongoDb format in JAVA.
Thanks !
MongoDB does not support joins.
You may have to resort to some clunky and inefficient map-reduce operations if you are unable to change your schema.
What you can do is embed the whole SegmentSpeed inside a document of Cell.

Join between a sqlsever table and an oracle table

I have been using SQL Server 2008 for a short time now and have never used Oracle before. I am able to access an Oracle table through SQL Server with the syntax
select * from [OracleDB1]..[OracleDB1].[Zips]
(where OracleDB1 is the oracle database and Zips is the table I require)
Is it possible to join a SQL Server table with this one in a Table-valued Function? Just using a normal join as I would with SQL Server tables gives an Invalid object name error on the Oracle table.
Can this be done directly (or at all) or is it possible to do this some other way such as table variables?
example query:
select * from dbo.Table1 t INNER JOIN [OracleDB1]..[OracleDB1].[Zips] z where t.zip = z.zip
I was performing the join wrong since I missed the ON clause. I was able to get it to work by declaring a temptable and joining on that.
declare #tempTable table{
ZIP nvarchar(5),
COUNTY nvarchar(10)
}
insert #tempTable select ZIP, COUNTY, from [OracleDB1]..[OracleDB1].[ZIPS]
select * from dbo.Table1 t INNER JOIN #tempTable z on t.ZIP = v.ZIP where t.AdmissionOn >= '08-08-2011' AND t.AdmissionOn <= ''09-08-2011'
This also worked in line as I had in the original question once I added the ON clause but the table variable suits my needs better since it only has to access the Oracle table once and not each comparison.

Resources