SQL Query Optimization Technique

Modern day software applications have millions of concurrent users. Development of an efficiently serviceable application requires a huge amount of effort and requires many tools and techniques. Software developers always try to improve the performance of the application by improving design, coding and database development. For database development, query optimization and evaluation techniques are playing vital parts.

Selection of required field only.

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

  1. SELECT login_id, pawwsord FROM tbluser  
Index

Properly created Indexes help to optimize search results. You need to better understand the databases before the selection of a better performing index. The selection of a highly used field as an index is very important.

  1. CREATE clustered INDEX ind_login_id ON tbluser(login_id)  
Primary Key

The Primary Key is the most important index of the table. The most important thing about a Primary Key is the selection of a short and unique field. This will lead to easy access to the data records.

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

The indexing of a unique column will improve searching and increase the efficiency of the database. You must have a better understanding of the data field and their utilization before indexing a unique column. The indexing of a less used column does not help improve the efficiency of the database.

  1. CREATE INDEX ind_email ON tbluser(email)  
Select limited records

None of the user interfaces can visualize thousands of records at once. Hence there is no way to select all the records at once, so always limit the selection when you have a large number of records. Select the required data only.

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

Selection of correct data type and length

Use the most appropriate data type and correct length of the data. The bad selection of a 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. )  
Avoid in sub query

Always avoid use of IN sub-queries in your applications. An In sub-query will evaluate all the records of table A with table B (product of records) before selecting the required data.

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

One of the correct ways is to use an inner join as in the following:

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

Avoid NOT operator

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

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

The prefered way is:

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


Similar Articles