09 - SQL Optimization - Efficient SQL

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Sistem Basis Data

Program Studi S1 Sistem Informasi


Fakultas Sains dan Teknologi
Universitas Airlangga
Indra Kharisma Raharjana, S.Kom, M.T.
Semester Genap Tahun Ajaran 2012 / 2013
Mei 2013
SilberschatzKorthSudarshan, Database System Concepts, Fourth Edition, The McGrawHill 2011


http://82.157.70.109/mirrorbooks/highperfmysql/0596003064/hpmysql-CHP-5-SECT-1.html

Parsing and translation
translate the query into its internal form. This is then translated into
relational algebra.
Parser checks syntax, verifies relations
Query Optimization: Amongst all equivalent evaluation
plans choose the one with lowest cost.
Cost is estimated using statistical information from the
database catalog
e.g. number of tuples in each relation, size of tuples, etc.
Evaluation
The query-execution engine takes a query-evaluation plan, executes
that plan, and returns the answers to the query.
What type of query is this? Is it
a SELECT, INSERT, UPDATE, or DELETE, or
some other administrative command such
as SET or GRANT?
Which tables are involved? Are there any
aliases used?
What is the WHERE clause?
Are there other hints or modifiers involved?

Relations generated by two equivalent expressions have the same set of attributes
and contain the same set of tuples, although their attributes may be ordered
differently.
The query optimizer's goal, simply put, is to find the most
efficient way to execute a query given all the available
information. Most of the time, this means the optimizer
works to limit the number of records it must examine. It
does this because the time associated with disk I/O is
often (but not always) the governing factor that
determines how long a query will take. Intuitively, this
makes complete sense. It is an extension of the very same
logic that explains why indexes are so helpful.
The MySQL developers are constantly improving the
optimizerattempting to make it smarter and faster with
each new release. Based on feedback from real-world
users, they are always looking for ways to refine MySQL's
ability to make the right decision

Are there any indexes that are candidates for
finding the rows quickly?
Which index is best? If multiple tables are
involved, which index is best for each table?
Which tables depend on which other tables in
the join?
What's the optimal join order for the tables?

Efficient SQL Statements , http://www.oracle-
base.com/articles/misc/EfficientSQLStatements.php

Vigyan Kaushik, Tips for Writing Efficient SQL Queries



"select * from ...
Network traffic is reduced.
The code is easier to understand.
It could save the need for changes in the
future.


Always use table alias and prefix all column
names with the aliases when you are using
more than one table.

It is a good practice to use a standard syntax for
wiring SQL queries. I will recommend following
standards to use while writing SQL queries.
Write all standard SQL TEXT in upper case:
Write all non standard SQL TEXT (Table name,
Column name etc) in lower case:
For example:
SELECT ename, sal
FROM emp
WHERE deptno = 20;

Never use NOT operator on an indexed column.
Whenever Oracle encounters a NOT on an index
column, it will perform full-table scan.
For Example:
SELECT *
FROM emp
WHERE NOT deptno = 0;
Instead use the following:
SELECT *
FROM emp
WHERE deptno > 0;

Never use a function or calculation on an
indexed column. If there is any function is used
on an index column, optimizer will not use index.
For Example:
Do not use until need exactly match string:
SELECT *
FROM emp
WHERE SUBSTR (ename, 1, 3) = 'MIL';
Use following instead:
SELECT *
FROM emp
WHERE ename LIKE 'MIL%';

Use untransformed column values.
For example, use:
WHERE a.order_no = b.order_no
Rather than
WHERE TO_NUMBER (SUBSTR(a.order_no,
INSTR(b.order_no, '.') - 1)) = TO_NUMBER
(SUBSTR(a.order_no, INSTR(b.order_no, '.') -
1))

If you specify 2 or more tables in the FROM
clause of a SELECT statement, then Oracle
parser will process the tables from right to
left, so the table name you specify last will be
processed first. In this case you have to
choose one table as driving table. Always
choose the table with less number of records
as the driving table.

The structure of the FROM and WHERE clauses
of DML statements can be tailored to improve
the performance of the statement.
you should always place your driving table at the
end of the FROM clause. Subsequent driven
tables should be placed in order so that those
retrieving the most rows are nearer to the start
of the FROM clause. Confusingly, the WHERE
clause should be writen in the opposite order,
with the driving tables conditions first and the
final driven table last.
always place your driving table at the end of the
FROM clause,
the WHERE clause should be writen in the
opposite order, with the driving tables
conditions first and the final driven table last
Tampilkan semua buku beserta nama
penerbit serta nama pengarangnya!
Tampilkan nama buku ,jumlah serta nama
pembelinya, untuk semua buku yang di beli
oleh customer yang mempunyai email
dengan domain @unair.ac.id
Buat Rancangan User Interface (di gambar dikertas, manually)
untuk proyek sistem basis data yang anda kerjakan
Rancanglah sql statements untuk setiap event basis data yang
terjadi, misalnya:
form input data karyawan : sql statements insert ke tabel karyawan
Form update data karyawan : sql statements update ke tabel
karyawan
Form melihat data seluruh karyawan : sql statements select untuk
melihat data seluruh karyawan
Form melihat seorang karyawan : sql statements select untuk melihat
data seorang karyawan
Form menghapus karyawan : sql statements delete untuk menghapus
data seorang karyawan

You might also like