Performance Issue With Join and Dates - sql-server-2012

I am trying to left join two tables which has around 870,000 records (each table).
I have to join on date and varchar fields.
I know this is the worst combination.
I tried converting Date Fields to int and also indexing all columns i am joining in both the tables.
This query is taking for ever.
I want a report for daily sliced by counties.
Is there any other way of doing this?
select
distinct
[MemberID]
, [AuthID]
, f.[Carrier]
, null As [County]
, [ServiceStatus]
, null As [ZipCode]
, [DischargedDate]
, f.[RequestType]
, [ProviderNPI]
, [ProviderName]
, [Discipline]
, f.[Market]
, f.[Phase]
, f.[DateValue]
, [UnitsAllowed]
, [CompletedVisits]
,COALESCE(f.[ForeCastVisits], '0') AS ForeCastVisits,f.[Is_holiday] AS IsHoliday
,f.[is_weekday] AS IsWeekday, f.[MyNexus_is_Holiday] AS MyNexusIsHoliday
,f.DaysOfTheWeek
, [ApprovedDate]
,DateFlag
into #utiltable
from #forecastValue f
left outer join #UtilizationData u
on f.datevalue=u.DailyDate
and f.market=u.market
and f.Phase=u.Phase
and f.Carrier=u.Carrier
and f.ServiceRequestType=u.ServiceRequestType
and f.county=u.county

Related

Query Error and Loading the result Query to new Table

Can anybody correct me with the below query where the query is going wrong ?
For the below Query i need to add where clause and get based on client names. When i execute the query i am able to view only first client data. And also i want the result to store in separate database.
select amount,[Account #]as cdn#,[CC ACCT]as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[VendorFiles] as a
inner join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),a.client)
Union all
Select [Amount],[CDN #] as cdn#,[Card_No] as CC#,[Card_Type] as CCType
from [PayPal_staging].[dbo].[VirtualFiles] as b
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
union all
select [SV10_Amt] as Amount,[Cdr_Id] as cdn#,[SV07_Last4] as CC#,[CardType] as CCType
from [PayPal_staging].[dbo].[IVRFiles] as c
left join [CUBS].[dbo].[Client] as d
on d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',' SWIS203',' SWIS600')
Can anybody help me with the above query where the query is going wrong and how to get the result for all clients in Where clause and how to store the result in separate database ?
Please write me a complete query for the above task....
It's hard to know for sure, but you have a space preceding each CLIENT ID except for the first one:
where PK_Client in ('SWMC600',' SQMC203',' SQMC600',' SBMC203',' SBMC600',
' PROV203',' PROV600',' SWRC203',' SWRC600',' SMCC203',' SMCC600',
' SWIS203',' SWIS600')
Just remove the spaces:
where PK_Client in ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600',
'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600',
'SWIS203','SWIS600')
The where clause only works for the last select. Maybe you want it this way.
SELECT *
FROM (SELECT [Amount]
, [Account #]as cdn#
, [CC ACCT]as CC#
, [CardType] as CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VendorFiles] AS a
INNER JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),a.client)
UNION ALL
SELECT [Amount]
, [CDN #] AS cdn#
, [Card_No] AS CC#
, [Card_Type] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[VirtualFiles] AS b
LEFT JOIN [CUBS].[dbo].[Client] as d
ON d.PK_Client=CONVERT(varchar(250),b.[Client_Id])
UNION ALL
SELECT [SV10_Amt] AS Amount
, [Cdr_Id] AS cdn#
, [SV07_Last4] AS CC#
, [CardType] AS CCType
, PK_Client
FROM [PayPal_staging].[dbo].[IVRFiles] AS c
LEFT JOIN [CUBS].[dbo].[Client] AS d
ON d.PK_Client=CONVERT(varchar(250),c.[SV03_Clientnbr])
) t
WHERE t.PK_Client IN ('SWMC600','SQMC203','SQMC600','SBMC203','SBMC600'
,'PROV203','PROV600','SWRC203','SWRC600','SMCC203','SMCC600'
,'SWIS203','SWIS600');

How do I change this sql statement to only select the first from each ID?

