Some SQL Query Optimization Technique

Submitted by: 
Language: 
Visitors have accessed this post 2705 times.

Modern day software application has millions of concurrent users. Development of efficiently serviceable application needs huge amount of effort, usage of lot of tools and techniques. Software developers always try to improve performance of the application by improving design, cording and database development. When we consider database development query optimization and evaluation techniques are playing vital part. In this tutorial I am going to introduce some of sql query optimization techniques.

1. Selection of required field only.

It is very important to avoid unnecessary data selection of the query. We should select data field that we need but not all fields of the table.

 SELECT login_id, pawwsord FROM tbluser

2. Index

Properly created Indexes are help to optimize search result. You need to better understanding of the databases before selection of better performing index. Selection of highly utilized filed as index is very important.

 CREATE clustered INDEX ind_login_id ON tbluser(login_id)


3. Primary key

Primary key is the most important index of the table. Most important thing of the primary key is selection of short and unique field. This will leads to easy access to data records.

  1. CREATE TABLE tbluser(id INT,
  2. name VARCHAR(150),
  3. email VARCHAR(100),
  4. login_id VARCHAR(100),
  5. password VARCHAR(10),
  6. primary_key(id)
  7. )
  8.  


4. Index unique column

Indexing of unique column will improve searching and increase efficiency of the database. You must have better understanding of the data field and their utilization before indexing unique column. Indexing lower utilized column is not helping any improvement of the efficiency of the database.

CREATE INDEX ind_email ON tbluser(email)


5. Select limited records

None of the user interfaces can visualize thousand of record at ones. Hence no means of having selected all the record at once, so always limit the selection when you have large number of record set. Select required data only.

SELECT id, name, email, login_id,password FROM tbluser WHERE 1 limite 10


6. Selection of correct data type and length

Use most appropriate data type and correct length of the data. Bad selection data type will produce bulky databases and poor performance. This will improve resource utilization of the database server.

  1. CREATE TABLE tbluser(id INT,
  2. name VARCHAR(150),
  3. email VARCHAR(100),
  4. login_id VARCHAR(100),
  5. password VARCHAR(10)
  6. )

7. Avoid in sub query

Always avoid use of IN sub query your application. In sub query will evaluate all the record of table A with table B (product of records) before selecting required data.

  1. SELECT login_id,name, email FROM tbluser WHERE login_id IN ( SELECT login_id FROM tbllogin_details)
  2.  

one of correct way is used inner join

SELECT login_id,name, email FROM tbluser INNER JOIN tbllogin_details ON tbluser.login_id = tbllogin_details.login_id

8. Avoid NOT operator

Please avoid the usage of NOT operator situation that numbers of qualifying records are lower than unqualified records. Always used positive operator such as LIKE, EXIST than NOT LIKE, NOT EXIST.

 SELECT * FROM tbluser WHERE email NOT LIKE '%gmail%'

The other way is

 SELECT * FROM tbluser WHERE email LIKE '%yahoo%'


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.