I have a massive problem with 100% CPU spikes because of a bad execution plan used by a specific query. I spend weeks now solve with by my own.
My sample DB contains 3 simplified tables.
CREATE TABLE [model].[DataLogger]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [ProjectID] [bigint] NULL, CONSTRAINT [PK_DataLogger] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
CREATE TABLE [model].[Inverter]( [ID] [bigint] IDENTITY(1,1) NOT NULL, [SerialNumber] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Inverter] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY], CONSTRAINT [UK_Inverter] UNIQUE NONCLUSTERED ( [DataLoggerID] ASC, [SerialNumber] ASC )WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] ALTER TABLE [model].[Inverter] WITH CHECK ADD CONSTRAINT [FK_Inverter_DataLogger] FOREIGN KEY([DataLoggerID]) REFERENCES [model].[DataLogger] ([ID])
CREATE TABLE [data].[InverterData]( [InverterID] [bigint] NOT NULL, [Timestamp] [datetime] NOT NULL, [DayYield] [decimal](18, 2) NULL, CONSTRAINT [PK_InverterData] PRIMARY KEY CLUSTERED ( [InverterID] ASC, [Timestamp] ASC )WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) )
Stats and Maintanance
[InverterData] table has around 120,000,000 rows.
All indexer are defragmentated and all stat rebuild/reorganized as needed on a daily/weekly turn.
The query is Entity Framework generated and also simple. But i runes 1,000 times per minute and performance is essential.
SELECT [Extent1].[InverterID] AS [InverterID], [Extent1].[DayYield] AS [DayYield] FROM [data].[InverterDayData] AS [Extent1] INNER JOIN [model].[Inverter] AS [Extent2] ON [Extent1].[InverterID] = [Extent2].[ID] INNER JOIN [model].[DataLogger] AS [Extent3] ON [Extent2].[DataLoggerID] = [Extent3].[ID] WHERE ([Extent3].[ProjectID] = @p__linq__0) AND ([Extent1].[Date] = @p__linq__1) OPTION (MAXDOP 1)
MAXDOP 1 hint is for another problem with a slow paralel plan.
The “good” plan
Over the 90% of time the used plan is lightning fast and looks like this:
Over the day the good plan randomly changed to a bad and slow plan.
The “bad” plan is used for 10-60min and then changed back to the “good” plan. The “bad” plan spike the CPU up to permanent 100%.
This is how it looks:
What I try so far
My first thought was the
Hash Match is the bad boy. So I modified the query with a new hint.
...Extent1].[Date] = @p__linq__1) OPTION (MAXDOP 1, LOOP JOIN)
LOOP JOIN should force to use
Nested Loop instant of
The result is that the 90% plan looks like before. But the plan also changed randomly to a bad one.
The “bad” plan now looks like this (table loop order changed):
The CPU also peeks to 100% during the “new bad” plan.
It comes to my mind to force the “good” plan. But I don’t knwo if this is a good idea.
Please help me!
Update 1 – related to @James question
here are both plans:
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!