I have the below code to select from a database, however, I only want the first record for each unique ID. Is there a way to change the SQL to achieve this?
SELECT
[CARL_Property].ID
,[PrDoorNum]
,[PrAddress1]
,[PrAddress2]
,[PrAddress3]
,[PrAddress4]
,[PrPostcode]
,[PrRent]
,[PrAgreedRent]
,[PrCommence]
,[PrEnd]
,[PrAvailable]
,[PrGrossIncome]
,[PrCouncilTax]
,[PrInventoryFee]
,[PrLetFee]
,[PrReletFee]
,[PrDateWithdrawn]
,[Rent Review]
,CARL_Owners.OwForenames
,CARL_Owners.OwSurname
,CARL_Property_List.[ID]
,CARL_Property_List.[PrId]
,CARL_Property_List.[PLBedrooms]
,CARL_Property_List.[PlRooms]
,CARL_Property_List.[PlBathrooms]
,CARL_Property_List.[PlReceptions]
,CARL_Property_List.[PlDeposit]
,CARL_Tenant_Contacts.[Tenant Name]
,CARL_New_Tenants.[TnLeaseperiod]
,CARL_Property_List.[PlAdvertising]
,[CARL_Property_Memos].[PrNotes]
,[CARL_Safety].[PrGasInsp]
from dbo.CARL_Property Join dbo.[CARL_Property_Memos] on CARL_Property.ID=CARL_Property_Memos.PrID Join dbo.CARL_Owners on CARL_Owners.ID=CARL_Property.OwID Join dbo.CARL_PROPERTY_LIST ON dbo.CARL_PROPERTY.ID=dbo.CARL_PROPERTY_LIST.PrId Join dbo.[CARL_New_Tenants] ON CARL_New_Tenants.PrId=CARL_Property.ID JOIN CARL_Tenant_Contacts ON CARL_New_Tenants.ID = CARL_Tenant_Contacts.TnID Join [dbo].[CARL_Safety] On dbo.CARL_Property.ID=dbo.CARL_Safety.PrID
The result is as seen below.
Something along these lines I think is what you are looking for. Also, notice that I used aliases in your main query. It makes this a lot simpler to work with and reduces the amount of typing by a LOT.
with SortedResults as
(
SELECT
cp.ID
, ROW_NUMBER() over(partition by cp.ID order by pl.ID) as RowNum --order by whatever column defines "first"
, [PrDoorNum]
, [PrAddress1]
, [PrAddress2]
, [PrAddress3]
, [PrAddress4]
, [PrPostcode]
, [PrRent]
, [PrAgreedRent]
, [PrCommence]
, [PrEnd]
, [PrAvailable]
, [PrGrossIncome]
, [PrCouncilTax]
, [PrInventoryFee]
, [PrLetFee]
, [PrReletFee]
, [PrDateWithdrawn]
, [Rent Review]
, o.OwForenames
, o.OwSurname
, pl.[ID] as PL_ID
, pl.[PrId]
, pl.[PLBedrooms]
, pl.[PlRooms]
, pl.[PlBathrooms]
, pl.[PlReceptions]
, pl.[PlDeposit]
, tc.[Tenant Name]
, nt.[TnLeaseperiod]
, pl.[PlAdvertising]
, pm.[PrNotes]
, cs.[PrGasInsp]
from dbo.CARL_Property p
Join dbo.[CARL_Property_Memos] pm on p.ID = pm.PrID
Join dbo.CARL_Owners o on o.ID = p.OwID
Join dbo.CARL_PROPERTY_LIST pl ON p.ID = pl.PrId
Join dbo.[CARL_New_Tenants] nt ON nt.PrId = p.ID
JOIN CARL_Tenant_Contacts tc ON nt.ID = tc.TnID
Join [dbo].[CARL_Safety] cs On p.ID = cs.PrID
)
select *
from SortedResults
where RowNum = 1
order by ID

SQL Query to get only 1 instance of a record where one to many relationship exists

