All of us who work with relational databases have learned (or are learning) that SQL is different. Eliciting the desired results, and doing so efficiently, involves a tedious process partly characterized by learning unfamiliar paradigms, and finding out that some of our most familiar programming patterns don't work here. What are the common antipatterns you've seen (or yourself committed)?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
The Altered View - A view that is altered too often and without notice or reason. The change will either be noticed at the most inappropriate time or worse be wrong and never noticed. Maybe your application will break because someone thought of a better name for that column. As a rule views should extend the usefulness of base tables while maintaining a contract with consumers. Fix problems but don't add features or worse change behavior, for that create a new view. To mitigate do not share views with other projects and, use CTEs when platforms allow. If your shop has a DBA you probably can't change views but all your views will be outdated and or useless in that case.
The !Paramed - Can a query have more than one purpose? Probably but the next person who reads it won't know until deep meditation. Even if you don't need them right now chances are you will, even if it's "just" to debug. Adding parameters lowers maintenance time and keep things DRY. If you have a where clause you should have parameters.
The case for no CASE -
Using meaningless table aliases:
Makes reading a large SQL statement so much harder than it needs to be
Contrarian view: over-obsession with normalization.
Most SQL/RBDBs systems give one lots of features (transactions, replication) that are quite useful, even with unnormalized data. Disk space is cheap, and sometimes it can be simpler (easier code, faster development time) to manipulate / filter / search fetched data, than it is to write up 1NF schema, and deal with all the hassles therein (complex joins, nasty subselects, etc).
I have found the over-normalized systems are often premature optimization, especially during early development stages.
(more thoughts on it... http://writeonly.wordpress.com/2008/12/05/simple-object-db-using-json-and-python-sqlite/)
1) I don't know it's an "official" anti-pattern, but I dislike and try to avoid string literals as magic values in a database column.
An example from MediaWiki's table 'image':
(I just notice different casing, another thing to avoid)
I design such cases as int lookups into tables ImageMediaType and ImageMajorMime with int primary keys.
2) date/string conversion that relies on specific NLS settings
without format identifier
Temporary Table abuse.
Specifically this sort of thing:
Don't build a temporary table from a query, only to delete the rows you don't need.
And yes, I have seen pages of code in this form in production DBs.
My bugbears are the 450 column Access tables that have been put together by the 8 year old son of the Managing Director's best friends dog groomer and the dodgy lookup table that only exists because somebody doesn't know how to normalise a datastructure properly.
Typically, this lookup table looks like this:
I've lost count of the number of clients I've seen who have systems that rely on abominations like this.