In a large stored procedure I am creating a temp table.
I am then selecting from that temp table after inserting into it at various points.
There is no WHERE statement, just a simple select.
- If I select one column, it takes 0ms to return results.
- If I select the same column twice, it takes 0ms to return results.
- If I select the same column three times, it takes over 200ms to return results.
The column is VARCHAR(81)
If I increase the size of the column, it makes no difference.
0 ms
SELECT Product_Description FROM #Products
0 ms
SELECT Product_Description, Product_Description FROM #Products
200 ms
SELECT Product_Description, Product_Description, Product_Description FROM #Products
200 ms
SELECT Product_Description, Product_Description, Product_Description, Product_Description FROM #Products
I'm not sure if there is something fundamental I'm not understanding about SQL server
but this can be replicated every time.
The execution plan shows the same for each select (just the select, with cost 0%).
EDIT: Here is what I am seeing.
This is the code selecting from the temp table. The top and bottom lines are printing the system time:
This is the printed output when those 2 columns (same column) are being selected:
Here is the printed output when just one column is being selected:
Execution plan for 2 columns:
Execution plan for 1 column:
Finally, if I select Product_ID (int) 5 times, it executes at 0-1ms. If I select it 6 times it jumps to 190+ ms:
5x
6x