Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Speed comparison of temporary tables against table variables Part four of a five-part series of blogs |
---|
When you are programming in SQL, you often have the choice of using table variables or temporary tables. This blog considers whether one method is substantially quicker than the other (spoiler alert: not really).
|
The SQL query to generate the table of rows for table variables is nearly identical to the one for using a temporary table:
-- create a new one
DECLARE @AdviceRows TABLE (
ID int,
OwlyAdvice varchar(100)
)
-- the number of rows to generate
DECLARE @max int = 100000
-- loop counter used and string variable
DECLARE @i int = 1
DECLARE @s varchar(100)
-- keep going till we've created enough rows
WHILE @i <=>=>
BEGIN
-- text to store
SET @s =
CASE
WHEN @i % 3 = 0 THEN 'This too shall pass'
WHEN @i % 3 = 1 THEN 'Look before you leap'
ELSE 'Fortune favours the brave'
END
INSERT INTO @AdviceRows (ID,OwlyAdvice)
VALUES (@i,@s)
SET @i = @i + 1
END
SELECT
a.OwlyAdvice AS Advice,
COUNT(*) AS Frequency
FROM
@AdviceRows AS a
GROUP BY
a.OwlyAdvice
ORDER BY
Frequency ASC
SELECT * FROM @AdviceRows
Now we've seen the two test queries, it's time to compare the results!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.