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
538 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 ...
Wise Owl have found one SQL structure which slows down code more than any other |
---|
If your stored procedures or SQL queries are running slowly, don't bother looking at correlated subqueries, cursors, functions or any of the other usual culprits. There's one SQL syntax which Wise Owl have found to be an order of magnitude worse than any other, as this blog shows. |
In general our stored procedures aren't that time-critical: we just don't have enough data for them to matter. But one procedure was seizing up our internal Intranet. It used lots of possible culprits:
Possible culprit | Notes |
---|---|
User-defined functions | Everything I read says that scalar functions are slow. |
Multiple criteria | My SQL statement had about 15 criteria in. |
Nested functions | One scalar function called another. |
I tried removing all of these, but the procedure still took over 10 seconds to run. Finally, I found the culprit - some really sloppy programming, because I'm always short of time when I write these things:
-- use main address if gmail, etc
IF EXISTS (
SELECT *
FROM tblUrlHome AS u
WHERE u.UrlHomeName = @DomainMain
)
SET @email = @EmailAddress
So I changed this to code which is almost as sloppy:
-- use main address if gmail, etc
DECLARE @NumberMain AS int = (
SELECT COUNT(*)
FROM tblUrlHome AS u
WHERE u.UrlHomeName = @DomainMain
)
IF @NumberMain > 0
SET @email = @EmailAddress
The difference? The use of IF EXISTS within a function. Lots of websites will tell you the SQL structures you should avoid, but this seems to me to be an order of magnitude slower than correlated subqueries, dynamic SQL, scalar functions, cursors and any of the other usual suspects.
Some other pages relevant to the above blog 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.