Https://www.cnblogs.com/srsrd/p/6962982.html
Method One:
Method Two:
Use the following statement to find out what statements account for the highest memory and optimize for statements that account for high memory
SELECT SS. Sum_execution_count,
T.text,
Ss. Sum_total_elapsed_time as ' Sum time ',
Ss. Sum_total_worker_time as ' execution time ',
Ss. Sum_total_logical_reads as ' sum logical readings ',
Ss. Sum_total_logical_writes as ' Sum logical write '
From (SELECT S.plan_handle,
SUM (S.execution_count) Sum_execution_count,
SUM (s.total_elapsed_time) Sum_total_elapsed_time,
SUM (s.total_worker_time) Sum_total_worker_time,
SUM (s.total_logical_reads) sum_total_logical_reads,
SUM (s.total_logical_writes) sum_total_logical_writes
From SYS. Dm_exec_query_stats S
GROUP by S.plan_handle
) as SS
Cross APPLY sys.dm_exec_sql_text (SS. plan_handle) T
ORDER by Sum_total_logical_reads DESC
SQL Server memory consumption high processing mode