Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query runs fast in SQL Developer but slow in SSRS

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
0
0
US
Just as the subject of this thread suggests, I have a query that executes quickly in Oracle SQL Developer (OSD), but runs forever in SSRS. The query usually takes between 0.5 - 2.5 seconds to run in OSD. However, I have let the query run in SSRS for probably around 20 minutes until eventually it appeared to time out.

Any ideas for a solution would be greatly appreciated. If necessary, I can post the query, but it is rather long. It does contain a temporary table (WITH clause).

Thanks again!
 
Maybe you can post the SQL for us to analyze?
~Brett

--------------------------
Web/.net Programmer & DBA
Central PA
 
The query was originally written with temp tables (WITH clauses) to break the query down into sections for both speed and readability.

We were trying to minimize the number of temp tables used. Also, we were trying to make the query work in SSRS, so we combined a couple of the temp table queries into one (so the second version has a coulpe of repetitive sub-queries).

The problem we are having is simply getting the query to run quickly in SSRS, as it is doing in OSD. If anyone happens to know a way to also clean up the SQL / make it more efficient in addition to solving the main problem, be our guest!

Thank you so much for your time and efforts!!! :) Below are both versions:






------------------ FIRST VERSION --------------------

WITH valid_dates AS (
--Get all weekdays that occurred in the last 100 days
SELECT TRUNC(sysdate) - level AS dates
FROM dual
WHERE TO_CHAR(TRUNC(sysdate) - level, 'D') BETWEEN 2 AND 6
CONNECT BY level <= 100
),

corrected_backup_alerts AS (
--Get only backup alerts and convert their dates from seconds since Jan 1st, 1970 to a date format. Correct applicable date values (alerts that have datetime values between 12:00 AM and 7:00 AM actually occurred the day before)
SELECT (CASE
WHEN (TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) >= 0 AND
TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) < 7)
THEN
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400 - 1)
ELSE
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400)
END) alert_date,
hostname
FROM tec_t_evt_rep
WHERE class = 'XMPAlert' AND
sub_source = 'Backup Exec' AND
((msg LIKE 'Backup Exec - Insert Tape%' OR msg LIKE 'Backup Exec - Job Cancelled%') OR
(long_msg LIKE 'Backup Exec - Insert Tape%' OR long_msg LIKE 'Backup Exec - Job Cancelled%'))
),

valid_alerts AS (
--For hostnames that had a backup alert occur on the most recent weekday (from inner query), get all backup alerts that occurred in the last 100 days (excluding weekends)
SELECT alert_date,
hostname
FROM corrected_backup_alerts c_b_a INNER JOIN valid_dates ON valid_dates.dates = c_b_a.alert_date
WHERE hostname IN (--Choose records where the corrected alert dates were the most recent weekday
SELECT DISTINCT hostname
FROM corrected_backup_alerts
WHERE alert_date = (--Get the most recent weekday
SELECT MAX(dates)
FROM valid_dates)
)
)

--Get a count of all alerts (only for a count of 3 or more) whose alert_date is greater than the latest date where a break in consecutive dates occurred (in other words, get all most recent consecutive alert occurrences)
SELECT hostname, COUNT(DISTINCT alert_date) AS num_failures
FROM valid_alerts v_a_outermost
WHERE alert_date > (--Get the latest alert_date (other than the most recent weekday) where there exists no records such that that record's alert_date is not consecutively higher than that sought alert_date
SELECT MAX(alert_date) AS max_nonconsec_alert
FROM valid_alerts v_a_outer
WHERE NOT EXISTS (SELECT 'x'
FROM valid_alerts v_a_inner
WHERE v_a_inner.hostname = v_a_outer.hostname AND
(((TO_CHAR(v_a_outer.alert_date, 'D') BETWEEN 2 AND 5) AND
(v_a_inner.alert_date = v_a_outer.alert_date + 1)) OR
((TO_CHAR(v_a_outer.alert_date, 'D') = 6) AND
(v_a_inner.alert_date = v_a_outer.alert_date + 3)))) AND
v_a_outermost.hostname = v_a_outer.hostname AND
v_a_outer.alert_date <> (--Get the most recent weekday
SELECT MAX(dates)
FROM valid_dates)
)
GROUP BY hostname
HAVING COUNT(DISTINCT alert_date) >= 3
ORDER BY hostname ASC

------------------------------------------------------






------------------ SECOND VERSION --------------------


