473,498 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL "Commandments"

I was wondering if anybody has a document that states do's and don'ts
when programming for a MS SQL DB. I was under the impression that
Microsoft once produced such a document. Rather than a "Best Practices"
type document it was more of a "Worst Practices" document. Stating
certain procedures that might create slow-downs, bottlenecks, etc. Any
help would be greatly appreciated.

Thank you.

Jul 23 '05 #1
10 1812
nib
ry********@hotmail.com wrote:
I was wondering if anybody has a document that states do's and don'ts
when programming for a MS SQL DB. I was under the impression that
Microsoft once produced such a document. Rather than a "Best Practices"
type document it was more of a "Worst Practices" document. Stating
certain procedures that might create slow-downs, bottlenecks, etc. Any
help would be greatly appreciated.

Thank you.


I haven't seen a document like that (although there could be one), but
here are a couple don'ts:

Dont*
------
Use dynamic sql
Use Cursors
Preface table names tbl or view vw, etc.
Preface column names: chrLastName
* = There are always exceptions to every rule.

I'm sure others will add to the list.

Zach
Jul 23 '05 #2

<ry********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I was wondering if anybody has a document that states do's and don'ts
when programming for a MS SQL DB. I was under the impression that
Microsoft once produced such a document. Rather than a "Best Practices"
type document it was more of a "Worst Practices" document. Stating
certain procedures that might create slow-downs, bottlenecks, etc. Any
help would be greatly appreciated.

Thank you.


You could start with the Best Practices Analyzer Tool (and of course the
various best practices documents) linked from here:

http://www.microsoft.com/sql/default.mspx

I realise that you say you're looking more for "don't do this" as opposed to
"do this", but in many cases the best practises are stated as "... is not
enabled", "permissions are not granted on ..." or "avoid use of ...".

If this isn't helpful, you might want to mention if you're interested in a
particular topic, such as security, TSQL coding, DTS packages etc. - MS has
several white papers (and also KB articles) for more specific areas.

Simon
Jul 23 '05 #3
Thank you both very much. I am most interested in T-SQL. I have some
applications that are several years old that somebody else developed
that seem to be having significant performance issues now that the load
has grown beyond what was initially expected.

Jul 23 '05 #4
http://www.mindsdoor.net/SQLAdmin/BadThings.html

Nigel Rivett
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

Nigel Rivett wrote:
http://www.mindsdoor.net/SQLAdmin/BadThings.html

Nigel Rivett
www.nigelrivett.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Here's a list of my own that I try and preach wherever possible:

Always use a SARG (Search ARGument) in a WHERE clause.
Avoid using the "LIKE" operator with no wildcard.
Don't use the HAVING clause on table columns - use WHERE. (Reserve
HAVING for aggregates)
Use >= instead of > , especially if data contains large number of
duplicate values.
Avoid use of SELECT * FROM ...
Don't Use Cursors! Use a WHILE loop instead.
Avoid use of !=, <> and 'not' operators where possible.
Don't use huge IN clauses.
Don't use DISTINCT in a UNION.
Don't use functions on table columns in WHERE clauses.
Avoid mismatched datatypes in WHERE and join clauses.
Avoid using derived tables where large rowsets are involved - you can't
index derived tables.
Beware of unknown run-time values in WHERE clauses - may result in
tablescans.

Jul 23 '05 #6
ry********@hotmail.com wrote:
I was wondering if anybody has a document that states do's and don'ts
when programming for a MS SQL DB. I was under the impression that
Microsoft once produced such a document. Rather than a "Best Practices"
type document it was more of a "Worst Practices" document. Stating
certain procedures that might create slow-downs, bottlenecks, etc. Any
help would be greatly appreciated.

Thank you.

1. Commandment:
It's "MS SQL Server". Not "SQL". SQL is an ANSI and ISO standard.
The reason why I say that is that its commandmends will look very
different than those of the MS product with its own strength and
weaknesses that need to be accounted for.

Cheers
Serge
Jul 23 '05 #7
There is a series of articles like that on SQLl Server Central right
now, I also have a book coming out this year undert the workinG title
"SQL PROGRAMMING STYLE", but it is generic.

Jul 23 '05 #8
Phil (ph********@btopenworld.com) writes:
Here's a list of my own that I try and preach wherever possible:
Some good advice - and some poor advice.
Always use a SARG (Search ARGument) in a WHERE clause.
Which means: don't stick your indexed columns into a function, and
make sure that they are not implicitly converted.
Use >= instead of > , especially if data contains large number of
duplicate values.
I guess which to use, depends on the actual problem at hand.
Avoid use of SELECT * FROM ...
Yes!
Don't Use Cursors! Use a WHILE loop instead.
No! Avoid iterating altogether, but if you have to iterate, use a
cursor (and be sure to make INSENSITIVE). Some of my colleagues are
fond of "poor man's cursor" with a WHILE loop. I have more than once
identified performance problems, because they were looping a non-
indexed temp table. Had they used a cursor, the damage would have been
limited-
Avoid use of !=, <> and 'not' operators where possible.
There is no reason for this. If the condition calls for a <>, use it.
(Although <> is not that very commonly used.)
Don't use huge IN clauses.
Yes! There is a tremendous cost for finding the query plan in this
case. Note that rewriting to OR clauses changes nothing - IN is just
syntactic sugar for that. Rewrite and use a table. Also look at my
web site for arrays and lists in SQL Server.
Don't use DISTINCT in a UNION.
Yup. UNION implies DISTINCT.
Don't use functions on table columns in WHERE clauses.
Yup. Scalar UDFs can be expensive. Always benchmark.
Avoid mismatched datatypes in WHERE and join clauses.
Yup. See above under SARG.
Avoid using derived tables where large rowsets are involved - you can't
index derived tables.
So what? You express the query - the optimizer evaluates it. You
may have:

UPDATE smalltable
SET col = col + b.sumval
FROM smalltable s
JOIN (SELECT keycol, sumval = SUM(sumval)
FROM bigtable
GROUP BY keycol) b ON s.keycol = b.keycol
WHERE s.last_updated < datedadd(DAY, -1, getdate())

That is necesarily how the optimizer will actually compute the query.

Specifically, my experience is that the above is a lot faster than

UPDATE smalltable
SET col = col + (SELECT SUM(sumval)
FROM bigtable b
WHERE smalltable.keycol = b.keycol)
WHERE last_updated < datedadd(DAY, -1, getdate())

which is how the ANSI purists would write the query.
Beware of unknown run-time values in WHERE clauses - may result in
tablescans.


So can known values as well.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #9
Check out http://www.sql-server-performance.com/

<ry********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I was wondering if anybody has a document that states do's and don'ts
when programming for a MS SQL DB. I was under the impression that
Microsoft once produced such a document. Rather than a "Best Practices"
type document it was more of a "Worst Practices" document. Stating
certain procedures that might create slow-downs, bottlenecks, etc. Any
help would be greatly appreciated.

Thank you.

Jul 23 '05 #10
Thank you all very much. I have been able to put together a great
document to distribute to my developers. They are hard at work combing
through this old code.

And Serge, I certainly understand your sentiment. However, my omission
was based on the casual nature of the group and the fact that when you
precede SQL with MS most, if not all, DB professionals know of what I
am referring. I sincerely apologize if I offended anyone!

Thanks again!

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1340
by: nice_reply_only | last post by:
Good News! Do you know how simple it is to go to Heaven after this life has ended? Some people believe that belonging to a local church, temple, mosque or synagogue will get them to Heaven.
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.