Note: The stored procedure in this post works with SQL Server 2016 SP2++ and on planXML for plans NOT using the Legacy CE. There will be a future post with a version using trace flag 8666 style planXML, which can be used prior to SQL Server 2016 SP2 or with the Legacy CE.
DBCC CLONEDATABASE is a good thing.
But sometimes its too broadly scoped to be useful, especially between organizations.
What if I need to troubleshoot a single query from (a) known, shared schema(s) when there are 20,000 + statistics and some may contain sensitive data in the range_hi key values?
Below is a tool that can help, provided the system is SQL Server 2016 SP2 or higher, the Query Store is enabled and captured the relevant plan, and the Legacy CE was NOT used to compile the plan. This stored procedure in that case takes the plan_id and retrieves from the Query Store and SHOW_STATISTICS commands what is needed to generate UPDATE STATISTICS statements including stats_stream, rowcount, and pagecount.
The basic mining of optimizer stats from Query Store for 2016 SP2 ++ can be seen here...
Harvesting SQL Server optimizer stats detail from Query Plan XML: Part II SQL Server 2016 SP2++