How to Optimize Sql Query
Ads by Google
What is Query Optimization in SQL?
A query is the statements for interacting with databases and tables. Sometimes we observe the same results with different SQL queries but at that time the selection of best query is an important question and also performance. To access data fast from your Server or Database it totally depends on your query. That’s why we optimize the query.
How can We Optimize the SQL Query?
We can increase the speed of query or optimize the query with a lot of tricks. Some of the tricks are given below:
- The SQL query becomes faster if you use the names of the actual columns in a SELECT statement instead of than ‘*’. For Example: Write the query as
SELECT Student_id , fname , lname , age , Department, major
FROM students;
Instead of:
SELECT * FROM students;
- We used HAVING clause for filter the record after selecting the all records from a given table. But some developers used HAVING clause for other purposes, that means your query will not work faster.
For Example:
SELECT Department , count(Department)
FROM students
WHERE Department ! = ‘ Computer Science ‘ AND major ! = ‘ Computer ‘
GROUP BY Department;
Instead of:
SELECT Department, count(Department)
FROM students
GROUP BY Department
HAVING Department! = ‘ Computer Science ‘ AND major! = ‘ Computer ‘
- Try to minimize the subqueries, because sometimes you may have more than one subqueries in your main query block. A simple example of this trick is given below:
SELECT fname , lname
FROM customer
WHERE ( salary , age ) = ( SELECT MAX ( salary ) , MAX ( age ) FROM customer_details ) AND Department = ‘ IT ‘ ;
Instead of:
SELECT fname , lname
FROM customer
WHERE salary = ( SELECT MAX ( salary ) FROM customer_details ) AND age = ( SELECT MAX ( age ) FROM customer_details ) AND Department = ‘ EE ‘ ;
- Proper use of these operators in EXISTS, IN, LIKE, AND, OR, UNION, WHERE and COUNT in your query.
Select * from product p
where EXISTS ( select * from order where product.product_id = order.product_id )
Instead of:
Select * from product p
where product_id IN ( select product_id from order )
- Use carefully UNION clause in your query. For example, a simple query is given below.
SELECT std_id , fname
FROM students UNION ALL ( SELECT std_id , fname FROM programmingTeam ) ;
Instead of:
SELECT std_id , fname
FROM students UNION ( SELECT std_id , fname FROM ProgrammingTeam ) ;
Also a lot of tips and tricks that can help a data administrator to increase the speed and optimize the query.
Optimize the Query According to Database
The query speed and your database performance depends on a lot of factors, your database model, you add or not Indexes in your database and also your query length or kind of query. For example you are retrieving information from one or multiple tables.
What are query optimization techniques?
What is the goal of query optimization?
What are query techniques?
- OR in the Join Predicate/WHERE Clause Across Multiple Columns.
- Over-Indexing a Table.
- Under-Indexing a Table.
- No Clustered Index/Primary Key.
How can I improve my SQL skills?
- Make SQL Part of Your Work Day.
- Document Your SQL Learning Experience.
- Produce Reports using SQL for your business.
- Share Your SQL Knowledge with Others.
- Volunteer or Freelance on an SQL or Database Project.
- Learn SQL Early in Your Career.
- Once You Know SQL, Look at Performance.
How do I optimize multiple joins query?
What are the basic steps of query optimizer?
What is physical query plan?
How does query optimizer work?
What is SQL performance tuning?
How do I select a SQL query?
- SELECT column1, column2, FROM table_name;
- SELECT * FROM table_name;
- Example. SELECT CustomerName, City FROM Customers; Try it Yourself »
- Example. SELECT * FROM Customers; Try it Yourself »
What is the purpose of a query processor?
What are the main phases of query processing?
What is query performance?
What is SQL process?
What triggers SQL?
Ads by Google