CodexBloom - Programming Q&A Platform

T-SQL Performance implementing Cross Join in SQL Server 2022

๐Ÿ‘€ Views: 1678 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-07-17
sql-server performance cross-join T-SQL

I'm working with severe performance optimization when executing a query that utilizes a cross join in SQL Server 2022. The query is meant to generate a Cartesian product between two relatively small tables, `Products` and `Categories`, but it takes an unexpectedly long time to run. Hereโ€™s the query Iโ€™m using: ```sql SELECT P.ProductName, C.CategoryName FROM Products P CROSS JOIN Categories C; ``` Both tables have around 1000 rows, so I anticipated that this query would execute fairly quickly. However, it takes over a minute to return the results. I also tried using an inner join as a test, and that worked fine, returning results almost instantaneously. Iโ€™ve checked the execution plan, and it shows that the cross join is being evaluated without any filters, leading me to suspect that the optimizer is struggling with this type of join. To optimize the performance, I attempted to create indexes on both tables, but that didnโ€™t seem to make a difference. I also made sure that there are no other heavy processes running on the server during execution, yet the performance optimization continue. Is there something inherently inefficient about using a cross join in this context, or is there a specific configuration or setting in SQL Server 2022 that I might be overlooking? Any insights or suggestions for improving the execution time would be greatly appreciated.