DB2 performance lack in subselect - db2

In my subselect, I select for a view, which takes 30% longer time
then to copy the content of the view into this query.
Is their any solution to handle this with a view.
select *
from abc
where id = 1
and ref_id in
(select ref
from view_vc
)

Related

Finding the percentage (%) range of average value in SQL

I am wanting to return the values that lie within 20% of the average value within the Duration column in my database.
I want to build on the code below but instead of returning Where Duration is less than the average value of duration I want to return all values which lay within 20% of the AVG(Duration) value.
Select * From table
Where Duration < (Select AVG(Duration) from table)
Here is one way...
Select * From table
Where Duration between (Select AVG(Duration)*0.8 from table)
and (Select AVG(Duration)*1.2 from table)
perhaps this to avoid repeated scans:
with cte as ( Select AVG(Duration) as AvgDuration from table )
Select * From table
Where Duration between (Select AvgDuration*0.8 from cte)
and (Select AvgDuration*1.2 from cte)
or
Select table.* From table
cross join ( Select AVG(Duration) as AvgDuration from table ) cj
Where Duration between cj.AvgDuration*0.8 and cj.AvgDuration*1.2
or using a window function:
Select d.*
from (
SELECT table.*
, AVG(Duration) OVER() as AvgDuration
From table
) d
Where d.Duration between d.AvgDuration*0.8 and d.AvgDuration*1.2
The last one might be the most efficient method.

SQL Server 2008 - Creating a View from Table

i am trying to create a view from a SQL table
following is a (very simplified...) subset of data in the table:
in the view i have to show only the EC_MODEL that have Installation as last EC_TYPE
that is, from the table, only the sensor from Station1 should be selected; on the contrary, the EC_MODEL from Station2 must not be selected, because the last operation is a Removal.
Well, I couldn't write the query that does the operation...how could I solve ?
thanks
Try this query:
select
ec_model
from (
select
ec_model, ec_type, rn = row_number() over (partition by ec_model order by operation_date desc)
from
myTable
) t
where
rn = 1
and ec_type = 'installation'

Doing SELECT from a VIEW is very slow

I have a table say tbl_test. In this table I have 2.4 million records. I run the following three queries which are very very slow and frustrating.
select count(*) from tbl_test;
-- 2.4 mil records in ~9 seconds
select count(*) from tbl_test where status = 'active';
-- 2.4 mil records in ~9 seconds
select count(*) from tbl_test where status = 'inactive';
-- 0 records in ~0 seconds
I have created a view say view_tbl_test using the following query:
create view view_tbl_test as
select * from
(select count(*) count_active from tbl_test where status = 'active' ) x,
(select count(*) count_inactive from tbl_test where status = 'inactive' ) y,
(select count(*) count_total from tbl_test) z
Now, I am picking only the single row from the view and its taking the same amount of time like previous.
select * from view_tbl_test limit 1;
Am I doing something wrong here? Is there any way which can make the view to return data in ~0 seconds?
Your statement runs three selects on the table. This can be done with a single statement:
create view view_tbl_test
as
select count(case when status = 'active' then 1 end) as count_active
count(case when status = 'inactive' then 1 end) as count_inactive,
count(*) as count_total
from tbl_test;
This should run in approx. 9seconds as it essentially does the same as your first statement.
The last statement is probably that fast because you have an index on status but as you did not provide the execution plans, this is nearly impossible to tell.

Updating with Nested Select Statements

I have a table that holds 3 fields of data: Acct#, YMCode, and EmployeeID. The YMCode is an Int that is formatted 201308, 201307, etc. For each Acct#, I need to select the EmployeedID used for the YMCode 201308 and then update all of the other YMCodes for the Acct# to the EmployeedID used in 201308.
so for each customer account in the table...
Update MyTable
Set EmployeeID = EmployeeID used in YMCode 201308
Having a hard time with it.
Put it in a transaction and look at the results before committing, but I think this is what you want:
UPDATE b
SET EmployeeID = a.EmployeeID
FROM MyTable a
INNER JOIN MyTable b
ON a.[Acct#] = b.[Acct#]
where a.YMCode =
(SELECT MAX(YMCode) from MyTable)
To get max YMCode, just add select statement at the end.

Need better summation select statement in postgres function

I've got two tables in my database. One of them, 'orders', contains a set of columns with an integer which represents what the order should contain (like 5 of A and 15 of B). The second table, 'production_work', contains those same order columns, and a date, so whenever somebody completes part of an order, I track it.
So now i need a fast way to know which orders are completed, and I'm hoping to avoid a 'completed' table on the first column as orders are editable and it's just more logic to keep correct.
This query works, but it's horribly written. What's a better way to do this? There are actually 12 of these columns that go into this query...I'm just showing 3 of them for the example.
SELECT *
FROM orders o
WHERE ud = (SELECT SUM(ud) FROM production_work WHERE order_id = o.ident)
AND dp = (SELECT SUM(dp) FROM production_work WHERE order_id = o.ident)
AND swrv = (SELECT SUM(swrv) FROM production_work WHERE order_id = o.ident)
select o.*
from
orders o
inner join
(
select order_id, sum(ud) as ud, sum(dp) as dp, sum(swrv) as swrv
from production_work
group by order_id
) pw on o.ident = pw.order_id
where
o.ud = pw.ud
and o.dp = pw.dp
and o.swrv = pw.swrv