I’m writing a stored procedure on SQL Server 2000.
I’ve written a complicated select statement that takes 18 seconds to run.
The question is, how can I self-join the result correctly and efficiently?
Is it possible to manage it in a single select statement without repeating the big query statement that I currently have? Or should I save the result in a table variable or temp table?
Many thanks.
If the original query runs a long time, but the result is small, yes, you could consider a temporary / helper table, and run your second query (the self-join) on that table.
This may also help keep your approach modular / maintainable. And you’ll be able to tune performance on that long-running query without your second one staring you in the face in the meantime.