Вы находитесь на странице: 1из 5

SQL Tuning/SQL Optimization Techniques:

1) The sql query becomes faster if you use the actual columns names in SELECT
statement instead of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;

2) H!I"# clause is used to filter the ro$s after all the ro$s are selected. It is %ust li&e a
filter. 'o not use H!I"# clause for any other (ur(oses.
For Example: Write the query as
SELECT subject, countsubject!
FROM student_details
"#ERE subject $% &Science&
'() subject $% &Mat*s&
+RO,- ./ subject;
Instead of:
SELECT subject, countsubject!
FROM student_details
+RO,- ./ subject
#'01(+ subject$% &0ancou2er& '() subject$% &Toronto&;

) Sometimes you may ha)e more than one subqueries in your main query. Try to
minimi*e the number of subquery bloc& in your query.
For Example: Write the query as
SELECT name
FROM em3lo4ee
"#ERE salar4, age ! % SELECT M'5 salar4!, M'5 age!
FROM em3lo4ee_details!
'() de3t % &Electronics&;
Instead of:
SELECT name
FROM em3lo4ee
"#ERE salar4 % SELECT M'5salar4! FROM em3lo4ee_details!
'() age % SELECT M'5age! FROM em3lo4ee_details!
'() em3_de3t % &Electronics&;

!) +se o(erator E,ISTS- I" and table %oins a((ro(riately in your query.
a) +sually I" has the slo$est (erformance.
") I" is efficient $hen most of the filter criteria is in the sub.query.
c) E,ISTS is efficient $hen most of the filter criteria is in the main query.
For Example: Write the query as
Select * from 3roduct 3
6*ere E51STS select * from order_items o
6*ere o73roduct_id % 373roduct_id!
Instead of:
Select * from 3roduct 3
6*ere 3roduct_id 1(
select 3roduct_id from order_items

#) +se E,ISTS instead of 'ISTI"CT $hen usin/ %oins $hich in)ol)es tables ha)in/
one.to.many relationshi(.
For Example: Write the query as
SELECT d7de3t_id, d7de3t
FROM de3t d
"#ERE E51STS SELECT &5& FROM em3lo4ee e "#ERE e7de3t % d7de3t!;
Instead of:
SELECT )1ST1(CT d7de3t_id, d7de3t
FROM de3t d,em3lo4ee e
"#ERE e7de3t % e7de3t;

$) Try to use +"I0" LL in (lace of +"I0".
For Example: Write the query as
SELECT id, first_name
FROM student_details_class89
,(1O( 'LL
SELECT id, first_name
FROM s3orts_team;
Instead of:
SELECT id, first_name, subject
FROM student_details_class89
,(1O(
SELECT id, first_name
FROM s3orts_team;

%) 1e careful $hile usin/ conditions in WHE2E clause.
For Example: Write the query as
SELECT id, first_name, age FROM student_details "#ERE age : 89;
Instead of:
SELECT id, first_name, age FROM student_details "#ERE age $% 89;
Write the query as
SELECT id, first_name, age
FROM student_details
"#ERE first_name L1;E &C*an<&;
Instead of:
SELECT id, first_name, age
FROM student_details
"#ERE S,.STRfirst_name,8,=! % &C*a&;
Write the query as
SELECT id, first_name, age
FROM student_details
"#ERE first_name L1;E (0L >name, &<&!;
Instead of:
SELECT id, first_name, age
FROM student_details
"#ERE first_name % (0L >name, first_name!;
Write the query as
SELECT 3roduct_id, 3roduct_name
FROM 3roduct
"#ERE unit_3rice .ET"EE( M'5unit_3rice! and M1(unit_3rice!
Instead of:
SELECT 3roduct_id, 3roduct_name
FROM 3roduct
"#ERE unit_3rice :% M'5unit_3rice!
and unit_3rice ?% M1(unit_3rice!
Write the query as
SELECT id, name, salar4
FROM em3lo4ee
"#ERE de3t % &Electronics&
'() location % &.angalore&;
Instead of:
SELECT id, name, salar4
FROM em3lo4ee
"#ERE de3t @@ location% &Electronics.angalore&;
+se non.column e3(ression on one side of the query because it $ill be (rocessed earlier.
Write the query as
SELECT id, name, salar4
FROM em3lo4ee
"#ERE salar4 ? AB999;
Instead of:
SELECT id, name, salar4
FROM em3lo4ee
"#ERE salar4 C 89999 ? =B999;
Write the query as
SELECT id, first_name, age
FROM student_details
"#ERE age : 89;
Instead of:
SELECT id, first_name, age
FROM student_details
"#ERE age (OT % 89;
&) +se 'EC0'E to a)oid the scannin/ of same ro$s or %oinin/ the same table
re(etiti)ely. 'EC0'E can also be made used in (lace of #20+4 15 or 02'E2 15
clause.
For Example: Write the query as
SELECT id FROM em3lo4ee
"#ERE name L1;E &Rames*<&
and location % &.angalore&;
Instead of:
SELECT )ECO)Elocation,&.angalore&,id,(,LL! id FROM em3lo4ee
"#ERE name L1;E &Rames*<&;
') To store lar/e binary ob%ects- first (lace them in the file system and add the file (ath in
the database.
1() To $rite queries $hich (ro)ide efficient (erformance follo$ the /eneral S6L
standard rules.
a) +se sin/le case for all S6L )erbs
") 1e/in all S6L )erbs on a ne$ line
c) Se(arate all $ords $ith a sin/le s(ace
)) 2i/ht or left ali/nin/ )erbs $ithin the initial S6L )erb

Вам также может понравиться