Oracle Performance Tuning:
3. Fully Qualify Column Reference:
select lname, order_date
from customer c join orders o on
c.customer_number = o.customer_number
select c.lname, o.order_date
from customer c join orders o on
c.customer_number = o.customer_number
|
---|
Use EXISTS rather than IN:
SELECT customer.lname FROM customer
WHERE customer_number
IN (SELECT customer_number FROM orders);
versus:
SELECT customer.lname FROM customer
WHERE EXISTS (SELECT
1 FROM orders WHERE orders.customer_number = customer.customer_number);
|
---|
Fixing the SQL:
First make it readable -- prettying the sql.
You can't fix what you can't read. One of the problems with code in general, not just SQL, is that lazy or inexperienced programmers don't bother to make the stuff readable, many programmers have the attitude that if I can squeeze it all onto one or two lines that is more efficient. Part of this attitude comes from the old-days when disk space was expensive. The few lines used less disk space. Today this is not an issue and the number of lines used has no effect on the compilers ability to efficiently execute the code. Making code readable does help programmers to debug the problem, especially if the code was written by someone else.
"Riding the unique indexes" You should join your tables from child to parent NOT from parent to child.
Now try a make the following statement readable.
select student.lname, student.fname, student.student_id, classes.name, sessions.room, sessions.time, sessions.days, sessions.length, teacher.lname, teacher.fname from student, enrollment, classes, sessions, teachers where session_id = 1234 and student.student_id = enrollment.student_id and enrollment.session_id = sessions.session_id and sessions.class_id = classes.class_id and sessions.teacher_id = teacher.teacher_id order by student.lname, student.fname
SELECT student.lname, student.fname, student.student_id, classes.name, sessions.room, sessions.time, sessions.days, sessions.length, teacher.lname, teacher.fname FROM enrollment, student, classes, sessions, teachers WHERE session_id = 1234 AND enrollment.student_id = student.student_id AND enrollment.session_id = sessions.session_id AND sessions.class_id = classes.class_id AND sessions.teacher_id = teacher.teacher_id ORDER BY student.lname, student.fname
Bind Variables: The technique of using place holders in the text of SQL statement and then substituting that place holder with a value stored in a variable is strongly encouraged for batch programs, forms, reports, and analysis tools. In Java this is done using Prepared Statements instead of Dynamic SQL.
// Dynamic Example:
String sql = "SELECT * FROM customer WHERE customer_id = 1";
// Prepared (Bind) Example:
String sql = "SELECT * FROM customer WHERE customer_id = ?";
Resolving Excessive Resource Consumption "On MySQL systems, excessive resource consumption is nearly always due to inefficient SQL statements. Fixing an inefficient SQL statement could be as simple as adding an appropriate index, or it could be as complex as brain surgery."
Remember the 80/20 rule: 80% of the problem can usually be fixed in 20% of the time. If the performance is now "Good Enough" don't waste time with the remaining 20%. Know when to quit and to recognize a "Point of diminishing returns".