Ive got an SQL Query trying to get 1 record back when a 1 to many relationship exists.
SELECT dbo.BlogEntries.ID AS blog_entries_id, dbo.BlogEntries.BlogTitle, dbo.BlogEntries.BlogEntry, dbo.BlogEntries.BlogName,
dbo.BlogEntries.DateCreated AS blog_entries_datecreated, dbo.BlogEntries.inActive AS blog_entries_in_active,
dbo.BlogEntries.HtmlMetaDescription AS blog_entries_html_meta_description, dbo.BlogEntries.HtmlMetaKeywords AS blog_entries_html_meta_keywords,
dbo.BlogEntries.image1, dbo.BlogEntries.image2, dbo.BlogEntries.image3, dbo.BlogEntries.formSelector, dbo.BlogEntries.image1Alignment,
dbo.BlogEntries.image2Alignment, dbo.BlogEntries.image3Alignment, dbo.BlogEntries.blogEntryDisplayName, dbo.BlogEntries.published AS blog_entries_published,
dbo.BlogEntries.entered_by, dbo.BlogEntries.dateApproved, dbo.BlogEntries.approved_by, dbo.blog_entry_tracking.id AS blog_entry_tracking_id,
dbo.blog_entry_tracking.blog, dbo.blog_entry_tracking.blog_entry, dbo.BlogCategories.ID, dbo.BlogCategories.BlogCategoryName,
dbo.BlogCategories.BlogCategoryComments, dbo.BlogCategories.DateCreated, dbo.BlogCategories.BlogCategoryTitle, dbo.BlogCategories.BlogCategoryTemplate,
dbo.BlogCategories.inActive, dbo.BlogCategories.HtmlMetaDescription, dbo.BlogCategories.HtmlMetaKeywords, dbo.BlogCategories.entry_sort_order,
dbo.BlogCategories.per_page, dbo.BlogCategories.shorten_page_content, dbo.BlogCategories.BlogCategoryDisplayName, dbo.BlogCategories.published,
dbo.BlogCategories.blogParent
FROM dbo.BlogEntries LEFT OUTER JOIN
dbo.blog_entry_tracking ON dbo.BlogEntries.ID = dbo.blog_entry_tracking.blog_entry LEFT OUTER JOIN
dbo.BlogCategories ON dbo.blog_entry_tracking.blog = dbo.BlogCategories.ID
i have some records assigned to 2 different blogcategories, and when i query everything it returns duplicate records.
How do i only return 1 instance of a blog?
Try this one -
SELECT blog_entries_id = be.Id
, be.BlogTitle
, be.BlogEntry
, be.BlogName
, blog_entries_datecreated = be.DateCreated
, blog_entries_in_active = be.inActive
, blog_entries_html_meta_description = be.HtmlMetaDescription
, blog_entries_html_meta_keywords = be.HtmlMetaKeywords
, be.image1
, be.image2
, be.image3
, be.formSelector
, be.image1Alignment
, be.image2Alignment
, be.image3Alignment
, be.blogEntryDisplayName
, blog_entries_published = be.published
, be.entered_by
, be.dateApproved
, be.approved_by
, blog_entry_tracking_id = bet.Id
, bet.blog
, bet.blog_entry
, bc2.Id
, bc2.BlogCategoryName
, bc2.BlogCategoryComments
, bc2.DateCreated
, bc2.BlogCategoryTitle
, bc2.BlogCategoryTemplate
, bc2.inActive
, bc2.HtmlMetaDescription
, bc2.HtmlMetaKeywords
, bc2.entry_sort_order
, bc2.per_page
, bc2.shorten_page_content
, bc2.BlogCategoryDisplayName
, bc2.published
, bc2.blogParent
FROM dbo.BlogEntries be
LEFT JOIN dbo.blog_entry_tracking bet ON be.Id = bet.blog_entry
OUTER APPLY (
SELECT TOP 1 *
FROM dbo.BlogCategories bc
WHERE bet.blog = bc.Id
) bc2
Also, I would like to mention that in this case, using of aliases in the column names decreases the size of your query and makes it more convenient for understanding.
if you just need one record back, you can use
SELECT TOP 1 dbo.BlogEntries.ID AS blog_entries_id, dbo.Bl.... (same as you have now).
it is more efficient than SELECT DISTINCT
Here is a Northwind Example.
It will return only 1 row in the Order Detail table for each Order.
Use Northwind
GO
Select COUNT(*) from dbo.Orders
select COUNT(*) from dbo.[Order Details]
select * from dbo.Orders ord
join
(select ROW_NUMBER() OVER(PARTITION BY OrderID ORDER BY UnitPrice DESC) AS "MyRowID" , * from dbo.[Order Details] innerOD) derived1
on ord.OrderID = derived1.OrderID
Where
derived1.MyRowID = 1
Order by ord.OrderID

