Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How can I speed up this date-range intersect query?

$
0
0

In the table below I have 270,000+ rows.

There is only one value for Customer, but there will be more later.

There are only 42 values for ProductTag, but most are one of only 10 different ones.

The update query runs for hours.

Can this be done better, faster?

CREATE TABLE [dbo].[EntryPairs](
	[Customer] [varchar](50) NULL,
	[ProductTag] [varchar](5) NULL,
	[CheckOut_Time] [datetime] NULL,
	[CheckIn_Time] [datetime] NULL,
	[ConcurrentCheckOuts] [int] NULL
) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [IX_EntryPairs_A] ON [dbo].[EntryPairs]
(
	[Customer] ASC,
	[ProductTag] ASC,
	[CheckOut_Time] ASC,
	[CheckIn_Time] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

-- Calculate ConcurrentCheckOuts
UPDATE EntryPairs 
	SET ConcurrentCheckOuts = (	SELECT COUNT(*) AS CustomerConcurrentUsage
										FROM EntryPairs ConcurrentUsages
										WHERE ConcurrentUsages.Customer = EntryPairs.Customer
												AND ConcurrentUsages.ProductTag = EntryPairs.ProductTag
												AND ((ConcurrentUsages.CheckOut_Time >= EntryPairs.CheckOut_Time AND ConcurrentUsages.CheckOut_Time <= EntryPairs.CheckIn_Time)
														OR (ConcurrentUsages.CheckIn_Time >= EntryPairs.CheckOut_Time AND ConcurrentUsages.CheckIn_Time <= EntryPairs.CheckIn_Time)
													)
									)
	WHERE Customer = @p_Customer


Kipp Woodard


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>