As a dynamic database developer in the banking sector, I'm always on the hunt for quicker, smarter ways to optimize queries. Join me on an exhilarating journey through my handcrafted list of top-notch techniques, guaranteed to rev up your query performance and leave you amazed!
1. Avoid SELECT *.
Use SELECT fields instead of SELECT *
Retrieving all columns with SELECT * may cause unnecessary overhead when querying a large volume of data. Instead, specify the exact columns you need to retrieve.
Example Query:
-- Avoid
SELECT * FROM Employees;
-- Recommended
SELECT FirstName, LastName, Email, Login FROM Employees;
For instance, you can create a covering index for the "Employees" table:
CREATE NONCLUSTERED INDEX IDX_Users_Covering ON Employees INCLUDE (FirstName, LastName, Email, Login);
2. Reduce data before JOIN
Perform aggregations to minimize data processing before a Join.
Aggregating data before joining tables reduces the volume of data processed during the join operation, leading to improved performance.
Example Query:
GROUP BY DepartmentName;
3. Use Joins instead of Subqueries to optimize query execution.
Using JOINs instead of subqueries can lead to more efficient execution plans, resulting in faster query performance.
Example Query:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
4. Avoid using View name
Use views query in the statement for better performance.
Avoid using views directly in the query, as it might cause the entire view's logic to be evaluated, leading to unnecessary overhead. Instead, use the view's query directly in the statement.
Example Query:
-- Avoid
SELECT * FROM View_Name;
-- Recommended
SELECT * FROM (SELECT col1, col2, col3 FROM Some_Table) AS View_Query;
5. Minimize Function usage
Limit the usage of functions in queries to enhance performance.
Overuse of functions in queries can impact performance. Minimize their usage where possible.
Example Query:
-- Avoid using this statement
SELECT
first_name,
last_name,
fnc_attendence(emp_id) AS total_dept
FROM
employees;
-- Use this statement
SELECT
first_name,
last_name,
(SELECT COUNT(attendence_id) FROM attendence WHERE at_emp_id = e.emp_id) AS total_dept
FROM
employees e;
6. Optimize with Indexes
Use indexes on columns used in WHERE clauses to speed up data retrieval.
Indexes can significantly improve query performance, especially when filtering on indexed columns.
Example Query:
CREATE INDEX idx_Products_CategoryID ON Products(CategoryID);
7. Maintain Column Order
Keep the order of the select columns as they appear in the table to optimize query processing.
Keeping the order of the columns in SELECT statements aligned with the table structure can optimize query execution.
Example Query:
SELECT EmployeeID, FirstName, LastName FROM Employees;
8. User Defined Function
One approach to optimize the query is to create a User-Defined Function (UDF).
The PL/SQL function remains unchanged, but the additional PRAGMA UDF directive informs the compiler that the function is mainly intended for use in SQL statements.
Here's an example of how to create the UDF:
CREATE OR REPLACE FUNCTION calculate_total_cost
(in_quantity NUMBER,in_unit_price NUMBER) RETURN NUMBER
IS
PRAGMA UDF;
v_total_cost NUMBER;
BEGIN
v_total_cost := in_quantity * in_unit_price;
RETURN v_total_cost;
END;
By adding PRAGMA UDF, the query becomes almost as fast as the function-based index in the initial query but without the overhead of maintaining the index. This optimization reduces the query execution time significantly, making it perform a full table scan on the ORDER_ITEMS table in just 0.3 seconds instead of 10 seconds. In this example, the improvement is around 97% due to reduced context switches.
9. Avoid multiple OR in FILTER predicates
Replace multiple OR conditions with UNION or split the query to improve performance.
Multiple OR conditions can negatively impact query performance. Use UNION or split the query when dealing with multiple OR conditions.
Example Query:
-- Avoid
SELECT * FROM Employees WHERE Department = 'HR' OR Department = 'Finance';
-- Recommended
SELECT * FROM Employees WHERE Department = 'HR' UNION SELECT * FROM Employees WHERE Department = 'Finance';
10. Mindful JOINs
Ensure JOINs are utilized appropriately to avoid data duplication and optimize queries.
Carefully plan JOIN operations to avoid redundant data and optimize query performance.
Example Query:SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
rajuddx free tag -
#bytebotsolution #Oracle #Query #Optimization #Performance #SQL #Tune #Faster #Tips #Database #Developer #Speed #Efficient #Accelerate #Techniques #Turbocharge #Hacks #Strategies #Boost #Mastery #Tricks #Results #Efficiency #Secrets #Tuning #Wizardry #Improve #Enhance #Acceleration #Hints #Strategies
0 Comments