Multiple Recordsets Issue

I have the below SQL Script which is run from Excel VBA using ADO against a SQL Server 2000 database.
The issue I have is that although there is only one SELECT statement in the script, I sometimes receive three recordsets back when executing the .Open method. I say sometimes as on occassion and on other 'parallel' databases I only receive one recordset back.
I know all about the .NextRecordset() method etc. but I am trying to understand why I get three recordsets back some times and other times I only receive one. I'm shortly going to run a SQL trace to see if that throws up any ideas but as usual, any help or advice would be greatly appreciated.
SET NOCOUNT ON
DECLARE #RunDate VARCHAR(8)
SET #RunDate = CONVERT(VARCHAR(8), DATEADD(d, -1 * 1, GETDATE()), 112)
IF OBJECT_ID('tempdb..#ActiveOrders') IS NOT NULL DROP TABLE #ActiveOrders
IF OBJECT_ID('tempdb..#ApplicableOrders') IS NOT NULL DROP TABLE #ApplicableOrders
IF OBJECT_ID('tempdb..#FilterOut') IS NOT NULL DROP TABLE #FilterOut
/*Temp table created as it has a self-join in the below query */
CREATE TABLE #ActiveOrders(
order_id VARCHAR(30)
, instrument_id VARCHAR(30)
, side CHAR(1)
)
CREATE INDEX idx_ActiveOrders_orderId ON #ActiveOrders(order_id)
/*Build dataset of all orders which have had activity on the run date or are in an Open status. Ignoring Program Trades.*/
INSERT INTO #ActiveOrders
SELECT o1.order_id COLLATE Latin1_General_CI_AS
, o1.instrument_id
, o1.side
FROM orders o1
INNER JOIN desk d1 ON d1.desk_id = o1.investment_desk
INNER JOIN (SELECT o0.order_id
FROM orders o0
WHERE ((LEFT(o0.added_datetime, 8) = #RunDate
OR LEFT(o0.approved_datetime, 8) = #RunDate)
OR (LEFT(o0.added_datetime, 8) <= #RunDate
AND o0.summary_status IN (1, 2, 3, 5, 8, 9))) /*Approved, Assigned, Acknowledged, Working, Partial, WorkingPartial*/
UNION
(SELECT r0.order_id
FROM releases r0
WHERE LEFT(r0.added_datetime, 8) = #RunDate)
UNION
(SELECT e0.order_id
FROM executions e0
WHERE LEFT(e0.execution_datetime, 8) = #RunDate
OR LEFT(e0.allocated_datetime, 8) = #RunDate)
) t1 ON o1.order_id = t1.order_id
WHERE d1.location_id = 'LDEQ'
AND o1.summary_status <> 4
AND o1.list_id IS NULL /*Ignore program trades*/
/*This is now the actual dataset we are interested in.
This is everything which could be a contender for aggregation.*/
CREATE TABLE #ApplicableOrders(
order_id VARCHAR(30)
, instrument_id VARCHAR(30)
, side CHAR(1)
, approved_datetime DATETIME
, acknowledged_datetime DATETIME
, last_allocation_datetime DATETIME
, latest_status INT
, merged_orders VARCHAR(500)
, dealer VARCHAR(100)
, manager VARCHAR(100)
, limit_price FLOAT
, original_qty FLOAT
, executed_qty FLOAT
, trader_instruction TEXT
, dealer_note TEXT
)
CREATE INDEX idx_ApplicableOrders_orderId ON #ApplicableOrders(order_id)
CREATE INDEX idx_ApplicableOrders_lastAllocation ON #ApplicableOrders(last_allocation_datetime)
CREATE INDEX idx_ApplicableOrders_approved ON #ApplicableOrders(approved_datetime)
/*All orders from #ActiveOrders where there are two or more orders which are for the same instrument and in the same direction.*/
INSERT INTO #ApplicableOrders
SELECT o.order_id
, o.instrument_id
, o.side
, dbo.mglz_datetime(o.approved_datetime)
, dbo.mglz_datetime(o.ack_datetime)
, MAX(dbo.mglz_datetime(e.allocated_datetime)) "Last Allocation DateTime"
, o.summary_status
, o.merged_orders
, o.ack_id
, o.approver_id
, o.limit_price
, o.original_qty
, o.executed_qty_at
, CONVERT(VARCHAR(900), o.trader_instruction)
, CONVERT(VARCHAR(900), o.dealer_note)
FROM orders o
INNER JOIN #ActiveOrders t ON o.order_id = t.order_id COLLATE Latin1_General_CI_AS
INNER JOIN #ActiveOrders s ON s.order_id <> o.order_id COLLATE Latin1_General_CI_AS
AND s.instrument_id = o.instrument_id COLLATE Latin1_General_CI_AS
AND s.side = o.side COLLATE Latin1_General_CI_AS
LEFT JOIN executions e ON e.order_id = o.order_id
GROUP BY o.order_id
, o.instrument_id
, o.side
, o.approved_datetime
, o.ack_datetime
, o.summary_status
, o.merged_orders
, o.ack_id
, o.approver_id
, o.limit_price
, o.original_qty
, o.executed_qty_at
, CONVERT(VARCHAR(900), o.trader_instruction)
, CONVERT(VARCHAR(900), o.dealer_note)
/*Filter out any orders where Order2.Approved_Date > Order1.Last_Release_Date AND Order1.Is_Complete
Order1 is defined as the order which was approved first.*/
SELECT t1.*
INTO #FilterOut
FROM #ApplicableOrders t1
WHERE EXISTS (SELECT 1
FROM
(SELECT order2.order_id
FROM (SELECT b.order_id
, b.instrument_id
, b.side
, b.approved_datetime
, b.last_allocation_datetime
, b.latest_status
, b.executed_qty
, b.original_qty
FROM #ApplicableOrders b
WHERE b.approved_datetime = (SELECT MIN(b1.approved_datetime) FirstApproval
FROM #ApplicableOrders b1
WHERE b1.instrument_id = b.instrument_id
AND b1.side = b.side)
) order1
INNER JOIN
(SELECT c.order_id
, c.instrument_id
, c.side
, c.approved_datetime
FROM #ApplicableOrders c
WHERE c.approved_datetime > (SELECT MIN(c1.approved_datetime) FirstApproval
FROM #ApplicableOrders c1
WHERE c1.instrument_id = c.instrument_id
AND c1.side = c.side)
) order2
ON order1.instrument_id = order2.instrument_id
AND order1.side = order2.side
AND order2.approved_datetime > order1.last_allocation_datetime
AND (order1.latest_status = 6 OR order1.executed_qty = order1.original_qty)) filter1
WHERE t1.order_id = filter1.order_id)
/*Filter out any orders where Order2.Acknowledged_Date > Order1.Last_Allocation_Date.*/
INSERT INTO #FilterOut
SELECT t1.*
FROM #ApplicableOrders t1
WHERE EXISTS (SELECT 1
FROM
(SELECT order2.order_id
FROM (SELECT b.order_id
, b.instrument_id
, b.side
, b.approved_datetime
, b.last_allocation_datetime
FROM #ApplicableOrders b
WHERE b.approved_datetime = (SELECT MIN(b1.approved_datetime) FirstApproval
FROM #ApplicableOrders b1
WHERE b1.instrument_id = b.instrument_id
AND b1.side = b.side)
) order1
INNER JOIN
(SELECT c.order_id
, c.instrument_id
, c.side
, c.approved_datetime
, c.acknowledged_datetime
FROM #ApplicableOrders c
WHERE c.approved_datetime > (SELECT MIN(c1.approved_datetime) FirstApproval
FROM #ApplicableOrders c1
WHERE c1.instrument_id = c.instrument_id
AND c1.side = c.side)
) order2
ON order1.instrument_id = order2.instrument_id
AND order1.side = order2.side
AND order2.acknowledged_datetime > order1.last_allocation_datetime) filter2
WHERE t1.order_id = filter2.order_id)
AND NOT EXISTS (SELECT 1
FROM #FilterOut a1
WHERE a1.order_id = t1.order_id)
/*Filter any 'single' orders. I.e. all 'matching' orders have been excluded so the instrument/direction combination is not applicable for Aggregation.*/
INSERT INTO #FilterOut
SELECT t1.*
FROM #ApplicableOrders t1 INNER JOIN (SELECT DISTINCT t.instrument_id
, t.side
FROM #ApplicableOrders t
INNER JOIN #FilterOut a ON t.instrument_id = a.instrument_id
AND t.side = a.side
GROUP BY t.instrument_id
, t.side
HAVING COUNT(t.instrument_id) > 1) t2 ON t1.instrument_id = t2.instrument_id
AND t1.side = t2.side
WHERE NOT EXISTS (SELECT 1
FROM #FilterOut a1
WHERE a1.order_id = t1.order_id)
/*Final Report*/
/*A list of all orders where aggregation could have possibly occurred but didn't.*/
SELECT t1.order_id "Order ID"
, i.name "Name"
, t1.side "B/S"
, userDealer.short_name "Dlr"
, userManager.short_name "FM"
, t1.limit_price "Limit"
, t1.approved_datetime "Order Approved"
, t1.acknowledged_datetime "Order Acknowledged"
, t1.last_allocation_datetime "Last Execution"
, t1.merged_orders "Merged Orders"
, m.description "Status"
, t1.dealer_note "Dealer Note"
, t1.trader_instruction "Trader Instruction"
FROM #ApplicableOrders t1
INNER JOIN instrument i ON t1.instrument_id = i.instrument_id COLLATE Latin1_General_CI_AS
INNER JOIN mnemonics m ON t1.latest_status = m.value AND m.attribute = 'order_summary_status'
LEFT JOIN users userDealer ON userDealer.user_id = t1.dealer COLLATE Latin1_General_CI_AS
LEFT JOIN users userManager ON userManager.user_id = t1.manager COLLATE Latin1_General_CI_AS
WHERE NOT EXISTS (SELECT 1
FROM #FilterOut t2
WHERE t1.order_id = t2.order_id)
ORDER BY t1.name
, t1.side
, t1.approved_datetime
IF OBJECT_ID('tempdb..#ActiveOrders') IS NOT NULL DROP TABLE #ActiveOrders
IF OBJECT_ID('tempdb..#ApplicableOrders') IS NOT NULL DROP TABLE #ApplicableOrders
IF OBJECT_ID('tempdb..#FilterOut') IS NOT NULL DROP TABLE #FilterOut
I would add an isotope column (dummy column) to the end of your temp tables so that when you see the three recordsets you can interrogate the columns names to see where they're coming from. And what happens when you take the SQL from profile and run directly in SSMS? Do you get just one resultset?
I finally found the issue and thought I would report back. The problem was with NULL values in two of the Aggregation Functions in the script. The reason this only occurred occasionally is because the data was constantly changing.
Although SQL Server handles the issue 'silently' it does produce warnings which ADO sees as recordsets although in my case (might be every case) these recordsets are closed and so impossible to actually see what they contain or what produced them.
Instead of re-writing the SQL script with ISNULL() logic, I have opted simply to include the
SET ANSI_WARNINGS OFF
statement at the top of the script. This prevents the warnings from being reported and thus ADO does not produce these additional, unwanted recordsets.
Disappointed I didn't spot this earlier but at least I've learnt something new. :)

