CodexBloom - Programming Q&A Platform

SQL Server: Performance issues with large table partitioning and query execution plans

πŸ‘€ Views: 1312 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-28
sql-server performance partitioning SQL

I'm updating my dependencies and I'm attempting to set up I'm currently working with a large SQL Server database (version 2019) that has a table with over 10 million rows for storing sales data. To optimize performance, I implemented partitioning based on the year of the sale. However, I'm experiencing significant delays when querying the table with recent partitions due to the execution plan that SQL Server chooses. For example, when I run the following query: ```sql SELECT * FROM SalesData WHERE SaleDate >= '2023-01-01' AND SaleDate < '2023-12-31'; ``` I noticed that SQL Server isn't using the partition elimination as expected. Instead, it processes all partitions, which is evident from the estimated execution plan showing a high cost for reading rows across all partitions. I have verified that the partition function and scheme are set up correctly: ```sql CREATE PARTITION FUNCTION SalesPF (DATE) AS RANGE RIGHT FOR VALUES ('2023-01-01', '2024-01-01'); CREATE PARTITION SCHEME SalesPS AS PARTITION SalesPF TO ([PRIMARY]); CREATE TABLE SalesData ( ID INT PRIMARY KEY, SaleDate DATE, Amount DECIMAL(10, 2) ) ON SalesPS(SaleDate); ``` I also updated my statistics recently to ensure they are current, but the performance remains suboptimal. I tried using `OPTION (RECOMPILE)` in my query, which improved the performance slightly, but it’s not a sustainable solution. Can anyone provide insights into why SQL Server might not be utilizing partition elimination effectively in this case or suggest best practices for optimizing queries against large partitioned tables? Any help would be greatly appreciated! I'm working on a service that needs to handle this. Is this even possible?