SQL Server join to hash join and optimize plan

Copyright: copy, please indicate the source https://blog.csdn.net/weixin_39392627/article/details/87920000

env:Windows Server 2016

       SQL Server 2016 SP2

Reports have a new demand, during the test is finished mostly takes about 11 minutes, after trying to adjust the grammar or been stuck in 11 minutes. Under think of any way to say look at the implementation plan is not a warning is encountered, warning messages can be optimized to save my suggestion to do a warehouse but was rejected by the DBA.

1. Try to get some of the information to be executed DB report, and on the need to make up the index

command:

CREATE CLUSTERED INDEX [IX_CreatOn] ON [dbo].[TESTTB01]
(
[CreatOn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, 
DROP_EXISTING = OFF, ONLINE = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
DATA_COMPRESSION=PAGE) ON [PRIMARY]
GO

USE [TESTDB01]
GO
CREATE NONCLUSTERED INDEX [IX_Brand_Session_Date]
ON [dbo].[TESTTB01] ([Brand],[Session],[Date])
INCLUDE ([GId],[CreatOn],[Amount],[Revenue])
WITH(ONLINE=ON, DATA_COMPRESSION=PAGE)
GO

2. In the execution plan found inner join a warning message

message:

Operator used tempdb to spill data during execution with spill level 2 and 1 spilled thread(s)

Beginning to see this message completely not figure out what to do with TEMPDB?

Internet looking for a bit, mostly around the TEMPDB space, SQL Server memory. See more below fourth graph appears "NO STATS ()", it must be considered more a lack of statistical information.

 

 

 

 

3. Try to check tempdb, whether statistical information is insufficient or missing

command:

check cpu

SELECT cpu_count AS [Logical CPU Count], hyperthread_ratio AS [Hyperthread Ratio],
cpu_count/hyperthread_ratio AS [Physical CPU Count], 
physical_memory_kb/1024 AS [Physical Memory (MB)], affinity_type_desc, 
virtual_machine_type_desc, sqlserver_start_time
FROM sys.dm_os_sys_info WITH (NOLOCK) OPTION (RECOMPILE);

chack memory:

SELECT
(physical_memory_in_use_kb/1024) AS Memory_usedby_Sqlserver_MB,
(locked_page_allocations_kb/1024) AS Locked_pages_used_Sqlserver_MB,
(total_virtual_address_space_kb/1024) AS Total_VAS_in_MB,
process_physical_memory_low,
process_virtual_memory_low
FROM sys.dm_os_process_memory

Update statistics information, adequate space tempdb

The result is still the same.

 

4.變更inner join為inner hash join

The idea is to have just remembered watching a video tutorial talked about execution plan mentioned hash join

We review the implementation of the adjustment program warning disappeared, speed has accelerated the remaining 1 minute.

In other to use these two tables syntax was changed to inner hash join also has the effect of accelerating

5. Experience

Personally think that this is a special case, see this situation for the first time. Found on the web are the norm. Touches to try to find new ways have more sense of accomplishment.

 

 

Guess you like

Origin blog.csdn.net/weixin_39392627/article/details/87920000