Oracle multiple partition selection - sql

I have a query that selects from multiple partitions
select * from table partition (P1), table2, table3, table4
union all
select * from table partition (P2), table2, table3, table4
....
select * from table partition (P12), table2, table3, table4
Table has over 30,331,246 entry's in the 12 partitions. I need to find a faster way to pull up the result (now it takes approx 35min). If I do select from the table not the partition the time will be over 135min but it's not how a query should look.
Could you please assist me in finding another way to do this ?

Related

hive how to loop partitions

there is table1 partitioned by date, and I create a table2 to store data from table1, and the table2 is also partitioned by date, I want to loop table1's partition, and select the data into table2 by partition. or there some other ways?

INSERT with UNION ALL returns only the first select result - HIVE Beeline

I want to do a query that inserts the result of table1 with table2 union's result. But when I try on beeline:
insert into table table3
select * from (
select * from table1 t1
where
h_time > '2019-05-01 00:00:00'
and t1.id in (select id from table4)
union all
select * from table2 t2
where
h_time > '2019-05-01 00:00:00'
and t2.id not in (select id from table4)
);
Consider that the both tables 1 and 2 have the same column numbers and datatypes have already fixed previously.
The result in table3 is only the rows of table1. And when I change the position of table 1 and 2, I get only the rows of table2.
Anyone have a guess what's happening?
Tks in advance!
it may be problem with table too, it may not show the actual count sometimes, use select * from tablename. If the count is as expected the run Analyze statement it'll re-compute and repair the statistics

How can I select the same number of records from two different tables in SQL Server

How can I select the same number of records from two different tables in SQL Server?
For example, I have a table A with 10 records and table B with 15 records; how can I select 10 records from A and 10 records from B ?
If both the tables have same number and type of columns. Then use union or union all
Select col1,col2,col3 from table1
union
Select col1,col2,col3 from table2
Something like that?
SELECT col1,col2,col3
FROM TABLE_A
UNION
SELECT TOP (SELECT COUNT(*) FROM TABLE_A) col1,col2,col3
FROM TABLE_B

UNION ALL vs OR condition in sql server query

I have to select some rows based on a not exists condition on a table. If I use a union all as below, it gets executed in less than 1 second.
SELECT 1 FROM dummyTable
WHERE NOT EXISTS
(
SELECT 1 FROM TABLE t
WHERE Data1 = t.Col1 AND Data2=t.Col2
UNION ALL
SELECT 1 FROM TABLE t
WHERE Data1 = t.Col2 AND Data2=t.Col1
)
but if I use an OR condition, it takes close to a minute as SQL server is doing a table lazy pool. Can someone explain it?
SELECT 1 FROM dummyTable
WHERE NOT EXISTS
(
SELECT 1 FROM TABLE t
WHERE ( (Data1 = t.Col1 AND Data2=t.Col2) OR (Data1 = t.Col2 AND Data2=t.Col1))
)
The issue is that you are specifying two conditions with OR that apply to separate tables in your query. Because of this, the nonclustered index seek has to return most or all of the rows in your big table because OR logic means they might also match the condition clause in the second table.
Look at the SQL execution plan in all three examples above, and notice the number of rows that come out of the nonclustered index seek from the big table. The ultimate result may only return 1,000 or fewer of the 800,000 rows in the table but the OR clause means that the contents of that table have to be cross-referenced with the conditional in the second table since OR means they may be needed for the final query output.
Depending on your execution plan, the index seek may pull out all 800,000 rows in big table because they may also match the conditions of the OR clause in the second table. The UNION ALL is two separate query against one table each, so the index seek only has to output the smaller result set that might match the condition for that query.
I hope this makes sense. I've run across the same situation while refactoring slow-running SQL statements.
Cheers,
Andre Ranieri
The query plan is also affected by the number of rows in your tables. How many rows are there in table t ?
You could also try:
SELECT 1 FROM dummyTable
WHERE NOT EXISTS
(
SELECT 1 FROM TABLE t
WHERE Data1 = t.Col1 AND Data2=t.Col2
)
AND NOT EXISTS
(
SELECT 1 FROM TABLE t
WHERE Data1 = t.Col2 AND Data2=t.Col1
)
or (corrected for SQL-Server) this that will use the index:
WITH tt AS <---- a temp table with 2 rows
( SELECT Data1 AS Col1, Data2 AS Col2
UNION
SELECT Data2 AS Col1, Data1 AS Col2
)
SELECT 1 FROM dummyTable
WHERE NOT EXISTS
(
SELECT 1
FROM TABLE t
JOIN tt
ON tt.Col1 = t.Col1 AND tt.Col2=t.Col2
)
The usage of the OR is probably causing the query optimizer to no longer use an index in the second query. Look at the explain for each query and that will tell you the answer.

SQL Append table queries

I have two tables, say table1 with two rows of data say row11 and row12
and table2 with 3 rows of data sat row21, row22, row23
Can anyone provide me with the SQL to create a query that returns
row11
row12
row21
row22
row23
Note: I dont want to create a new table just return the data.
Use UNION ALL, based on the example data:
SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2
UNION removes duplicates - if both tables each had a row whose values were "rowx, 1", the query will return one row, not two. This also makes UNION slower than UNION ALL, because UNION ALL does not remove duplicates. Know your data, and use appropriately.
select * from table1 union select * from table2
Why not use a UNION?
SELECT
Col1,Col2,Col3
FROM
TABLE1
UNION
SELECT
Col1,Col2,Col3
FROM
TABLE2
Are the columns on the two tables identical?
In MS Access you can achieve the same goal with an INSERT INTO query:
INSERT INTO TABLE1 SELECT * FROM TABLE2;