I have the following query:
SELECT [t].[Key], SUM([t].[ValueA]), SUM([t].[ValueB])
FROM [MyTable] AS [t]
GROUP BY [t].[Key]
This table had an nvarchar column with value lengths of 2000 characters (I think this shoot up the I/O cost). As I didn't need this column in the query, I created a new auxiliary table, and moved this heavy column to make the query faster and update statistics runing:
Update STATISTICS HCJConsumos WITH FULLSCAN
But the query still takes 1 minute to run and the I/O Cost is still very high 383
This improved the execution time but the I/O cost was still high. Then, I tried Exporting Data-tier Applicattion and importing it, when I estimated the Execution Plan again the I/O cost dropped to 7.
And this improved the performance much more, it takes 6 seconds.
Is it possible to make the database realize that this cost is overestimated without exporting and importing the database, as this makes the query run much faster. Due it is the same data, this must be affecting the execution plan and performance of the query.
CREATE TABLE Aux( PkId int, FkId int, HeavyColumn nvarchar(max)); INSERT INTO Aux(...) SELECT ... FROM MyTable
Then I added a migration to remove the heavy column from MyTable and create the OfficialAux table, to keep this OfficialAux table in the known by the model. And later executed another Insert statement manually to move the data from Aux to OfficialAux (I didn't do this in the migration since I got a Timeout) and remove Aux table.