Ways to speed up this query
i was wondering if anyone has any ideas on how to speed this query up
SELECT NAME, trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') AS opr_date, PRICE,
Case OPR_HOUR When 0 THEN 24 ELSE OPR_HOUR END AS OPR_HOUR
FROM ze_data.pjm_edf_lmp_hourly_int
WHERE trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') between to_date ('2010/07/26', 'yyyy/mm/dd') and to_date ('2010/07/28', 'yyyy/mm/dd') and NAME in ('MISO')
ORDER BY OPR_DATE desc, opr_hour descThe data is stored in the database with hours 0 to 23 this code take hour 0 and switches it to hour 24 of the the day before. I was wondering if there was a way to speed up the query as it takes 8 seconds to run
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0
Since you're comparing OPR_DATE to a date range, is there any need to add the time to OPR_DATE?
Would this work better? An index on OPR_DATE might help, but wouldn't an index on NAME be even better?
I don't know, just a guess. You haven't posted anything we can use.
SELECT NAME
,trunc (opr_date + opr_hour/24-numtodsinterval(1,'second'), 'DDD') AS opr_date
,PRICE
,Case OPR_HOUR When 0 THEN 24 ELSE OPR_HOUR END AS OPR_HOUR
FROM ze_data.pjm_edf_lmp_hourly_int
WHERE opr_date >= to_date ('2010/07/26', 'yyyy/mm/dd')
and opr_date <= to_date ('2010/07/28', 'yyyy/mm/dd')
and NAME in ('MISO')
ORDER BY OPR_DATE desc, opr_hour desc;
Similar Messages
-
How do I speed up this query by ignoring computers off the network faster?
Good afternoon,
I am running the below script to query the entire domain for local admins. Could anyone reccomend a way to speed this up by more quickly skipping computers that aren't on the network? Currently, every time it reaches a computer that is not on the network
it hangs for up to 20 seconds (computers on the network return the data in less than a second). If I could decrease the ping time-out time, I could speed up this query tenfold.
Script pasted below - Thank you!!!
$Searcher = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
$Searcher.Filter = "(objectClass=computer)"
$Computers = ($Searcher.Findall())
md C:\Reports\IBX_Local_Admins_ALL
Foreach ($Computer in $Computers)
$Path=$Computer.Path
$Name=([ADSI]"$Path").Name
write-host $Name
$members =[ADSI]"WinNT://$Name/Administrators"
$members = @($members.psbase.Invoke("Members"))
$members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty',
$null, $_, $null) | out-file -append C:\Reports\IBX_Local_Admins_ALL\$name.txtThis version will not fail no matter what is in the group.
function Get-LocalAdmins{
[CmdLetBinding()]
Param(
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
Position=0
)]$computer
Process{
Write-Verbose "Polling system: $computer"
if(Test-Connection $Computer -quiet -count 1){
$group=[ADSI]"WinNT://$computer/Administrators"
$group.Invoke("Members") |
ForEach-Object{
New-Object PsObject -Property @{
Computer=$Computer
aDSPath=$_.GetType().InvokeMember('aDSPath', 'GetProperty',$null, $_, $null)
#UserID=$_.GetType().InvokeMember('Name', 'GetProperty',$null, $_, $null)
}else{
Write-Warning "System not found: $computer"
$computers=([adsisearcher]'(objectClass=computer)').FindAll() |%{$_.Properties['name']}
$computers | Get-LocalAdmins -verbose
¯\_(ツ)_/¯
Hi JRV,
Thank you for your help so far! When I ran the above version, it seemed to be going fine but it stopped at a certain point saying the script was "successful", but it only queried 148 computers. I should note that it is also the same 148 computers each time
I run it. No errors, it just stops and says successful. Any ideas?
TY! -
How to speed up this query?
I have created a demo table:
create table demo1(d date);
and insert some data to table:
begin
-- add 6000000 rows
for i in 1..1000000 loop
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
insert into demo1 values(trunc(sysdate-i));
end loop;
commit;
end;
The query
select * from demo1
where d=to_date('25.10.2004','DD.MM.YYYY')
executed three times faster than
select from demo1 where d=trunc(sysdate-1);
Why? How to speed up this query if I do not want to use index?
I have created index:
create index demo1_indx on demo1(d);
Execution time of queries became identical (for this volume of data).Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
SQL> create table demo1(d date);
Table created.
SQL> begin
2 for i in 1..1000000 loop
3 insert into demo1 values(trunc(sysdate-i));
4 insert into demo1 values(trunc(sysdate-i));
5 insert into demo1 values(trunc(sysdate-i));
6 insert into demo1 values(trunc(sysdate-i));
7 insert into demo1 values(trunc(sysdate-i));
8 insert into demo1 values(trunc(sysdate-i));
9 insert into demo1 values(trunc(sysdate-i));
10 insert into demo1 values(trunc(sysdate-i));
11 end loop;
12 commit;
13 end;
14 /
PL/SQL procedure successfully completed.
SQL> alter session set timed_statistics=true;
Session altered.
SQL> alter session set sql_trace=true;
Session altered.
SQL> set timing on;
SQL> set autotrace on;
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:10.70
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
29 recursive calls
1 db block gets
28988 consistent gets
13030 physical reads
1035300 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.35
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12837 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:04.95
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d='25.10.2004';
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:03.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3285 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3285 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12752 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:17.53
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
6 recursive calls
0 db block gets
14503 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:15.82
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12753 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:14.56
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12758 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> select * from demo1 where d=trunc(sysdate-3);
D
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
25.10.04
8 rows selected.
Elapsed: 00:00:11.84
Execution Plan
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=3696 Card=159 Byte
s=1431)
1 0 TABLE ACCESS (FULL) OF 'DEMO1' (TABLE) (Cost=3696 Card=159
Bytes=1431)
Statistics
0 recursive calls
0 db block gets
14441 consistent gets
12757 physical reads
0 redo size
453 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
8 rows processed
SQL> alter session set sql_trace=false;
Session altered.
Elapsed: 00:00:00.00
SQL> alter session set timed_statistics=false;
Session altered.
Elapsed: 00:00:00.01
SQL> -
Is there any way to tune this query? EXPLAIN PLAN included
DB version:10gR2
The below query was taking more than 3 seconds. The statistics are up to date for these tables. Is there any other way i could tune this query?
SELECT COUNT(1)
FROM
INVN_SCOPE_DTL, ship_dtl WHERE ship_dtl.WHSE = INVN_SCOPE_DTL.WHSE (+)
AND 'QC' = INVN_SCOPE_DTL.FROM_WORK_GRP (+)
AND 'MQN' = INVN_SCOPE_DTL.FROM_WORK_AREA (+)
AND ship_dtl.START_CURR_WORK_GRP = INVN_SCOPE_DTL.TO_WORK_GRP (+)
AND ship_dtl.START_CURR_WORK_AREA = INVN_SCOPE_DTL.TO_WORK_AREA (+)
AND ship_dtl.WHSE = '930' AND ship_dtl.OWNER_USER_ID = 'CTZDM'
OR ship_dtl.OWNER_USER_ID = '*'
AND ship_dtl.STAT_CODE >= '10'
AND ship_dtl.STAT_CODE <= '20'
ORDER BY ship_dtl.OWNER_USER_ID DESC,
ship_dtl.CURR_TASK_PRTY ASC, INVN_SCOPE_DTL.DISTANCE ASC, ship_dtl.RLS_DATE_TIME ASC, ship_dtl.TASK_ID ASC;
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
| 0 | SELECT STATEMENT | | 1 | 86 | 86 (2)|
| 1 | SORT AGGREGATE | | 1 | 86 | |
| 2 | NESTED LOOPS OUTER | | 898 | 77228 | 86 (2)|
| 3 | INLIST ITERATOR | | | | |
|* 4 | TABLE ACCESS BY INDEX ROWID| ship_dtl | 898 | 31430 | 85 (2)|
|* 5 | INDEX RANGE SCAN | ship_dtl_IND_4 | 2876 | | 1 (0)|
| 6 | TABLE ACCESS BY INDEX ROWID | INVN_SCOPE_DTL | 1 | 51 | 2 (50)|
PLAN_TABLE_OUTPUT
|* 7 | INDEX UNIQUE SCAN | PK_INVN_SCOPE_DTL | 1 | | |
Predicate Information (identified by operation id):
4 - filter("ship_dtl"."WHSE"='930' AND "ship_dtl"."STAT_CODE">=10 AND
"ship_dtl"."STAT_CODE"<=20)
5 - access("ship_dtl"."OWNER_USER_ID"='*' OR "ship_dtl"."OWNER_USER_ID"='CTZDM')
7 - access("INVN_SCOPE_DTL"."WHSE"(+)='930' AND
"INVN_SCOPE_DTL"."FROM_WORK_GRP"(+)='QC' AND "INVN_SCOPE_DTL"."FROM_WORK_AREA"(+)='MQN'
PLAN_TABLE_OUTPUT
AND "ship_dtl"."START_CURR_WORK_GRP"="INVN_SCOPE_DTL"."TO_WORK_GRP"(+) AND
"ship_dtl"."START_CURR_WORK_AREA"="INVN_SCOPE_DTL"."TO_WORK_AREA"(+))
filter("ship_dtl"."WHSE"="INVN_SCOPE_DTL"."WHSE"(+))
25 rows selected.William Robertson wrote:
I notice an OR predicate in the middle of some AND predicates without explicit bracketing. Are you sure it does what you think it does?I underline this point.
A conjuction (AND expression) has a higher priority and will be executed (logically) before the disjunction (OR expression)! So your select looks like this
SELECT COUNT(1)
FROM INVN_SCOPE_DTL, ship_dtl
WHERE
( ship_dtl.WHSE = INVN_SCOPE_DTL.WHSE (+)
AND 'QC' = INVN_SCOPE_DTL.FROM_WORK_GRP (+)
AND 'MQN' = INVN_SCOPE_DTL.FROM_WORK_AREA (+)
AND ship_dtl.START_CURR_WORK_GRP = INVN_SCOPE_DTL.TO_WORK_GRP (+)
AND ship_dtl.START_CURR_WORK_AREA = INVN_SCOPE_DTL.TO_WORK_AREA (+)
AND ship_dtl.WHSE = '930'
AND ship_dtl.OWNER_USER_ID = 'CTZDM'
OR ( ship_dtl.OWNER_USER_ID = '*'
AND ship_dtl.STAT_CODE >= '10'
AND ship_dtl.STAT_CODE <= '20'
;This might be want you want, but I doubt it very much. Please add parenthesis', to get it working the way it should be.
Edited by: Sven W. on Oct 16, 2008 3:25 PM -
Speeding up a query on a concatenated field.
Hi,
I have a view with a concatenated field that basically comes down on this:
ALTER VIEW [dbo].[_FO_QP_Items_4_0] AS
SELECT
Col A ,Col B --etc
,CONVERT(nvarchar(36), sysguid) +'|'+crdnr AS [LevCode]
FROM table A
As you see I am concatenating 2 fields together to get a unique [LevCode] (concatenating Itemcode + Supplier code)
If I query that view like this, it runs rather slow on a 500.000+ rows table:
SELECT *
FROM [dbo].[_FO_QP_Items_4_0]
WHERE [LevCode] = 'DF007704-38EF-4389-8544-E70CC32404D5| 60102'
ORDER BY Col A
Unfortunately I am not able to add an extra column to the table and concatenate the fields there.
Is there a way to speed up the query (or view).
Thanks for helping me out here.Index view has high overhead. It has to be really justified.
BOL: "Indexed views work best when the underlying data is infrequently updated. The maintenance of an indexed view can be greater than the cost of maintaining a table index. If the underlying data is updated frequently, the cost of maintaining the indexed
view data may outweigh the performance benefits of using the indexed view. If the underlying data is updated periodically in batches but treated primarily as read-only between updates, consider dropping any indexed views before updating, and rebuilding them
afterward. Doing this may improve performance of the updates."
Reference:
https://technet.microsoft.com/en-us/library/ms187864%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
Can I refactor this query to use an index more efficiently?
I have a members table with fields such as id, last name, first name, address, join date, etc.
I have a unique index defined on (last_name, join_date, id).
This query will use the index for a range scan, no sort required since the index will be in order for that range ('Smith'):
SELECT members.*
FROM members
WHERE last_name = 'Smith'
ORDER BY joindate, idIs there any way I can get something like the following to use the index (with no sort) as well:
SELECT members.*
FROM members
WHERE last_name like 'S%'
ORDER BY joindate, idI understand the difficulty is probably; even if it does a range scan on every last name 'S%' (assuming it can?), they're not necessarily in order. Case in point:
Last_Name: JoinDate:
Smith 2/5/2010
Smuckers 1/10/2010An index range scan of 'S%' would return them in the above order, which is not ordered by joindate.
So is there any way I can refactor this (query or index) such that the index can be range scanned (using LIKE 'x%') and return rows in the correct order without performing a sort? Or is that simply not possible?xaeryan wrote:
I have a members table with fields such as id, last name, first name, address, join date, etc.
I have a unique index defined on (last_name, join_date, id).
This query will use the index for a range scan, no sort required since the index will be in order for that range ('Smith'):
SELECT members.*
FROM members
WHERE last_name = 'Smith'
ORDER BY joindate, idIs there any way I can get something like the following to use the index (with no sort) as well:
SELECT members.*
FROM members
WHERE last_name like 'S%'
ORDER BY joindate, idI understand the difficulty is probably; even if it does a range scan on every last name 'S%' (assuming it can?), they're not necessarily in order. Case in point:
Last_Name: JoinDate:
Smith 2/5/2010
Smuckers 1/10/2010An index range scan of 'S%' would return them in the above order, which is not ordered by joindate.
So is there any way I can refactor this (query or index) such that the index can be range scanned (using LIKE 'x%') and return rows in the correct order without performing a sort? Or is that simply not possible?Come on. Index column order does matter. "LIKE 'x%'" actually is full table scan. The db engine accesses contiguous index entries and then uses the ROWID values in the index to retrieve the table rows. -
Looking for a way to speed up rendering when only changing a JPG in HD Video
Hey guys,
Wondering if anyone can help. I have a movie that I render on a weekly basis for a business which has various HD video clips as well as a background JPG layer. Fundamentally, the video clip plays for 30 seconds followed by 30 seconds of the JPG, and this alternates for about 15 minutes. Every week I have to change the JPG to reflect what is current for that week, which means I have to re-render the entire project which is pretty time consuming. Is anyone familiar with a way to speed up this process? All help is greatly appreciated.Perhaps you misunderstood. If I understand your process correctly, you have one video over one jpeg background that repeats and repeats.
My recommendation: don't render the same thing over and over again in AE. Render in AE ONCE, then take it into the editing app to repeat and repeat. -
Is there any way to speed up "repair disk" over internet recovery on OSX
After receiving a grey screen on startup, I finally have been able to boot into internet recovery on my 13" macbook pro. I'm running "Repair Disk" from the Disk Utility on my "Macintosh HD" drive. The estimated time is listed at "1 day, 23 hours", and has been stuck there for about 8 hours. It was 2 days and then did move to 1 day, 23 hours, but has not moved since.
Is there any way to speed up this disk repair? Would connecting over ethernet rather than WiFi help? Should I go somewhere with a faster internet connection than my home (1.1Mbps Down / 1.6 Up)? It can't possibly take over 2 days to repair the disk under normal conditions, right?A hard wire connection should be faster than wifi. If you have been running Disk Utility Repair for 8 hrs, try is again and if after an hour you still see the same result, I suspect that the HDD is faulty.
I would try an external boot HDD and see if the MBP can boot from that. If it can, than that indicates that your internal HDD has failed and must be replaced. Transfer as much data as you can.
Ciao. -
Re: speeding up SPATIAL QUERY
hi! ,
Heres my sql statement ..
Select ODPM2001_I From DFT.URBANAREAS U
Where SDO_RELATE(U.GEOLOC, obTempPt, 'mask=CONTAINS querytype=WINDOW') = 'TRUE'; -- querytype=WINDOW
I have put a spatial index on U.GEOLOC ....
The sql statement takes too long
I pose my challenge to you ..
How can I speed up this query ?
From the DBA side and/or the PL/SQL side ...
thanks,
HarishHi,
The first thing you should try is updating to the latest patch release. Often there are performance enhancements included.
If you don't mind returning rectangles on whose boundary the point falls, use the anyinteract mask - it is the optimal mask to use if this situation allows it.
Hope this helps. -
Best way to print result of query to file from pl/sql?
If I have a query, let's say 'select * form my_table', what's the best way to print it to a file from pl/sql?
Do I have to create a cursor, go through each line, and use some file output function? Or is there some clever way to say 'print this query to file!'The rest of my procedure (the one generating the query itself) is in PL/SQL. So it needs to be done from there. Any tricks?
(What's paper? I meant 'write to file') -
Is ther any way to speed up SCP.
Hi,
I am restoring DB from previous cold backup of DB(on another machine).
but scp took app. 36hrs to copy datafiles(in .gz format)
is ther any way to speed up this copy for future.
DB size is app 1600GB
DB version 10.2.0.3
Sun 5.9Use FTP or NFS instead of SCP - or split the file to be transferred in pieces and transfer in parallel.
Ronny Egner
My Blog: http://blog.ronnyegner-consulting.de -
[8i] Query with lots of subqueries: a simpler way to do this query?
Ok, so generally, my problem is that I can't figure out how to write this query without including a bunch of copies of the same subquery (similar to having to join multiple copies of a table). Basically, I have to get information that's in rows in one table into columns in the results of my query. I've created sample tables, with insert statements for sample data, and a trimmed-down version of the query I'm running against those (2) tables. I'll explain what I mean by "trimmed-down" where I provide the SQL for that query.
My restrictions:
1) I'm running in 8i
2) I cannot create any tables in the database (I can only do read-only queries and create views).
Here are my sample tables and sample data:
CREATE TABLE reqs
( req_id NUMBER NOT NULL,
part_no VARCHAR2(5),
req_date DATE,
req_qty NUMBER,
CONSTRAINT reqs_pk PRIMARY KEY (req_id)
INSERT INTO reqs
VALUES (1,'part1',To_Date('01/01/2010','mm/dd/yyyy'),5);
INSERT INTO reqs
VALUES (2,'part2',To_Date('01/10/2010','mm/dd/yyyy'),25);
INSERT INTO reqs
VALUES (3,'part1',To_Date('01/20/2010','mm/dd/yyyy'),3);
INSERT INTO reqs
VALUES (4,'part2',To_Date('02/01/2010','mm/dd/yyyy'),15);
INSERT INTO reqs
VALUES (5,'other',To_Date('02/05/2010','mm/dd/yyyy'),1);
INSERT INTO reqs
VALUES (6,'part1',To_Date('02/07/2010','mm/dd/yyyy'),10);
INSERT INTO reqs
VALUES (7,'part3',To_Date('02/25/2010','mm/dd/yyyy'),22);
INSERT INTO reqs
VALUES (8,'part1',To_Date('03/01/2010','mm/dd/yyyy'),24);
INSERT INTO reqs
VALUES (9,'part3',To_Date('04/01/2010','mm/dd/yyyy'),12);
CREATE TABLE part
( part_no VARCHAR2(5) NOT NULL,
part_desc VARCHAR2(25),
qty_instock NUMBER,
CONSTRAINT part_pk PRIMARY KEY (part_no)
INSERT INTO part
VALUES ('part1','description 1 here',5);
INSERT INTO part
VALUES ('part2','description 2 here',10);
INSERT INTO part
VALUES ('part3','description 3 here',0);Now, here is the query I'm running against the two tables:
SELECT part.part_no
, part.part_desc
, part.qty_instock
, pd.tot_req_qty AS qty_past_due
, m1.tot_req_qty AS qty_this_month
, m2.tot_req_qty AS qty_next_month
, m3.tot_req_qty AS qty_month_3
FROM part
SELECT reqs.part_no
, SUM(reqs.req_qty) AS tot_req_qty
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END AS month_reqd
FROM reqs
GROUP BY reqs.part_no
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END
) pd --for past due
SELECT reqs.part_no
, SUM(reqs.req_qty) AS tot_req_qty
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END AS month_reqd
FROM reqs
GROUP BY reqs.part_no
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END
) m1 --for month 1
SELECT reqs.part_no
, SUM(reqs.req_qty) AS tot_req_qty
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END AS month_reqd
FROM reqs
GROUP BY reqs.part_no
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END
) m2 --for month 2
SELECT reqs.part_no
, SUM(reqs.req_qty) AS tot_req_qty
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END AS month_reqd
FROM reqs
GROUP BY reqs.part_no
, CASE
WHEN reqs.req_date < sysdate
THEN 'Past Due'
ELSE To_Char(reqs.req_date,'MON-yyyy')
END
) m3 --for month 3
WHERE part.part_no = pd.part_no
AND pd.part_no = m1.part_no
AND m1.part_no = m2.part_no
AND m2.part_no = m3.part_no
AND pd.month_reqd = 'Past Due'
AND m1.month_reqd = To_Char(Add_Months(sysdate,1),'MON-yyyy')
AND m2.month_reqd = To_Char(Add_Months(sysdate,2),'MON-yyyy')
AND m3.month_reqd = To_Char(Add_Months(sysdate,3),'MON-yyyy')
ORDER BY part.part_noThe sample query above only gets 3 months worth of my tot_req_qty for each part number, which equals 3 (+1 for past due) copies of the subquery. This is how this query is "trimmed down" from my actual query; in my real query, I need to include at least 6 months (up to 12 months) of my tot_req_qty for each part number--which means 6-12 (+1) copies of the subquery...ick. I just can't imagine that being an efficient way to get this done.
Here's a table of the results I expect to get from my sample query:
PART_NO PART_DESC QTY_INSTOCK QTY_PAST_DUE QTY_THIS_MONTH QTY_NEXT_MONTH QTY_MONTH_3
part1 description 1 here 5 8 10 24 0
part2 description 2 here 10 40 0 0 0
part3 description 3 here 0 0 22 0 12Is there anything I can do differently here?
Thanks!!
Edited by: user11033437 on Feb 4, 2010 11:53 AM Fixed a typo
Edited by: user11033437 on Feb 4, 2010 12:08 PM Fixed another typoHi,
Instead of doing self-joins, you can pivot one result set.
Since it's just one result set, you can use an in-line view for it:
SELECT part.part_no
, part.part_desc
, part.qty_instock
, SUM ( CASE WHEN m.month_reqd = 'Past Due'
THEN m.tot_req_qty
ELSE 0
END
) AS qty_past_due
, SUM ( CASE WHEN m.month_reqd = To_Char(sysdate,'MON-yyyy')
THEN m.tot_req_qty
ELSE 0
END
) AS qty_this_month
, SUM ( CASE WHEN m.month_reqd = To_Char(Add_Months(sysdate, 1),'MON-yyyy')
THEN m.tot_req_qty
ELSE 0
END
) AS qty_next_month
, SUM ( CASE WHEN m.month_reqd = To_Char(Add_Months(sysdate, 2),'MON-yyyy')
THEN m.tot_req_qty
ELSE 0
END
) AS qty_month_3
FROM part
, ( -- Begin in-line view m
SELECT part_no
, SUM (req_qty) AS tot_req_qty
, month_reqd
FROM ( -- Begin in-line view rd
SELECT part_no
, req_qty
, CASE
WHEN req_date < sysdate
THEN 'Past Due'
ELSE To_Char(req_date,'MON-yyyy')
END AS month_reqd
FROM reqs
WHERE req_date < TRUNC ( ADD_MONTHS (SYSDATE, 3)
, 'MONTH'
) rd -- End in-line view rd
GROUP BY part_no
, month_reqd
) m -- End in-line view m
WHERE part.part_no = m.part_no
GROUP BY part.part_no
, part.part_desc
, part.qty_instock
ORDER BY part.part_no
;Notice I used a nested in-line view to compute month_reqd, rather than repeat the CASE expression. That's independent of the pivot: you can do either one with or without the other.
Thanks for posting the CREATE TABLE and INSERT statements! -
Is there any way we can simplify this update query ?
is there any way we can simplify this update query ? There is nothing wrong with the query ,but it looks so clumsy ...is there any other ways of doing this update like using with clause or exists or any other?
[code]
UPDATE STG_TMP_MBS_POOL s SET s.instrument_id = case when ( (select distinct iai.alternate_id from instrument_alternate_id iai, STG_TMP_MBS_POOL s where s.fi_instrument_id=iai.fi_instrument_id and iai.alternate_id_type_code IN ( 'FMR_CUSIP','CUSIP')) > 1) then (select distinct iai.alternate_id from instrument_alternate_id iai, STG_TMP_MBS_POOL s where s.fi_instrument_id=iai.fi_instrument_id and iai.alternate_id_type_code = 'FMR_CUSIP') else (select distinct iai.alternate_id from instrument_alternate_id iai, STG_TMP_MBS_POOL s where s.fi_instrument_id=iai.fi_instrument_id and iai.alternate_id_type_code IN ('FMR_CUSIP', 'CUSIP')) END;
[\code]update stg_tmp_mbs_pool s
set s.instrument_id = case when (select distinct iai.alternate_id
from instrument_alternate_id iai,
stg_tmp_mbs_pool s
where s.fi_instrument_id = iai.fi_instrument_id
and iai.alternate_id_type_code in ('FMR_CUSIP','CUSIP')
) > 1
then (select distinct iai.alternate_id
from instrument_alternate_id iai,
stg_tmp_mbs_pool s
where s.fi_instrument_id = iai.fi_instrument_id
and iai.alternate_id_type_code = 'FMR_CUSIP'
else (select distinct iai.alternate_id
from instrument_alternate_id iai,
stg_tmp_mbs_pool s
where s.fi_instrument_id = iai.fi_instrument_id
and iai.alternate_id_type_code in ('FMR_CUSIP','CUSIP')
end
Maybe
begin
update stg_tmp_mbs_pool s
set s.instrument_id = (select distinct iai.alternate_id
from instrument_alternate_id iai,
stg_tmp_mbs_pool s
where s.fi_instrument_id = iai.fi_instrument_id
and iai.alternate_id_type_code in ('FMR_CUSIP','CUSIP')
update stg_tmp_mbs_pool s
set s.instrument_id = (select distinct iai.alternate_id
from instrument_alternate_id iai,
stg_tmp_mbs_pool s
where s.fi_instrument_id = iai.fi_instrument_id
and iai.alternate_id_type_code = 'FMR_CUSIP'
where s.instrument_id > 1;
end;
Regards
Etbin -
Please help me what other way i can tune this select query..
Hello Guru,
I have a select query which retrieve data from 10 tables and around 4 tables having 2-4 Lac record and rest are having 80,000 - 1 Lac record.
It is taking around 7-8 seconds to fetch 55000 record.
I was strictly told by the client that i should not use HINTS in my query. My query is below. Please help me what other way i can tune this select query..
select
CT.CUST_ID
,CT.ROMANISED_SURNAME
,CT.SURNAME
,CT.ROMANISED_GIVEN_NAME
,CT.GIVEN_NAME
,CT.ROMANISED_MIDDLE_NAME
,CT.MIDDLE_NAME
,CT.ROMANISED_NAME_SUFFIX
,CT.NAME_SUFFIX
,CT.ROMANISED_TITLE
,CT.TITLE
,CT.ROMANISED_NAME_INITIALS
,CT.NAME_INITIALS
,CT.NAME_TEXT
,CT.CUST_JRNY_ID
,RK.REMARK_TYPE
,RK.REMARK_ID+CT.CUST_ID as REMARK_ID
,RK.REMARK_STATUS
,RK.REMARK_TEXT
,RK.HOST_ONLY_IND
,RK.SUPERVISORY_IND
,RK.CUST_COMM_IND
,RK.REMARK_SEQ
,RK.REMARK_CODE
,RK.DEFAULT_CUST_REL_IND
,RK.DEFAULT_FLIGHT_SEG_REL_IND
,RK.IATA_CODE
,RK.ICAO_CODE
,CJ.RECORD_LOCATOR "SITA_RECORD_LOCATOR"
,Cjv.Record_Locator "ORIGINATOR_RECORD_LOCATOR"
,FS.TRAVELLING_GROUP_CODE
,CG.GROUP_NAME
FROM FLIGHT_LEG FL
,CUST_FLIGHT_LEG CFL
,CUST CT
,CUST_REMARK CTR
,REMARK RK
,FLIGHT_SEG_FLIGHT_LEG FSFL
,FLIGHT_SEG FS
,CUST_JRNY CJ
,CUST_JRNY_VERSION CJV
,CUST_GROUP CG
WHERE FL.OPR_FLIGHT_NUMBER = 1--I_OPR_FLIGHT_NUMBER
and FL.HISTORY_VERSION_NUMBER = 0
and FL.DEPARTURE_STATION_CODE = 'DEL'--I_DEPARTURE_STATION_CODE
and FL.DEPARTURE_DATETIME = TO_DATE('10-DEC-2012 18.45.00', 'DD-MON-YYYY HH24.MI.SS')
and FL.OPR_SERVICE_PROVIDER_CODE= 'AI'--i_opr_service_provider_code
and FL.OPR_FLIGHT_SUFFIX = 'A'--NVL(I_OPR_FLIGHT_SUFFIX, FL.OPR_FLIGHT_SUFFIX)
AND FL.FLIGHT_LEG_ID = CFL.FLIGHT_LEG_ID
AND CFL.CUST_ID = CT.CUST_ID
AND FL.FLIGHT_LEG_ID=FSFL.FLIGHT_LEG_ID
AND FSFL.FLIGHT_SEG_ID=FS.FLIGHT_SEG_ID
AND CT.CUST_ID = CTR.CUST_ID(+)
AND CTR.REMARK_ID = RK.REMARK_ID(+)
AND FL.CUST_JRNY_ID = CJ.CUST_JRNY_ID
and CJ.CUST_JRNY_ID = CJV.CUST_JRNY_ID
AND CG.CUST_JRNY_ID(+) = CT.CUST_JRNY_ID
AND CFL.HISTORY_VERSION_NUMBER = 0
AND CT.HISTORY_VERSION_NUMBER = 0
AND NVL(CTR.HISTORY_VERSION_NUMBER,0) = 0
AND NVL(RK.HISTORY_VERSION_NUMBER,0) = 0
AND FS.HISTORY_VERSION_NUMBER = 0
AND FSFL.HISTORY_VERSION_NUMBER = 0
-- AND CJ.HISTORY_VERSION_NUMBER = 0
and CJV.VERSION_NUMBER = 0 --- Need to check
AND NVL(CG.HISTORY_VERSION_NUMBER,0) = 0
order by CT.CUST_JRNY_ID,CT.CUST_ID;
The Tables having record:
select COUNT(*) from FLIGHT_LEG -----241756
select COUNT(*) from CUST_FLIGHT_LEG---632585
select COUNT(*) from CUST---240015
select COUNT(*) from CUST_REMARK---73724
select COUNT(*) from REMARK---73654
select COUNT(*) from FLIGHT_SEG_FLIGHT_LEG---241789
select COUNT(*) from FLIGHT_SEG----260004
select COUNT(*) from CUST_JRNY----74288
select COUNT(*) from CUST_JRNY_VERSION----74477
select COUNT(*) from CUST_GROUP----55819
Thanks,
HP..Plan hash value: 3771714931
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 10239 | 2949K| | 7515 (1)| 00:01:31 | | |
| 1 | SORT ORDER BY | | 10239 | 2949K| 3160K| 7515 (1)| 00:01:31 | | |
|* 2 | HASH JOIN | | 10239 | 2949K| | 6864 (1)| 00:01:23 | | |
| 3 | PARTITION HASH ALL | | 73687 | 1079K| | 417 (1)| 00:00:06 | 1 | 512 |
|* 4 | TABLE ACCESS FULL | CUST_JRNY_VERSION | 73687 | 1079K| | 417 (1)| 00:00:06 | 1 | 512 |
|* 5 | HASH JOIN | | 10239 | 2799K| | 6445 (1)| 00:01:18 | | |
| 6 | PARTITION HASH ALL | | 73654 | 863K| | 178 (1)| 00:00:03 | 1 | 512 |
| 7 | TABLE ACCESS FULL | CUST_JRNY | 73654 | 863K| | 178 (1)| 00:00:03 | 1 | 512 |
|* 8 | FILTER | | | | | | | | |
|* 9 | HASH JOIN RIGHT OUTER | | 10239 | 2679K| | 6267 (1)| 00:01:16 | | |
| 10 | PARTITION HASH ALL | | 55315 | 756K| | 137 (1)| 00:00:02 | 1 | 512 |
| 11 | TABLE ACCESS FULL | CUST_GROUP | 55315 | 756K| | 137 (1)| 00:00:02 | 1 | 512 |
|* 12 | FILTER | | | | | | | | |
|* 13 | HASH JOIN OUTER | | 10240 | 2540K| 2056K| 6129 (1)| 00:01:14 | | |
|* 14 | FILTER | | | | | | | | |
|* 15 | HASH JOIN RIGHT OUTER | | 10242 | 1930K| | 5531 (1)| 00:01:07 | | |
| 16 | INDEX FAST FULL SCAN | CUST_REMARK_PK | 73677 | 935K| | 190 (0)| 00:00:03 | | |
|* 17 | HASH JOIN | | 10257 | 1802K| | 5339 (1)| 00:01:05 | | |
|* 18 | HASH JOIN | | 10257 | 701K| | 3516 (1)| 00:00:43 | | |
|* 19 | HASH JOIN | | 3963 | 220K| | 2476 (1)| 00:00:30 | | |
|* 20 | HASH JOIN | | 3963 | 181K| | 1300 (1)| 00:00:16 | | |
| 21 | PARTITION HASH ALL | | 3963 | 131K| | 728 (1)| 00:00:09 | 1 | 512 |
|* 22 | TABLE ACCESS FULL | FLIGHT_LEG | 3963 | 131K| | 728 (1)| 00:00:09 | 1 | 512 |
|* 23 | INDEX FAST FULL SCAN| FLIGHT_SEG_FLIGHT_LEG_PK | 240K| 3059K| | 571 (1)| 00:00:07 | | |
| 24 | PARTITION HASH ALL | | 259K| 2531K| | 1175 (1)| 00:00:15 | 1 | 512 |
|* 25 | TABLE ACCESS FULL | FLIGHT_SEG | 259K| 2531K| | 1175 (1)| 00:00:15 | 1 | 512 |
| 26 | PARTITION HASH ALL | | 631K| 8011K| | 1037 (1)| 00:00:13 | 1 | 512 |
|* 27 | TABLE ACCESS FULL | CUST_FLIGHT_LEG | 631K| 8011K| | 1037 (1)| 00:00:13 | 1 | 512 |
| 28 | PARTITION HASH ALL | | 239K| 25M| | 1822 (1)| 00:00:22 | 1 | 512 |
|* 29 | TABLE ACCESS FULL | CUST | 239K| 25M| | 1822 (1)| 00:00:22 | 1 | 512 |
| 30 | PARTITION HASH ALL | | 73623 | 4385K| | 243 (1)| 00:00:03 | 1 | 512 |
| 31 | TABLE ACCESS FULL | REMARK | 73623 | 4385K| | 243 (1)| 00:00:03 | 1 | 512 |
Predicate Information (identified by operation id):
2 - access("CJ"."CUST_JRNY_ID"="CJV"."CUST_JRNY_ID")
4 - filter("CJV"."VERSION_NUMBER"=0)
5 - access("FL"."CUST_JRNY_ID"="CJ"."CUST_JRNY_ID")
8 - filter(NVL("CG"."HISTORY_VERSION_NUMBER",0)=0)
9 - access("CG"."CUST_JRNY_ID"(+)="CT"."CUST_JRNY_ID")
12 - filter(NVL("RK"."HISTORY_VERSION_NUMBER",0)=0)
13 - access("CTR"."REMARK_ID"="RK"."REMARK_ID"(+))
14 - filter(NVL("CTR"."HISTORY_VERSION_NUMBER",0)=0)
15 - access("CT"."CUST_ID"="CTR"."CUST_ID"(+))
17 - access("CFL"."CUST_ID"="CT"."CUST_ID")
18 - access("FL"."FLIGHT_LEG_ID"="CFL"."FLIGHT_LEG_ID")
19 - access("FSFL"."FLIGHT_SEG_ID"="FS"."FLIGHT_SEG_ID")
20 - access("FL"."FLIGHT_LEG_ID"="FSFL"."FLIGHT_LEG_ID")
22 - filter("FL"."DEPARTURE_STATION_CODE"='DEL' AND "FL"."DEPARTURE_DATETIME"=TO_DATE(' 2012-12-10 18:45:00', 'syyyy-mm-dd
hh24:mi:ss') AND "FL"."OPR_SERVICE_PROVIDER_CODE"='AI' AND "FL"."OPR_FLIGHT_NUMBER"=1 AND "FL"."OPR_FLIGHT_SUFFIX"='A' AND
"FL"."HISTORY_VERSION_NUMBER"=0)
23 - filter("FSFL"."HISTORY_VERSION_NUMBER"=0)
25 - filter("FS"."HISTORY_VERSION_NUMBER"=0)
27 - filter("CFL"."HISTORY_VERSION_NUMBER"=0)
29 - filter("CT"."HISTORY_VERSION_NUMBER"=0) -
After an auto-update installation this morning a dialog box appeared asking if I want Firefox to return to its default state. I stupidly said yes and now have lost all my Speed Dial thumbnails, URLs, and saved passwords. Is there a way I can undo this to return to the way Firefox was set up and working yesterday?
Thanks for the response. Of course, I did not back up my profile, so I guess I'm going to have to recreate everything.
Maybe you are looking for
-
I just brought a movie from itunes on my iphone and im trying to sync them onto my mac so they can be in my itunes library, how do i do this?
-
How to get/read region item value in java script
Hi All, i have text item on a page. how to read the item value in java script EXAMPLE P10_RESULT IS ITEM IT HAS VALUE "38.956472,-77.447777","38.999123,-77.026184","12.951497,70.668646","17.459075,78.456888" NOW I WANT TO REFER ABOVE ITEM VALUE IN JA
-
Hi, like many people, I have some pictures that are part of several albums. As my folders- albums become more numerous, it gets harder and harder to find these pictures. There must be a way to find locations of pictures. How can I find where "photo x
-
Hi, Here is my little contribution to the forum. It's a report on EMP table with A-Z filter. You can see it in action here: http://apex.oracle.com/pls/otn/f?p=25110:1 - Create hidden item (P1_FILTER) - Create a Region SQL Report select * from emp whe
-
Got an error message while trying to upgrade to ISO4! Help!!!
I have upgraded my 3G phone with ISO4 software upgrade as requested by the system. The phone activation failed as Apple could not activate at that stage. Now my phone will not cut on, it stays on the Apple screen. I tried to restore the Iphone and it