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)?
39 Answers
I am consistently disappointed by most programmers' tendency to mix their UI-logic in the data access layer:
SELECT
FirstName + ' ' + LastName as "Full Name",
case UserRole
when 2 then "Admin"
when 1 then "Moderator"
else "User"
end as "User's Role",
case SignedIn
when 0 then "Logged in"
else "Logged out"
end as "User signed in?",
Convert(varchar(100), LastSignOn, 101) as "Last Sign On",
DateDiff('d', LastSignOn, getDate()) as "Days since last sign on",
AddrLine1 + ' ' + AddrLine2 + ' ' + AddrLine3 + ' ' +
City + ', ' + State + ' ' + Zip as "Address",
'XXX-XX-' + Substring(
Convert(varchar(9), SSN), 6, 4) as "Social Security #"
FROM Users
Normally, programmers do this because they intend to bind their dataset directly to a grid, and its just convenient to have SQL Server format server-side than format on the client.
Queries like the one shown above are extremely brittle because they tightly couple the data layer to the UI layer. On top of that, this style of programming thoroughly prevents stored procedures from being reusable.
-
13A good poster-child pattern for maximum coupling across the largest possible number of tiers/abstraction layers.– dkretzDec 6, 2008 at 22:33
-
3It may not be good for de-coupling, though for performance reasons I've done stuff like that often, iterative changes done by SQL Server are faster than done by code in mid-tier. I don't get you reusability point - nothing stops you from running the SP and renaming the cols if so you wish. Dec 7, 2008 at 6:17
-
57My favorite is when people embed HTML AND javascript, e.g. SELECT '<a href=... onclick="">' + name ' </a>' Jan 14, 2009 at 17:19
-
16With queries like this, you can edit the grid in a website with a simple alter statement. Or change the content of an export, or reformat a date in a report. This makes clients happy, and saves me time. So thanks, but no thanks, I'll stick with queries like this.– AndomarMay 18, 2009 at 15:13
-
4
Here are my top 3.
Number 1. Failure to specify a field list. (Edit: to prevent confusion: this is a production code rule. It doesn't apply to one-off analysis scripts - unless I'm the author.)
SELECT *
Insert Into blah SELECT *
should be
SELECT fieldlist
Insert Into blah (fieldlist) SELECT fieldlist
Number 2. Using a cursor and while loop, when a while loop with a loop variable will do.
DECLARE @LoopVar int
SET @LoopVar = (SELECT MIN(TheKey) FROM TheTable)
WHILE @LoopVar is not null
BEGIN
-- Do Stuff with current value of @LoopVar
...
--Ok, done, now get the next value
SET @LoopVar = (SELECT MIN(TheKey) FROM TheTable
WHERE @LoopVar < TheKey)
END
Number 3. DateLogic through string types.
--Trim the time
Convert(Convert(theDate, varchar(10), 121), datetime)
Should be
--Trim the time
DateAdd(dd, DateDiff(dd, 0, theDate), 0)
I've seen a recent spike of "One query is better than two, amiright?"
SELECT *
FROM blah
WHERE (blah.Name = @name OR @name is null)
AND (blah.Purpose = @Purpose OR @Purpose is null)
This query requires two or three different execution plans depending on the values of the parameters. Only one execution plan is generated and stuck into the cache for this SQL text. That plan will be used regardless of the value of the parameters. This results in intermittent poor performance. It is much better to write two queries (one query per intended execution plan).
-
7hmmm, I'll give you a +1 for points 2 and 3 alone, but developers overplay rule 1. It has it's place sometimes.– annakataDec 6, 2008 at 20:01
-
1
-
30When you use select *, you get whatever is in the table. Those columns may change names and order. Client code frequently relies on names and order. Every 6 months I'm asked how to preserve column order when modifying a table. If the rule was followed it wouldn't matter.– Amy BDec 6, 2008 at 20:11
-
I've used #2 sometimes, others I've gone the cursor route (though then I first save the results of the query on a table var, open the cursor on that). I've always wondered if someone has done a performance test of both. Dec 7, 2008 at 6:13
-
5...but of course cursors should almost always be a last resort, after failure to figure out how to do the job with set-based SQL. I once spent about 45 minutes carefully dissecting a horrendous, gigantic PL/SQL cursor in a stored procedure (drew diagrams of the rotten thing), which populated a big temp table then selected the contents of the temp table back to the caller to render a report. It took 8.5 minutes to run, on substantial hardware. After diagramming the whole thing, I was able to replace it with a single query which returned the same results in under 2 seconds. Cursors, man... Oct 9, 2014 at 3:44
Human readable password fields, egad. Self explanatory.
Using LIKE against indexed columns, and I'm almost tempted to just say LIKE in general.
Recycling SQL-generated PK values.
Surprise nobody mentioned the god-table yet. Nothing says "organic" like 100 columns of bit flags, large strings and integers.
Then there's the "I miss .ini files" pattern: storing CSVs, pipe delimited strings or other parse required data in large text fields.
And for MS SQL server the use of cursors at all. There's a better way to do any given cursor task.
Edited because there's so many!
-
19wrong about cursors, i would be hesitant about saying doing any particular thing is 100% right or 100% wrong– ShawnDec 6, 2008 at 23:41
-
4So far every cursor defense example I've seen is using the wrong tool for the job. But if all you know is SQL, you either use it inappropriately, or you learn to write other kinds of software.– dkretzDec 8, 2008 at 2:08
-
3@tuinstoel: How does LIKE '%blah%' get to use an index? Indexing relies on ordering and this example searches a random middle position of a string. (Indexes order by the 1st character 1st, and so looking at the middle 4 characters gives a virtually random order...) Feb 4, 2009 at 15:06
-
12On most database servers (at least the ones I've used), LIKE can use indexes.. as long as it's a prefix-search (LIKE 'xxx%') -- that is, as long as the wildcard characters don't come first in the search string. I think you might be talking at cross-purposes here a little.– CowanJun 5, 2010 at 7:50
-
10
Don't have to dig deep for it: Not using prepared statements.
-
3Yup. Followed closely in the same context, in my experience, with "not trapping errors".– dkretzDec 6, 2008 at 22:37
-
2@stesch: This is nothing compared to using views and having a variable reporting date. Views are an antipattern if you have a variable reporting date (i assume most applications have). Would add this in a separate answer, but it's closed, unfortunately. Dec 12, 2013 at 14:02
Using meaningless table aliases:
from employee t1,
department t2,
job t3,
...
Makes reading a large SQL statement so much harder than it needs to be
-
51
-
10terse aliases are OKAY. If you want a meaningful name then don't use an alias at all. Dec 6, 2008 at 22:56
-
45He didn't say "terse," he said "meaningless." In my book there would be nothing wrong with using e, d, and j as the aliases in the example query. Dec 7, 2008 at 9:14
-
12
-
8I would use emp for employee, dep for department and job for job (or maybe jb) :) Dec 17, 2008 at 2:04
var query = "select COUNT(*) from Users where UserName = '"
+ tbUser.Text
+ "' and Password = '"
+ tbPassword.Text +"'";
- Blindly trusting user input
- Not using parameterized queries
- Cleartext passwords
-
All of which can usefully be dealt with by using a database abstracton layer of some (any) kind.– dkretzDec 7, 2008 at 3:04
-
@doofledorfer: Agree, a middle tier would be definitely better in a case like this, plus providing results caching as a nice side effect. Dec 7, 2008 at 6:45
-
Awesome example. If a dev groks how to replace that with a good solution, they are half-way to becoming a decent SQL dev. Dec 7, 2008 at 8:17
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:
ID INT, Name NVARCHAR(132), IntValue1 INT, IntValue2 INT, CharValue1 NVARCHAR(255), CharValue2 NVARCHAR(255), Date1 DATETIME, Date2 DATETIME
I've lost count of the number of clients I've seen who have systems that rely on abominations like this.
-
1Worse yet, I read that in newest version of Access that's actually supported automatically, which I fear will encourage more of this Value1, Value2, Value3... column fetichism Dec 7, 2008 at 6:18
-
The ones that I dislike the most are
Using spaces when creating tables, sprocs etc. I'm fine with CamelCase or under_scores and singular or plurals and UPPERCASE or lowercase but having to refer to a table or column [with spaces], especially if [ it is oddly spaced] (yes, I've run into this) really irritates me.
Denormalized data. A table doesn't have to be perfectly normalized, but when I run into a table of employees that has information about their current evaluation score or their primary anything, it tells me that I will probably need to make a separate table at some point and then try to keep them synced. I will normalize the data first and then if I see a place where denormalization helps, I'll consider it.
Overuse of either views or cursors. Views have a purpose, but when each table is wrapped in a view it's too much. I've had to use cursors a few times, but generally you can use other mechanisms for this.
Access. Can a program be an anti-pattern? We have SQL Server at my work, but a number of people use access due to it's availabilty, "ease of use" and "friendliness" to non-technical users. There is too much here to go into, but if you've been in a similar environment, you know.
-
2#4 - there is another thread just for <a href='stackoverflow.com/questions/327199/…> :).– dkretzDec 6, 2008 at 22:40
-
4Access is NOT a DBMS. It's a RAD environment, with a very simple database manager included. SQL Server, Oracle, et al. will never replace it, unless you add a VB-like language and a Crystal Reports like facility. Dec 7, 2008 at 6:31
-
@JoePineda Not sure what you're on about citing VB and Crystal Reports, but I'd say 12 years later and C# and SSRS definitely surpassed them greatly at this point. While unfortunately Access is still used in the wild by less-than-technical people, it's surely dead at this point. :)– J.D.Jan 24, 2021 at 14:01
For storing time values, only UTC timezone should be used. Local time should not be used.
-
4I've still not found a good simple solution for converting from UTC to local time for dates in the past, when daylight saving has to be considered, with varying change dates accross years and countries, as well as all exceptions within countries. So UTC doesn't save you from conversion complexity. However, it's important to have a way to know the timezone of every stored datetime.– ckarrasJun 14, 2009 at 11:38
-
1@CsongorHalmai Many places practice daylight savings, so time values within an hour of the time shift can be ambiguous. Sep 3, 2017 at 17:40
-
2That's certainly right for the present and the past, but for the future, especially the fairly-far future, explicit time zones are often a necessity. If you have a 30-year option that was just written and expires in 2049-09-27T17:00:00 New York time, then you can't just blindly assume that will be 21:00:00Z. The U.S. Congress might well change the DST rules. You have to keep the local time and the true time zone (America/New_York) separate. Sep 27, 2019 at 12:13
use SP as the prefix of the store procedure name because it will first search in the System procedures location rather than the custom ones.
-
1Can also be extended to using any other common prefix for all stored procedures, making it more difficult to pick through a sorted list.– dkretzDec 6, 2008 at 22:36
-
7+1 for doofledorfer comment!! I've seen this a lot, I find this idiotic and does indeed make searching for a particular SP very difficult!!! Also extended to "vw_" for views, "tbl_" for tables and the like, how I hate them! Dec 7, 2008 at 6:24
-
1The prefixes can be useful if you're scripting the objects to files (eg: for source control, deployments or migration)– RickJul 15, 2009 at 23:59
-
1Why on earth would it be useful to prefix every single stored procedure with sp or usp? It just makes it harder to scan the list for the one you want. Dec 3, 2009 at 20:14
Overuse of temporary tables and cursors.
-
2
-
2Overuse of anything is by definition unwanted. A specific example of where using temp tables/cursors would not needed would be helpful. Feb 8, 2010 at 18:39
-
7Mostly I see temp tables under-used. with SQL Server often you get performance gains by doing stuff with a bunch of temp tables instead of one monolithic query.– CervoSep 10, 2010 at 20:05
using @@IDENTITY instead of SCOPE_IDENTITY()
Quoted from this answer :
- @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes. You need to be careful here, since it's across scopes. You could get a value from a trigger, instead of your current statement.
- SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope. Generally what you want to use.
- IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope. This lets you specify which table you want the value from, in case the two above aren't quite what you need (very rare). You could use this if you want to get the current IDENTITY value for a table that you have not inserted a record into.
-
-
Using @@ in SQL is already an anti-pattern, because ANSI/ISO SQL doesn't know variables. Oct 7, 2020 at 9:47
Re-using a 'dead' field for something it wasn't intended for (e.g. storing user data in a 'Fax' field) - very tempting as a quick fix though!
select some_column, ...
from some_table
group by some_column
and assuming that the result will be sorted by some_column. I've seen this a bit with Sybase where the assumption holds (for now).
-
2upvote for EVER assuming sort order, just because that was the way it showed up in the query tool that one time Dec 6, 2008 at 23:00
-
3
-
6in MySQL, it is documented to sort. <dev.mysql.com/doc/refman/5.0/en/select.html>. So blame MySQL (again).– derobertDec 7, 2008 at 6:09
-
1In Oracle, the unsorted results (almost) always matched the grouping - until version 10G. Lots of rework for the developers who used to leave out the ORDER BY! Jan 14, 2009 at 17:05
-
1I was even in a training class where this was stated as a fact for SQL Server. I had to protest really loud. For just saving to type 20 characters you rely on obscure or undocumented behavior. Dec 16, 2009 at 21:42
SELECT FirstName + ' ' + LastName as "Full Name", case UserRole when 2 then "Admin" when 1 then "Moderator" else "User" end as "User's Role", case SignedIn when 0 then "Logged in" else "Logged out" end as "User signed in?", Convert(varchar(100), LastSignOn, 101) as "Last Sign On", DateDiff('d', LastSignOn, getDate()) as "Days since last sign on", AddrLine1 + ' ' + AddrLine2 + ' ' + AddrLine3 + ' ' + City + ', ' + State + ' ' + Zip as "Address", 'XXX-XX-' + Substring(Convert(varchar(9), SSN), 6, 4) as "Social Security #" FROM Users
Or, cramming everything into one line.
-
Used a previous comment's query, just because that was the first SQL-statement I had available. Apr 19, 2009 at 3:54
The
FROM TableA, TableB WHERE
syntax for JOINS rather thanFROM TableA INNER JOIN TableB ON
Making assumptions that a query will be returned sorted a certain way without putting an ORDER BY clause in, just because that was the way it showed up during testing in the query tool.
-
6My Oracle DBAs always complain that I use "ANSI joins", that is, what you present as the correct way. But I keep doing it, and I suspect that deep down they know its better. Dec 7, 2008 at 8:19
-
1I suspect that Oracle wishes standard SQL would go away. :-) Also, you can't mix implicit and explicit JOINS (aka ANSI JOINs) in MySQL 5 - it doesn't work. Which is another argument for explicit JIONs. Dec 8, 2008 at 0:42
-
3I would say that even A INNER JOIN B ON is an anti pattern. I prefer A INNER JOIN B USING. Mar 10, 2009 at 21:39
-
Oracle supports ANSI syntax now, but they used to have this really weird syntax for outer joins in the past and there are too many people still using it.– CervoSep 10, 2010 at 20:06
-
well...Oracle still won't let you use ANSI joins for Fast Refreshable, On-Commit Materialized Views– GerratMay 17, 2018 at 18:12
Learning SQL in the first six months of their career and never learning anything else for the next 10 years. In particular not learning or effectively using windowing/analytical SQL features. In particular the use of over() and partition by.
Window functions, like aggregate functions, perform an aggregation on a defined set (a group) of rows, but rather than returning one value per group, window functions can return multiple values for each group.
See O'Reilly SQL Cookbook Appendix A for a nice overview of windowing functions.
I need to put my own current favorite here, just to make the list complete. My favorite antipattern is not testing your queries.
This applies when:
- Your query involves more than one table.
- You think you have an optimal design for a query, but don't bother to test your assumptions.
- You accept the first query that works, with no clue about whether it's even close to optimized.
And any tests run against atypical or insufficient data don't count. If it's a stored procedure, put the test statement into a comment and save it, with the results. Otherwise, put it into a comment in the code with the results.
-
A very useful technique for minimal T-SQL test: In the .SQL file where you define your SP, UDF, etc., immediately after it create a block test like IF 1=2 BEGIN (sample cases for your code, with expected results as comments) END Dec 7, 2008 at 6:34
-
SQL Server does parse the code within the test block, even though it's never executed. So when your object gets modified and receives more parameters, or of different type, etc. or an objects it depends on is modified you'll receive an error just by asking for an execution plan! Dec 7, 2008 at 6:37
-
It's not always possible to test with real data. Often the dev server/"test" server is underpaid and gets a fraction of the live server. Generally tests are frowned on against the live server. Some places are better and have a test or staging server with live data.– CervoSep 10, 2010 at 20:08
Temporary Table abuse.
Specifically this sort of thing:
SELECT personid, firstname, lastname, age
INTO #tmpPeople
FROM People
WHERE lastname like 's%'
DELETE FROM #tmpPeople
WHERE firstname = 'John'
DELETE FROM #tmpPeople
WHERE firstname = 'Jon'
DELETE FROM #tmpPeople
WHERE age > 35
UPDATE People
SET firstname = 'Fred'
WHERE personid IN (SELECT personid from #tmpPeople)
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.
-
1+1, I agree. Although, I have found at least one or two cases where this technique has improved performance - the queries involved were complex to say the least.– a'rAug 30, 2010 at 10:45
-
1
-
1Sometimes you have to do that if the conditions are super complicated. True it can be abused to extremes. But many times a simple delete is much simpler than the logic for getting the case in the initial query. Also sometimes if the clause is not sargeable the initial query will slow down. But just doing it on the smaller temp table is more efficient. And other times you keep adding cases that business people keep adding after the fact.– CervoSep 10, 2010 at 20:13
I just put this one together, based on some of the SQL responses here on SO.
It is a serious antipattern to think that triggers are to databases as event handlers are to OOP. There's this perception that just any old logic can be put into triggers, to be fired off when a transaction (event) happens on a table.
Not true. One of the big differences are that triggers are synchronous - with a vengeance, because they are synchronous on a set operation, not on a row operation. On the OOP side, exactly the opposite - events are an efficient way to implement asynchronous transactions.
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/)
-
24
-
Sometimes it is, sometimes it isn't. Luckily, it's often easy to test, and different options work with different db needs. Jan 5, 2009 at 0:26
-
19Normalization is not just for disk space savings. It is also to create an authoritative source for the data. If the data is stored only one place, then consistency is not a byproduct of careful coding, but is instead a byproduct of design. Jan 12, 2012 at 21:27
-
Storing compound data in JSON format is one thing: there is more and more support for it, and it's a conscious tradeoff. Using comma-separated (or whatever) values in an attempt to save one join is penny-wise and pound-foolish. Sep 27, 2019 at 12:20
-
noSQL solutions are showing a performance benefit at the cost of duplicate data by eliminating multi-table lookups. Puts the whole normalization thing on its head. In some examples the data is collected in multiple places to ensure one process has the fastest response time possible. Of course, questions about authoritative sources come in to play. Dec 5, 2019 at 22:47
Stored Procedures or Functions without any comments...
-
And views ;) Functions true, except table-valued functions (=views with parameters). Dec 12, 2013 at 14:22
-
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':
img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO",
"MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
img_major_mime ENUM("unknown", "application", "audio", "image", "text",
"video", "message", "model", "multipart") NOT NULL default "unknown",
(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
CONVERT(NVARCHAR, GETDATE())
without format identifier
-
-
2Why is this bad? surely if you are trying to express a set of values this works just as well as a lookup table, and fits better with code that calls it. Id rather have an enum in my app code that maps to an enum constraint in my DB than an enum in my app code that maps to specific rows of a lookup table. It just feels cleaner. Feb 8, 2010 at 18:10
-
Identical subqueries in a query.
-
10Unfortunately, sometimes you just can't avoid that - in SQL 2000 there was no "WITH" keyword, and using UDFs to encapsulate common subqueries sometime leads to performance penalties, blame MS on that... Dec 7, 2008 at 6:25
-
Well, hopefully they will get around to adding it one of these days. Dec 9, 2008 at 2:24
-
-
@recursive: you can't have indexes on a table variable, which will often make it slower than a subquery. However you could use a temporary table with custom indexes.– RickJul 16, 2009 at 0:08
-
Cool, have been working with SQL for years, and didn't even know Common Table Expressions exist (though I would have needed them). Now I do! Thanks!– sleskeOct 29, 2009 at 23:27
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 -
SELECT CASE @problem WHEN 'Need to replace column A with this medium to large collection of strings hanging out in my code.' THEN 'Create a table for lookup and add to your from clause.' WHEN 'Scrubbing values in the result set based on some business rules.' THEN 'Fix the data in the database' WHEN 'Formating dates or numbers.' THEN 'Apply formating in the presentation layer.' WHEN 'Createing a cross tab' THEN 'Good, but in reporting you should probably be using cross tab, matrix or pivot templates' ELSE 'You probably found another case for no CASE but now I have to edit my code instead of enriching the data...' END
The two I find the most, and can have a significant cost in terms of performance are:
Using cursors instead of a set based expression. I guess this one occurs frequently when the programmer is thinking procedurely.
Using correlated sub-queries, when a join to a derived table can do the job.
-
I agree if you mean what I think you mean; although a correlated sub-query is a type of derived table IIRC.– dkretzDec 7, 2008 at 5:08
-
1A derived table is a set operation, whereas a correlated subquery runs for each row in the outer query, making it less efficient (9 times out of 10) Dec 7, 2008 at 6:07
-
A couple years ago I found to my surprise that SQL S. is somehow optimized for handling correlated queries: for simple ones you get the same execution plan as with a logically equivalent query using a JOIN! Also, correlated queries that bring Oracle to its knees run only slowly on SQL S.! Dec 7, 2008 at 6:39
-
That's why I always test it both ways. And I <i>do</> usually try it both ways. In practice, for SQL Server anyway, I've usually found the correlated sq to be no slower.– dkretzDec 7, 2008 at 8:46
-
4PLEASE understand that a correlated subquery and a join are IDENTICAL (in most cases). They are not even different things that are optimized to one another, but just different textual representations of the same operation. Dec 16, 2009 at 21:54
Developers who write queries without having a good idea about what makes SQL applications (both individual queries and multi-user systems) fast or slow. This includes ignorance about:
- physical I/O minimization strategies, given that most queries' bottleneck is I/O not CPU
- perf impact of different kinds of physical storage access (e.g. lots of sequential I/O will be faster than lots of small random I/O, although less so if your physical storage is an SSD!)
- how to hand-tune a query if the DBMS produces a poor query plan
- how to diagnose poor database performance, how to "debug" a slow query, and how to read a query plan (or EXPLAIN, depending on your DBMS of choice)
- locking strategies to optimize throughput and avoid deadlocks in multi-user applications
- importance of batching and other tricks to handle processing of data sets
- table and index design to best balance space and performance (e.g. covering indexes, keeping indexes small where possible, reducing data types to minimum size needed, etc.)
Putting stuff in temporary tables, especially people who switch from SQL Server to Oracle have a habit of overusing temporary tables. Just use nested select statements.
-
Depending on the type of nested select (IN, EXISTS) they can run for every row of the parent query which is super slow. Jul 4, 2022 at 22:46
Using primary keys as a surrogate for record addresses and using foreign keys as a surrogate for pointers embedded in records.
Application Joins Not solely an SQL issue, but looking for descriptions of the problem and finding this question, I was surprised it wasn't listed.
As I've heard the phrase used, an application join, is when you pull a set of rows out of each of two or more tables and then join them in your (Java) code with a pair of nested for loops. This burdens the system (your app and the database) with having to identify the whole cross product, retrieving it and sending it to the appication. Assuming the app can filter the cross product down as fast as the database (dubious), just cutting the result set down sooner means less data transfer.