© 2021 Mullins Consulting, Inc. All Rights Reserved Privacy Policy Contact Us
by Craig S. Mullins
Assuring optimal performance of database applications starts with coding properly formulated SQL. Poorly written SQL and application code is the cause of most performance problems. As much as 75% of poor relational performance is caused by "bad" SQL and application code.
But writing efficient SQL statements can be tricky. This is especially so for programmers new to a relational database environment or those who have never been trained to properly write SQL. Of course, it is impossible to adequately address all SQL performance issues in a column like this, but we can address the basics of SQL coding for performance.
Steps to Efficient SQL Statements
The first step is to move from thinking about file-
The next step is to always provide only the exact columns that you need to retrieve in the SELECT-
Next rule: Be sure to use the WHERE clause to filter data in the SQL instead of bringing it all into your program to filter. This is a common rookie mistake. It is much better for the DBMS to filter the data before returning it to your program, because I/O and CPU resources are required to obtain each row of data. The fewer rows passed to your program, the more efficient your SQL will be. So, the following SQL
SELECT EMPNO, LASTNAME, SALARY
FROM EMP
WHERE SALARY > 50000.00;
is better than simply reading all of the data without the WHERE clause and then checking each row in your program to see if the SALARY is greater than 50000.00.
Another important rule is to avoid asking for what you already know. This may sound simplistic, but many developers violate this rule at one time or another. For example, what is wrong with the following SQL statement?
SELECT EMPNO, LASTNAME, SALARY
FROM EMP
WHERE EMPNO = '000010';
Give up? The problem is that EMPNO is included in the SELECT-
From an administrative perspective, it is important to keep your database statistics up-
Follow the Rules
Building appropriate indexes for your queries is also critically important, but we cannot adequately get into all the nuances of that topic in this month's column. Suffice it to say, that if you follow all of the above rules and experience performance problems, the next things to check are your query's access path and what indexes are available.
These rules are not difficult to follow and additional, in-
From Database Trends and Applications, December 2010.
© 2012 Craig S. Mullins,
December 2010