Where to add index? - sql

I'm trying to figure out where I could add an index or modify an existing one to make the query go faster.
Main problem is that I cannot change the query itself, it's generated by Business Objects.
The strange thing is that if I change the WHERE clause to use another table, then the query is really fast!
The query is the following:
SELECT Year(listingperformanceindicator.date),
Month(listingperformanceindicator.date),
categoryflattened.parentname,
categoryflattened.groupname,
categoryflattened.categoryname,
Count(listingperformanceindicator.listingid),
( Count(listingperformanceindicator.listingid) ) / 30,
Sum(listingperformanceindicator.listingviews),
CASE
WHEN ( ( Count(listingperformanceindicator.listingid) ) / 30 ) = 0 THEN
0
ELSE ( Sum(listingperformanceindicator.listingviews) ) /
( ( Count(listingperformanceindicator.listingid) ) / 30 )
END
FROM listingperformanceindicator
RIGHT OUTER JOIN categorytolisting
ON ( categorytolisting.siteid = listingperformanceindicator.siteid
AND categorytolisting.listingid = listingperformanceindicator.listingid )
RIGHT OUTER JOIN categoryflattened
ON ( categoryflattened.siteid = categorytolisting.siteid
AND ( categoryflattened.parentid = categorytolisting.categoryid
OR categoryflattened.categoryid = categorytolisting.categoryid
OR categoryflattened.groupid = categorytolisting.categoryid ) )
RIGHT OUTER JOIN site ON (site.id=categoryflattened.siteId)
WHERE --listingperformanceindicator.siteid = 'DED29E78-17B0-423B-A1D1-67E2F3CA864D' --THIS IS REALLY FAST :/
site.id = 'DED29E78-17B0-423B-A1D1-67E2F3CA864D' --THIS IS REALLY SLOW! :(
AND categoryflattened.languagecode ='SE'
GROUP BY Year(listingperformanceindicator.date),
Month(listingperformanceindicator.date),
categoryflattened.parentname,
categoryflattened.groupname,
categoryflattened.categoryname
I've tried looking at the execution plan but it makes no sense to me :(
Here they are:
fast: http://imageshack.us/a/img211/5755/fastquery.png
slow: http://imageshack.us/a/img823/7227/slowquery.png
Any suggestion appreciated!
Thanks!

Related

Optimize many subqueries with same source

Does anyone have any idea how to optimize such a query?
How do you encapsulate this into one query?
,(SELECT top 1 WFD_AttDecimal2 as Nocleg
FROM WFElements INNER JOIN
WFElementDetails ON WFElements.WFD_ID = WFElementDetails.DET_WFDID
Where
WFD_STPID = #KrokSlownikUdostepnionyKraj
and dbo.ClearWFElemID (WFD_AttChoose20) = KrajDoDiety6 -- id kraju
and dbo.ClearWFElemID (WFD_AttChoose9) = #Spolka) as NoclegStawka7
,(SELECT top 1 WFD_AttDecimal3 as NoclegPrzelicznik
FROM WFElements INNER JOIN
WFElementDetails ON WFElements.WFD_ID = WFElementDetails.DET_WFDID
Where
WFD_STPID = #KrokSlownikUdostepnionyKraj
and dbo.ClearWFElemID (WFD_AttChoose20) = KrajDoDiety6 -- id kraju
and dbo.ClearWFElemID (WFD_AttChoose9) = #Spolka) as NoclegPrzelicznik7
,PelneDoby6 * (select MinDieta from #MinDieta where RodzajDelegacji = KrajZagr6) as MinIloscDiet7
from #Dieta4
You can use OUTER APPLY or CROSS APPLY and select all the necessary columns within that, you can then access all of them in the SELECT
SELECT
wfd.Nocleg as NoclegStawka7
, wfd.NoclegPrzelicznik as NoclegPrzelicznik7
, d.PelneDoby6 * (select md.MinDieta from #MinDieta md where RodzajDelegacji = KrajZagr6) as MinIloscDiet7
from #Dieta4 d
OUTER APPLY (
SELECT TOP (1)
WFD_AttDecimal2 as Nocleg,
WFD_AttDecimal3 as NoclegPrzelicznik
FROM WFElements INNER JOIN
WFElementDetails ON WFElements.WFD_ID = WFElementDetails.DET_WFDID
Where
WFD_STPID = #KrokSlownikUdostepnionyKraj
and dbo.ClearWFElemID (WFD_AttChoose20) = KrajDoDiety6 -- id kraju
and dbo.ClearWFElemID (WFD_AttChoose9) = #Spolka
) wfd;
Note also:
I suggest you rethink your usage of a scalar function ClearWFElemID as it can be slow. Use a join or a Table Valued function instead.
TOP (1) without an ORDER BY is a code-smell: you may get a different result each time.
Always specify the table alias when using subqueries, or you risk getting the wrong results.

Query fail Oracle but working in SQL Server

I have a query that runs perfectly n SQL server but in Oracle, I get the following error:
ORA-00923: FROM keyword not found where expected
SELECT
MyApps.AppConfigurationId,
MyApps.GUID,
MyApps.AppName,
MyApps.BinaryData,
MyApps.Color1,
MyApps.UserCreatorName,
MyApps.CreatedAt,
MyApps.UserUpdaterName,
MyApps.UpdatedAt,
MyApps.UserPublisherName,
MyApps.LastPublishedAt,
MyApps.AppConfigStateId,
MyApps.AppConfigStateLabel
FROM
(
SELECT
ROW_NUMBER() OVER (
PARTITION BY "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID"
ORDER BY LastestPublishInfo."PUBLISHEDAT" DESC
) AS RowNum,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" AS AppConfigurationId,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."GUID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."NAME" AS AppName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."BINARYDATA",
"OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."COLOR1",
UserCreator."NAME" AS UserCreatorName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDAT",
UserUpdater."NAME" AS UserUpdaterName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDAT",
LastestPublishInfo.PublisherName AS UserPublisherName ,
LastestPublishInfo."PUBLISHEDAT" AS LastPublishedAt,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."LABEL" AS AppConfigStateLabel
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266" JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."ID" AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."PARENTAPPCONFIGID" IS NULL AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ISACTIVE" = 1
)
JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserCreator
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserUpdater
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."COLORPALETTEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."ID")
LEFT JOIN
(
SELECT "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."APPCONFIGURATIONID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT",
UserPublisher."NAME" AS PublisherName
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16" LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserPublisher
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDBY" = UserPublisher."ID")
WHERE "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT" IS NOT NULL
AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."ISACTIVE" = 0
) LastestPublishInfo
ON (LastestPublishInfo.AppConfigurationId = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."APPCONFIGURATIONID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" )
) MyApps
WHERE MyApps.RowNum = 1
ORDER BY
MyApps.UpdatedAt DESC
I already tried to check syntax follow some articles but I'm not able to solve it.
Added " around de the aliases, tried to replace attribute names in the subquery not to match the parent but nothing worked.
Hope you could help me with this since I went to the point I have no clue about what to try next!
Cheers
Using the following web: https://rextester.com/l/oracle_online_compiler
I tryed your SQL and got the same error as you.
I then started simplifing it up to:
SELECT 1 AS RowNum
FROM blah
and still getting the same error.
I think RowNum is a reserved word in Oracle, you can not use it as a field alias.

SQL View slow when filtered. Is there a clean way to improve performance?

Let me open with:
SHOWPLAN permission denied in database 'MyDatabase'.
With that out of the way, I'll layout my situation.
So, The database I work with has a view that executes fairly quickly.
SELECT * FROM MyView
returns 32 rows in 1 second and includes a non-indexed column of values (IDs) I need to filter on.
If I filter on these IDs directly in the view:
SELECT * FROM MyView WHERE MyView.SomeId = 18
Things slow immensely and it takes 21 seconds to return the 20 rows with that ID.
As an experiment I pushed the unfiltered results into a temporary table and executed the filtered query on the the temporary table:
IF OBJECT_ID('tempdb..#TEMP_TABLE') IS NOT NULL
BEGIN
DROP TABLE #TEMP_TABLE
END
SELECT * INTO #TEMP_TABLE
FROM MyView;
SELECT *
FROM #TEMP_TABLE
WHERE #TEMP_TABLE.SomeId = 18
DROP TABLE #TEMP_TABLE
And found that it returns the filtered results far faster (roughly 1 second)
Is there a cleaner syntax or pattern that can be implemented to achieve the same performance?
UPDATE: View Definition and Description
Manually obfuscated, but I was careful so hopefully there aren't many errors. Still waiting on SHOWPLAN permissions, so Execution Plan is still pending.
The view's purpose is to provide a count of all the records that belong to a specific component (CMP.COMPONENT_ID = '100') grouped by location.
"Belonging" is determined by the record's PROC_CODE (mapped through PROC_ID) being within the CMP's inclusion range (CMP_INCs) and not in the CMP's exclusion range (CMP_EXCs).
In practice, exclusion ranges are created for individual codes (the bounds are always equal) making it sufficient to check that the code is not equal a bound.
PROC_CODES can (and don't always) have an alphabetic prefix or suffix, which makes the ISNUMERIC() comparison necessary.
Records store PROC_IDs for their PROC_CODEs, so it's necessary to convert the CMP's PROC_CODE ranges into a set of PROC_IDs for identifying which records belong to that component
The performance issue occurs when trying to filter by DEPARTMENT_ID or LOCATION_ID
[CO_RECORDS] is also a view, but if it's that deep I'm going turf this to someone with less red tape to fight through.
CREATE VIEW [ViewsSchema].[MyView] AS
WITH
CMP_INCs AS (SELECT RNG.*, COALESCE(RNG.RANGE_END, RNG.RANGE_BEG) [SAFE_END] FROM DBEngine.DBO.DB_CMP_RANGE [RNG] WHERE [RNG].COMPONENT_ID = '100'),
CMP_EXCs AS (SELECT CER.* FROM DBEngine.DBO.DB_CMP_EXC_RANGE CER WHERE CER.COMPONENT_ID = '100'),
CMP_PROC_IDs AS (
SELECT
DBEngine_ProcTable.PROC_ID [CMP_PROC_ID],
DBEngine_ProcTable.PROC_CODE [CMP_PROC_CODE],
DB_CmpTable.COMPONENT_ID [CMP_ID],
MAX(DB_CmpTable.COMPONENT_NAME) [CMP_NAME]
FROM [DBEngine].DBO.DBEngine_ProcTable DBEngine_ProcTable
LEFT JOIN CMP_INCs ON ISNUMERIC(DBEngine_ProcTable.PROC_CODE) = ISNUMERIC(CMP_INCs.RANGE_BEG)
AND(DBEngine_ProcTable.PROC_CODE = CMP_INCs.RANGE_BEG
OR DBEngine_ProcTable.PROC_CODE BETWEEN CMP_INCs.RANGE_BEG AND CMP_INCs.SAFE_END)
INNER JOIN DBEngine.DBO.DB_CmpTable ON CMP_INCs.COMPONENT_ID = DB_CmpTable.COMPONENT_ID
LEFT JOIN CMP_EXCs EXCS ON EXCS.COMPONENT_ID = DB_CmpTable.COMPONENT_ID AND EXCS.EXCL_RANGE_END = DBEngine_ProcTable.PROC_CODE
WHERE EXCS.EXCL_RANGE_BEG IS NULL
GROUP BY
DBEngine_ProcTable.PROC_ID,
DBEngine_ProcTable.PROC_CODE,
DBEngine_ProcTable.BILL_DESC,
DBEngine_ProcTable.PROC_NAME,
DB_CmpTable.COMPONENT_ID
)
SELECT
RECORD.LOCATION_NAME [LOCATION_NAME]
, RECORD.LOCATION_ID [LOCATION_ID]
, MAX(RECORD.[Department]) [DEPARTMENT]
, RECORD.[Department ID] [DEPARTMENT_ID]
, SUM(RECORD.PROCEDURE_QUANTITY) [PROCEDURE_COUNT]
FROM DBEngineCUSTOMRPT.ViewsSchema.CO_RECORDS [RECORDS]
INNER JOIN CMP_PROC_IDs [CV] ON [CV].CMP_PROC_ID = [RECORDS].PROC_ID
CROSS JOIN (SELECT DATEADD(M, DATEDIFF(M, 0,GETDATE()), 0) [FIRSTOFTHEMONTH]) VARS
WHERE [RECORDS].TYPE = 1
AND ([RECORDS].VOID_DATE IS NULL OR [RECORDS].VOID_DATE >= VARS.[FIRSTOFTHEMONTH] )
AND [RECORDS].POST_DATE < VARS.[FIRSTOFTHEMONTH]
AND [RECORDS].DOS_MONTHS_BACK = 2
GROUP BY [RECORDS].LOCATION_NAME, [RECORDS].[Department ID]
GO
Based on the swift down votes, the answer to my question is
'No, there is not a clean syntax based solution for the improved
performance, and asking for one is ignorant of the declarative nature
of SQL you simple dirty plebeian'.
From the requests for the view's definition, it's clear that performance issues in simple queries should be addressed by fixing the structure of the objects being queried ('MyView' in this case) rather than syntactical gymnastics.
For interested parties the issue was resolved by adding a Row_Number() column to the final select in the view definition, wrapping it in a CTE, and using the new column in an always true filter while selecting the original columns.
I have no idea if this is the optimal solution. It doesn't feel good to me, but it appears to be working.
CREATE VIEW [ViewsSchema].[MyView] AS
WITH
CMP_INCs AS (SELECT RNG.*, COALESCE(RNG.RANGE_END, RNG.RANGE_BEG) [SAFE_END] FROM DBEngine.DBO.DB_CMP_RANGE [RNG] WHERE [RNG].COMPONENT_ID = '100'),
CMP_EXCs AS (SELECT CER.* FROM DBEngine.DBO.DB_CMP_EXC_RANGE CER WHERE CER.COMPONENT_ID = '100'),
CMP_PROC_IDs AS (
SELECT
DBEngine_ProcTable.PROC_ID [CMP_PROC_ID],
DBEngine_ProcTable.PROC_CODE [CMP_PROC_CODE],
DB_CmpTable.COMPONENT_ID [CMP_ID],
MAX(DB_CmpTable.COMPONENT_NAME) [CMP_NAME]
FROM [DBEngine].DBO.DBEngine_ProcTable DBEngine_ProcTable
LEFT JOIN CMP_INCs ON ISNUMERIC(DBEngine_ProcTable.PROC_CODE) = ISNUMERIC(CMP_INCs.RANGE_BEG)
AND(DBEngine_ProcTable.PROC_CODE = CMP_INCs.RANGE_BEG
OR DBEngine_ProcTable.PROC_CODE BETWEEN CMP_INCs.RANGE_BEG AND CMP_INCs.SAFE_END)
INNER JOIN DBEngine.DBO.DB_CmpTable ON CMP_INCs.COMPONENT_ID = DB_CmpTable.COMPONENT_ID
LEFT JOIN CMP_EXCs EXCS ON EXCS.COMPONENT_ID = DB_CmpTable.COMPONENT_ID AND EXCS.EXCL_RANGE_END = DBEngine_ProcTable.PROC_CODE
WHERE EXCS.EXCL_RANGE_BEG IS NULL
GROUP BY
DBEngine_ProcTable.PROC_ID,
DBEngine_ProcTable.PROC_CODE,
DBEngine_ProcTable.BILL_DESC,
DBEngine_ProcTable.PROC_NAME,
DB_CmpTable.COMPONENT_ID
),
RESULTS as (
SELECT
RECORD.LOCATION_NAME [LOCATION_NAME]
, RECORD.LOCATION_ID [LOCATION_ID]
, MAX(RECORD.[Department]) [DEPARTMENT]
, RECORD.[Department ID] [DEPARTMENT_ID]
, SUM(RECORD.PROCEDURE_QUANTITY) [PROCEDURE_COUNT]
, ROW_NUMBER() OVER (ORDER BY TDL.[Medical Department ID], TDL.[BILL_AREA_ID], TDL.JP_POS_NAME) [ROW]
FROM DBEngineCUSTOMRPT.ViewsSchema.CO_RECORDS [RECORDS]
INNER JOIN CMP_PROC_IDs [CV] ON [CV].CMP_PROC_ID = [RECORDS].PROC_ID
CROSS JOIN (SELECT DATEADD(M, DATEDIFF(M, 0,GETDATE()), 0) [FIRSTOFTHEMONTH]) VARS
WHERE [RECORDS].TYPE = 1
AND ([RECORDS].VOID_DATE IS NULL OR [RECORDS].VOID_DATE >= VARS.[FIRSTOFTHEMONTH] )
AND [RECORDS].POST_DATE < VARS.[FIRSTOFTHEMONTH]
AND [RECORDS].DOS_MONTHS_BACK = 2
GROUP BY [RECORDS].LOCATION_NAME, [RECORDS].[Department ID]
)
SELECT
[LOCATION_NAME]
, [LOCATION_ID]
, [DEPARTMENT]
, [DEPARTMENT_ID]
, [PROCEDURE_COUNT]
FROM RESULTS
WHERE [ROW] > 0
GO

WHERE Clause does not accept just defined column

This Code does not work:
SELECT
(
SELECT [T_Licence].[isInstalled]
FROM [T_Licence]
WHERE [T_Licence].[System] = [T_System].[ID]
AND [T_Licence].[Software] = 750
) AS [IsInstalled] ,*
FROM [T_System]
WHERE [IsInstalled] = 1
I have to do it this way, but this makes the whole code so complicated. I really dont want that:
SELECT
(
SELECT [T_Licence].[isInstalled]
FROM [wf_subj_all].[T_Licence]
WHERE [T_Licence].[System] = [T_System].[ID]
AND [T_Licence].[Software] = 750
) AS [IsInstalled] ,*
FROM [wf_subj_it].[T_System]
WHERE
(
SELECT
(
SELECT [T_Licence].[isInstalled]
FROM [wf_subj_all].[T_Licence]
WHERE [T_Licence].[System] = [T_System].[ID]
AND [T_Licence].[Software] = 750
)
) = 1
Is there any way to do it like shown in the first code snippet?
So that the code stays somehow readeble.
thx very much
Try this one -
SELECT *
FROM wf_subj_it.T_System s
CROSS APPLY (
SELECT /*TOP(1)*/ t.isInstalled
FROM wf_subj_all.T_Licence t
WHERE t.[System] = s.ID
AND t.Software = 750
) t
WHERE t.isInstalled = 1
Just wrap the query with an outer select and it should work.
SELECT *
FROM
(
SELECT
(
SELECT [T_Licence].[isInstalled]
FROM [T_Licence]
WHERE [T_Licence].[System] = [T_System].[ID]
AND [T_Licence].[Software] = 750
) AS [IsInstalled], *
FROM [T_System]
) As tbl1
WHERE [IsInstalled] = 1
The assumptions I made for my answer:
You're trying to select the systems (table T_System) which have software with id=750 installed
The table T_License contains the installed information
There is a 1:n relation between T_System and T_License: T_License may contain 0, 1 or more records per Sytem value...
but the combination System plus Software is unique
I think this will work
SELECT l.[isInstalled], s.*
FROM [wf_subj_it].[T_System] AS s
INNER JOIN [wf_subj_it].[T_License] AS l
ON l.[System] = s.[ID]
AND l.[Software] = 750
AND l.isInstalled = 1

Sql code help in Visual Studio 2008

I'm new to visual studio 2008 and sql, little under 2 weeks now. Was hoping someone can help clean up my code. This is a large database and can pull back 6000+ pages. This code works with smaller call backs but on the larger ones hangs.
SELECT vRTCAttStatusDaily.yr_cde,
vRTCAttStatusDaily.trm_cde,
vRTCAttStatusDaily.crs_cde,
vRTCAttStatusDaily.clean_crs_cde,
vRTCAttStatusDaily.id_num,
vRTCAttStatusDaily.firstname,
vRTCAttStatusDaily.lastname,
vRTCAttStatusDaily.middlename,
name_master.first_name,
name_master.last_name,
address_master.phone,
stud_term_sum_div.udef_1a_1,
table_detail.table_desc,
vrtcreauthorizationstatus.authorization_status,
section_master.division_cde,
degree_history.major_1,
vRTCAttStatusDaily.StartDate,
vRTCAttStatusDaily.cleanclassdate,
vRTCAttStatusDaily.attend_status,
stud_term_sum_div.id_num AS id_num2
FROM {oj vRTCAttStatusDaily LEFT OUTER JOIN vrtcreauthorizationstatus ON vRTCAttStatusDaily.id_num = vrtcreauthorizationstatus.id_num LEFT OUTER JOIN section_master ON vRTCAttStatusDaily.yr_cde = section_master.yr_cde AND vRTCAttStatusDaily.trm_cde = section_master.trm_cde AND vRTCAttStatusDaily.crs_cde = section_master.crs_cde},
faculty_load_table,
name_master,
address_master,
stud_term_sum_div,
table_detail,
degree_history
WHERE ( vRTCAttStatusDaily.yr_cde = faculty_load_table.yr_cde ) and
( vRTCAttStatusDaily.trm_cde = faculty_load_table.trm_cde ) and
( vRTCAttStatusDaily.crs_cde = faculty_load_table.crs_cde ) and
( faculty_load_table.instrctr_id_num = name_master.id_num ) and
( vRTCAttStatusDaily.id_num = stud_term_sum_div.id_num ) and
( vRTCAttStatusDaily.yr_cde = stud_term_sum_div.yr_cde ) and
( vRTCAttStatusDaily.trm_cde = stud_term_sum_div.trm_cde ) and
( stud_term_sum_div.udef_1a_1 = table_detail.table_value ) and
( vRTCAttStatusDaily.id_num = address_master.id_num ) and
( vRTCAttStatusDaily.id_num = degree_history.id_num ) and
( ( faculty_load_table.lead_instrctr_flg = 'Y' ) AND
( address_master.addr_cde = '*LHP' ) AND
( table_detail.column_name = 'rtc_enroll_sts' ) AND
( vRTCAttStatusDaily.yr_cde IN (#Year)) AND
( vRTCAttStatusDaily.trm_cde IN (#Term)) AND
degree_history.cur_degree = 'Y' )
ORDER BY address_master.id_num,
vRTCAttStatusDaily.startdate,
vRTCAttStatusDaily.clean_crs_cde
Any tips would be appreciated.
Ok, it turns out the code written for vrtcattstatusdaily called on itself basically twice. Not much I could do about that, but removing faculty_table_load from the query made it work properly. It now can pull 289k rows in roughly 1.4 mins. Thanks Biggs
Here is the thread when it was moved to codereview https://codereview.stackexchange.com/questions/2783/sql-code-help-in-visual-studio-2008