WITH valid_alerts AS (
--For hostnames that had a backup alert occur on the most recent weekday (from inner query in the WHERE clause), get all backup alerts that occurred in the last 100 days (excluding weekends)
SELECT alert_date,
hostname
FROM (--Get all backup alerts that have occurred for all weekdays in a 100 day timespan
SELECT (CASE
WHEN (TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) >= 0 AND
TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) < 7)
THEN
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400 - 1)
ELSE
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400)
END) alert_date,
hostname
FROM tec_t_evt_rep
WHERE class = 'XMPAlert' AND
sub_source = 'Backup Exec' AND
((msg LIKE 'Backup Exec - Insert Tape%' OR msg LIKE 'Backup Exec - Job Cancelled%') OR
(long_msg LIKE 'Backup Exec - Insert Tape%' OR long_msg LIKE 'Backup Exec - Job Cancelled%'))
) c_b_a INNER JOIN (--Get all weekdays that occurred in the last 100 days
SELECT TRUNC(sysdate) - level AS dates
FROM dual
WHERE TO_CHAR(TRUNC(sysdate) - level, 'D') BETWEEN 2 AND 6
CONNECT BY level <= 100) valid_dates ON valid_dates.dates = c_b_a.alert_date
WHERE hostname IN (--Get hostnames that had a backup alert occur on the most recent weekday
SELECT DISTINCT hostname
FROM (--Get only backup alerts and convert their dates from seconds since Jan 1st, 1970 to a date format. Correct applicable date values (alerts that have datetime values between 12:00 AM and 7:00 AM actually occurred the day before)
SELECT (CASE
WHEN (TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) >= 0 AND
TO_NUMBER(TO_CHAR(TO_DATE('19700101','YYYYMMDD') + date_reception/86400,'HH24')) < 7)
THEN
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400 - 1)
ELSE
TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400)
END) alert_date,
hostname
FROM tec_t_evt_rep
WHERE class = 'XMPAlert' AND
sub_source = 'Backup Exec' AND
((msg LIKE 'Backup Exec - Insert Tape%' OR msg LIKE 'Backup Exec - Job Cancelled%') OR
(long_msg LIKE 'Backup Exec - Insert Tape%' OR long_msg LIKE 'Backup Exec - Job Cancelled%'))
)
WHERE alert_date = (--Get the most recent weekday
SELECT MAX(TRUNC(sysdate) - level)
FROM dual
WHERE TO_CHAR(TRUNC(sysdate) - level, 'D') BETWEEN 2 AND 6
CONNECT BY level <= 3)
)
)

--Get a count of all alerts (only for a count of 3 or more) whose alert_date is greater than the latest date where a break in consecutive dates occurred (in other words, get all most recent consecutive alert occurrences)
SELECT hostname, COUNT(DISTINCT alert_date) AS num_failures
FROM valid_alerts v_a_outermost
WHERE alert_date > (--Get the latest alert_date (other than the most recent weekday) where there exists no records such that that record's alert_date is not consecutively higher than that sought alert_date
SELECT MAX(alert_date) AS max_nonconsec_alert
FROM valid_alerts v_a_outer
WHERE NOT EXISTS (SELECT 'x'
FROM valid_alerts v_a_inner
WHERE v_a_inner.hostname = v_a_outer.hostname AND
(((TO_CHAR(v_a_outer.alert_date, 'D') BETWEEN 2 AND 5) AND
(v_a_inner.alert_date = v_a_outer.alert_date + 1)) OR
((TO_CHAR(v_a_outer.alert_date, 'D') = 6) AND
(v_a_inner.alert_date = v_a_outer.alert_date + 3)))) AND
v_a_outermost.hostname = v_a_outer.hostname AND
v_a_outer.alert_date <> (--Get the most recent weekday
SELECT MAX(TRUNC(sysdate) - level)
FROM dual
WHERE TO_CHAR(TRUNC(sysdate) - level, 'D') BETWEEN 2 AND 6
CONNECT BY level <= 3)
)
GROUP BY hostname
HAVING COUNT(DISTINCT alert_date) >= 3
ORDER BY hostname ASC

------------------------------------------------------




In case you are wondering what may be the number of records we are dealing with, the last time I ran a query against the database there were less than 2400 backup failures (although there were many more failures that are not considered to be "backup failures").


 

I would suggest copying the queries above into notepad or some other editor. It's much easier to read / shorter to scroll through then.
 

We never could figure out why the query took so long to run in SSRS, so we re-wrote it. This way is a lot more slick, anyhow :)


SELECT hostname, COUNT(DISTINCT alert_date) AS num_failures
FROM (SELECT alert_date, hostname, DENSE_RANK() OVER (PARTITION BY hostname ORDER BY alert_date DESC) AS rank_order
FROM (SELECT TRUNC(TO_DATE('19700101','YYYYMMDD') + date_reception/86400 - 7/24) AS alert_date,
hostname
FROM tec.tec_t_evt_rep
WHERE class = 'XMPAlert' AND
sub_source = 'Backup Exec' AND
((msg LIKE 'Backup Exec - Insert Tape%' OR msg LIKE 'Backup Exec - Job Cancelled%') OR
(long_msg LIKE 'Backup Exec - Insert Tape%' OR long_msg LIKE 'Backup Exec - Job Cancelled%'))
)
WHERE alert_date < TRUNC(sysdate) AND
TO_CHAR(alert_date, 'd') BETWEEN 2 AND 6
) alerts

INNER JOIN

(SELECT TRUNC(sysdate) - level AS dates, RANK() OVER (ORDER BY TRUNC(sysdate) - level DESC) AS date_order
FROM dual
WHERE TO_CHAR(TRUNC(sysdate) - level, 'D') BETWEEN 2 AND 6
CONNECT BY level <= 100) weekdates

ON alerts.alert_date = weekdates.dates AND
date_order = rank_order
GROUP BY hostname
HAVING COUNT(DISTINCT alert_date) >= 3



This query runs within 1 second, no matter what tool is used.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top