How to use the Code View mode effectively in Bold BI
When working with complex queries in Code View mode, it’s essential to optimize them for better performance and avoid timeout errors. This article provides some tips on how to use code view mode effectively to optimize your queries.
1. Select only the necessary columns
Instead of using SELECT *
, only select the columns that are actually needed in the query. This will reduce the amount of data that needs to be processed and may improve performance.
SELECT column1, column2, column3
FROM table_name;
2. Simplify join condition
Joining tables using more complex conditions, such as case-when statements, can lead to performance issues. To optimize query execution and enhance overall system efficiency, it is advisable to simplify join conditions.
Follow these steps to simplify join conditions:
- Identify the conditions required for each case when statement.
- Simplify the join condition by using logical operators to combine the conditions.
- Formulate the join condition as a combination of multiple simpler conditions using the AND and OR operators.
For example, instead of using case when statements inside the join, you can simplify it as follows:
Original Join Condition:
case when login = 1 then join based on one condition
when login = -1 then join based on two conditions
End
Simplified Join Condition:
(Login = 1 and one condition) OR (Login = -1 and two conditions)
3. Avoid ambiguous column references
Check your query for ambiguous column references, and make sure that all columns are properly referenced with their respective table names. If the same column name is present in multiple tables in the subquery, or if the same column name is present in multiple tables being joined, it can cause a naming conflict even if the table name is specified.
SELECT table1.column1, table2.column2
FROM table1
JOIN table2
ON table1.column1 = table2.column1;
4. Optimize query structure
Ensure your query is written in a way that minimizes unnecessary operations and filters data as early as possible. Avoid using subqueries unless necessary and consider using join operations instead.
5. Filter data efficiently
Apply filters to reduce the amount of data being processed. Use WHERE clauses to limit the result set to the necessary rows. If possible, use indexed columns for filtering.
6. Aggregate data
If your analysis requires summary information rather than individual records, use aggregate functions (such as SUM
, COUNT
, AVG
, etc.) to retrieve aggregated results. This reduces the number of rows returned and processed.
7. Avoid using functions in WHERE clauses
Functions on columns prevent the use of indexes. If possible, transform the data before storing it or create computed columns that can be indexed at the database level.
8. Analyze and optimize subqueries
If you must use subqueries, ensure they are efficient by using appropriate filtering early and limiting the result set.
By following these tips, you can optimize your queries and use code view mode effectively to improve the performance of your database operations.