SQL Server view with a 'select where x is not null' takes ages to complete

I have a complex view, which is described here: View of multiple tables. Need to remove "doubles" defined by 1 table
I used a Cross Apply in it, and the code is this: (please do check the url above to understand the view)
SELECT dbo.InstellingGegevens.INST_SUBTYPE
, dbo.InstellingGegevens.INST_BRON
, dbo.InstellingGegevens.INST_INSTELLINGSNUMMER
, dbo.InstellingGegevens.INST_NAAM
, dbo.InstellingGegevens.INST_KORTENAAM
, dbo.InstellingGegevens.INST_VESTIGINGSNAAM
, dbo.InstellingGegevens.INST_ROEPNAAM
, dbo.InstellingGegevens.INST_STATUUT
, dbo.InstellingGegevens.ONDERWIJSNIVEAU_REF
, dbo.InstellingGegevens.ONDERWIJSSOORT_REF
, dbo.InstellingGegevens.DATUM_TOT
, dbo.InstellingGegevens.DATUM_VAN
, dbo.InstellingGegevens.VERBOND_REF
, dbo.InstellingGegevens.VSKO_LID
, dbo.InstellingGegevens.NET_REF
, dbo.Instellingen.Inst_ID
, dbo.Instellingen.INST_TYPE
, dbo.Instellingen.INST_REF
, dbo.Instellingen.INST_LOC_REF
, dbo.Instellingen.INST_LOCNR
, dbo.Instellingen.Opt_KalStandaard
, dbo.InstellingTelecom.INST_TEL
, dbo.InstellingTelecom.INST_FAX
, dbo.InstellingTelecom.INST_EMAIL
, dbo.InstellingTelecom.INST_WEB
, dbo.InstellingAdressen.SOORT
, dbo.InstellingAdressen.STRAAT
, dbo.InstellingAdressen.POSTCODE
, dbo.InstellingAdressen.GEMEENTE
, dbo.InstellingAdressen.GEM_REF
, dbo.InstellingAdressen.FUSIEGEM_REF
, dbo.InstellingAdressen.FUSIEGEM
, dbo.InstellingAdressen.ALFA_G
, dbo.InstellingAdressen.PROVINCIE
, dbo.InstellingAdressen.BISDOM
, dbo.InstellingAdressen.ARRONDISSEMENT
, dbo.InstellingAdressen.GEWEST
, dbo.InstellingContPersDirecteurs.AANSPREKING
, dbo.InstellingContPersDirecteurs.CONTACTPERSOON
, dbo.InstellingContPersDirecteurs.FUNCTIE
, InstellingLogin.Inst_Gebruikersnaam
, InstellingLogin.Inst_Concode
, InstellingLogin.Inst_DirCode
, InstellingLogin.DOSSNR
, InstellingLogin.Instelling_ID
FROM dbo.InstellingGegevens
RIGHT OUTER JOIN dbo.Instellingen
ON dbo.InstellingGegevens.INST_TYPE = dbo.Instellingen.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.Instellingen.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.Instellingen.INST_LOC_REF
AND dbo.InstellingGegevens.INST_LOCNR = dbo.Instellingen.INST_LOCNR
LEFT OUTER JOIN dbo.InstellingTelecom
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingTelecom.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingTelecom.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingTelecom.INST_LOC_REF
LEFT OUTER JOIN dbo.InstellingAdressen
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingAdressen.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingAdressen.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingAdressen.INST_LOC_REF
LEFT OUTER JOIN dbo.InstellingContPersDirecteurs
ON dbo.InstellingGegevens.INST_TYPE = dbo.InstellingContPersDirecteurs.INST_TYPE
AND dbo.InstellingGegevens.INST_REF = dbo.InstellingContPersDirecteurs.INST_REF
AND dbo.InstellingGegevens.INST_LOC_REF = dbo.InstellingContPersDirecteurs.INST_LOC_REF
CROSS APPLY
(SELECT TOP (1) *
FROM InstellingLogin AS il
WHERE Instellingen.INST_LOC_REF = il.Inst_Loc_REF
AND Instellingen.INST_LOCNR = il.Inst_Loc_Nr
AND Instellingen.INST_REF = il.Inst_InstellingIKON_REF
AND Instellingen.INST_TYPE = il.Inst_InstellingIKONType
ORDER BY CASE
WHEN il.datum_tot IS NULL
THEN 0 ELSE 1
END
, il.datum_tot DESC) InstellingLogin
This view returns me about 5.5k rows, in about 1s. This is fast!
However!
When I call this view with a where clause:
SELECT *
FROM [Tink].[dbo].[InstellingAlleDetails]
where gemeente is not null and (DATUM_TOT is null or DATUM_TOT > GETDATE())
order by GEMEENTE, POSTCODE,STRAAT, INST_NAAM
it takes 1min 20s to return all rows.
When I drop the gemeente is not null part, it takes again 1s.
Gemeente is a varchar(255). I also tried it with Inst_Naam is not null and that also took about 1min 30s.
Why does this is not null take so much time? And more importantly: how do I fix this?
I don't know why. Probably SQL Server comes up with a query plan that is not so good.
You could try to first run the query without gemeente is not null and put the result in a temp table and then query the temp table with gemeente is not null.
select *
into #TempTable
from YourView
select *
from #TempTable
where gemeente is not null
drop table #TempTable
First check the execution plans on both the query with and without that is not null and see the differences.
BTW are any of these joins to other views? That can cause tremendous performance problems.