How to build indexes for multiple Max() functions for SQL Server?

For some SQL statements with multiple Max() functions in the select list and nothing in the Where clause, we have different methods to create new indexes to improve the SQL speed.

Here is an example SQL, it is to retrieve the maximum name and age from the employee table.
select   max(emp_name),
     max(emp_age)
from  employee

The following is the query plan that takes 9.27 seconds.

The SQL cannot be tuned by SQL syntax rewrite or hints injection, and the SSMS cannot recommend any index to improve the SQL.

For this kind of SQL that we can consider building a composite index or two individual indexes for emp_name and emp_age. A new composite of these two columns (emp_age, emp_name) can improve the SQL around 7 times. The following is the query plan shows that the new composite index is used, but it has to scan the entire index for these two stream aggregate operations before getting the max(emp_name) and max(emp_age).

How about if we build two individual indexes for emp_name and emp_age. The following is the result and query plan of these two indexes created. A Top operator selects the first row from each index and returns to the Stream Aggregate operation, and then a Nested Loops join the two maximum results together. It is 356 times much faster than the original SQL.

This kind of indexes recommendation can be achieved by Tosska SQL Tuning Expert Pro for SQL Server automatically:
Tosska SQL Tuning Expert Pro (TSES Pro™) for SQL Server – Tosska Technologies Limited

How to build indexes for slow first execution SQL – SQL Server?

You may suffer from SQL statements with a slow first execution time due to the long data cache process. The following SQL is simple that retrieves records from the EMPLOYEE table that if EMP_SALARY < 500000 and the result set is ordered by EMP_NAME.

Select emp_id,
    emp_name,
    emp_salary,
    emp_address,
    emp_telephone
from    employee
where  emp_salary < 500000
order by emp_name;

The following is the query plan that takes 9.51 seconds for the first execution and takes 0.99 seconds for the second execution without data cache.

The SQL cannot be tuned by SQL syntax rewrite or hints injection for both the first execution and the second execution, it is because SQL Server has selected the best query plan for this simple SQL statement. But the problem is that if the condition “where emp_salary < 500000” is changed; say from 500000 to 510000 or the EMPLOYEE data is flushed out from the memory, the execution time will then be prolonged up to 9.51 seconds.

Let’s see if we can build indexes to improve this situation. There is a common perception that a good index can help to improve both the first execution time and the second execution time. So, I use a tool to explore a lot of indexes configurations, but none of them can improve both executions’ performance. Here the following is the performance of the second execution with data cached for different indexes proposed by the tool. You can see the performance of “Index Set 1” is close to the original SQL performance with a little performance variation due to the system’s loading status and all other indexes sets are worse than the original SQL. Normally, we will give up the tuning of the SQL statement without even trying to see whether those recommended indexes are good for the first execution time.

I did a test for those recommended indexes to see whether they are helpful to improve the first execution time, it surprises me that the “Index Set 1” is tested with a significant improvement and improves the first execution time from 9.51 seconds to 0.65 seconds. It is a 14 times improvement that can make my database run more efficiently. So, you should be very careful to tune your SQL with new indexes that may not be good for your second execution with all data cached, but it may be very good for your first execution without data cached.

This kind of indexes recommendation can be achieved by Tosska SQL Tuning Expert Pro for SQL Server automatically.

Tosska SQL Tuning Expert Pro (TSES Pro™) for SQL Server – Tosska Technologies Limited

How to index SQL with aggregate function SQL for Oracle?

Here the following is an example SQL shows you that select the maximum emp_address which is not indexed in the EMPLOYEE table with 3 million records, the emp_grade is an indexed column.

select max(emp_address) from employee a
where emp_grade<4000

As 80% of the EMPLOYEE table’s records will be retrieved to examine the maximum emp_address string. The query plan of this SQL shows a Table Access Full on EMPLOYEE table is reasonable.

How many ways to build an index to improve this SQL?
Although it is simple SQL, there are still 3 ways to build an index to improve this SQL, the following are the possible indexes that can be built for the SQL, the first one is a single column index and the 2 and 3 are the composite index with a different order.
1. EMP_ADDRESS
2. EMP_GRADE, EMP_ADDRESS
3. EMP_ADDRESS, EMP_GRADE

Most people may use the EMP_ADDRESS as the first choice to improve this SQL, let’s see what the query plan is if we build a virtual index for the EMP_ADDRESS column in the following, you can see the estimated cost is reduced by almost half, but this query plan is finally not being used after the physical index is built for benchmarking due to actual statistics is collected.

The following query shows the EMP_ADDRESS index is not used and the query plan is the same as the original SQL without any new index built.

Let’s try the second composite index (EMP_GRADE, EMP_ADDRESS), the new query plan shows an Index Fast Full Scan of this index, it is a reasonable plan which no table’s data is needed to retrieve. So, the execution time is reduced from 16.83 seconds to 3.89 seconds.

Let’s test the last composite index (EMP_ADDRESS, EMP_GRADE) that EMP_ADDRESS is placed as the first column in the composite index, it creates a new query plan that shows an extra FIRST ROW operation for the INDEX FULL SCAN (MIN/MAX), it highly reduces the execution time from 16.83 seconds to 0.08 seconds.

So, indexing sometimes is an art that needs you to pay more attention to it, some potential solutions may perform excess your expectation.

The best index solution is now more than 200 times better than the original SQL without index, this kind of index recommendation can be achieved by Tosska SQL Tuning Expert for Oracle automatically.

https://tosska.com/tosska-sql-tuning-expert-pro-tse-pro-for-oracle/

Creating an Index in Oracle, and the Best Way to Make Use of It

Create index oracle

Indexes are among the most useful and underutilized components of SQL. The user can create an Oracle index and store values along with their location in it.

Similar to the index at the end of a book, an index enables the user to go straight to the data they are interested in. Indexes are most useful when a user has to find a few rows. Therefore, they can use an index in statements that return a handful of rows – after creating one, of course!

Simple Techniques to Create an Index in Oracle Database

Creating an index is a simple task in MySQL query optimization as you only need to know two things:

  • The columns that require indexing
  • The name you will give the index

Here’s how to create one:

create index <indexname> on <tablename> ( <col1>, <col2>, <col3>, … <coln> );

Eg. create index cars_colour_metallic on cars (colour);

However, there are a few things to know about indexes before you begin:

  • You can place several columns in a single index, which then becomes a composite or compound index.

For instance, in the above example, you could also add the types of cars in the index like this: create index cars_colour_metallic on cars (colour, type);

  • The order in which you set columns in the index affects its use by the optimizer.

Next, let’s take a look at two of the most important index types users create in Oracle.

Two Major Index Types – and When to Pick Each

There are several kinds of indexes in the Oracle database that can improve your SQL. However, one of the most significant decisions you’ll have to make is likely to involve choosing between B-trees and bitmaps.

Create Index Oracle: B-tree Versus Bitmap Indexes

B-trees:– Indexes are in balanced B-tree format by default, which means all the leaf nodes are located at the same depth. It takes equal effort (O(log n)) to access any value, and one leaf index entry contains one row of data.

Bitmap:- Bitmaps also store indexed values, but in a completely different manner as compared to B-trees. In it, one value entry is associated with a range of row values. A bitmap has a series of 1s (yes) and 0s (no) to indicate whether any of the range rows contains the value or not.

One major difference between these two index types is that a B-tree doesn’t include null indexed values; a bitmap does. A bitmap can, therefore, answer some statements during MySQL query optimization, such as targeted index searches in which the column has a null value.

Although this won’t work for a B-tree, the user can add a constant at the end of an index to turn it into a composite index.

Bitmaps are also helpful because compressing the bits is simpler, which is why a bitmap index is generally smaller as compared to a B-tree index with identical data.

Why You Need to Keep a Check on the Indexes You Create

With all the benefits an index provides, it is important to create as few of them as possible. This is because you may end up creating one for every specific requirement and forget about them over time. The same goes for other users who may come and go on your team. And no one will have a clue why Brad needed to create that six-column function-based nightmare.

Since you don’t know if the index in question is only used for year-end reporting or never used, you cannot drop an index whenever you want. This can result in awkward situations where a table contains more indexes than columns!

So, if you’re unsure between two excellent indexes and one “good enough” index, it is better to choose the latter. And don’t forget to test!

5 Essential Paid & Free SQL Tools for Windows SQL Database

MySQL database and SQL

Effective database management requires one or more tools regardless of the platform your databases run on. The benefit of these tools is, whether they are operating on Windows, macOS, Linux, or the cloud, the tools mentioned below don’t require the same platform as the databases. 

Thanks to plenty of choices when it comes to SQL management tools, it may seem difficult to select the ones that will work best as per your specific needs. In this blog, we’ve picked out the best paid and free SQL tools for Windows along with their essential features.

Free SQL Tools for Windows with a Graphical User Interface (GUI)

Consider the following free SQL tools for Windows to help you with efficient database management:

  1. SQL Server Data Tools (SSDT)

The SSDT is designed for a variety of databases such as Azure SQL, SQL Server relational databases, RS reports(Reporting Services), IS packages(Integration Services), and AS data models (Analysis Services). It is a development tool that allows users to create and implement any SQL Server content form with a convenience identical to that of Visual Studio.

  1. SQL Server Management Studio (SSMS)

The SQL Server Management Studio tool comes with an interactive Graphical User Interface that helps users control a SQL Server database or an instance. Users can gain access to any part of the SQL Server, Azure Synapse Analytics, or the Azure SQL Database and make changes, regulate, supervise, and develop them. 

You may want to look for this tool when you download free SQL Server as it also offers an all-encompassing utility which brings together a wide range of graphical tools along with several rich script editors. These are useful to DBAs and developers of varying skill levels.

  1. Visual Studio Code

The Visual Studio Code enables users to write T-SQL scripts using a compact editor. We are talking about the mssql extension – the official extension for Visual Studio Code that supports SQL Server linking and offers a productive T-SQL editing experience.

  1. Azure Data Studio

This is also a compact and handy editor capable of running SQL various functions including –

  • Executing SQL statements whenever required
  • Organizing preferred database connections
  • Viewing and storing results in text, JSON, or Excel format
  • Exploring database objects in a familiar environment
  • Editing information

Tosska’s Range of SQL Tuning & Query Optimizer Tools

Tosska Technologies offers several solutions to improve database performance through query optimization in SQL. With the goal of introducing new technologies that will help users overcome SQL-related obstacles, they provide a range of software designed using AI technology capable of solving a broad range of database performance issues.

Here’s what this range includes:

  • Tosska SQL Tuning Expert (TSES™) for SQL Server® – Unlike some of the free SQL tools for Windows, this is a powerful tool that doesn’t require professional expertise to tune your SQL queries. The AI engine does all the work, generating the most useful hints and alternate SQL statements that are semantically equivalent to the query that’s been entered to know whether there are better execution plans. You may pick the best option among the ones provided by the engine.
  • Tosska SQL Tuning Expert (TSEM™) for MySQL® – The TSEM™ comes with the A.I. capabilities of the TSE product range. However, this one-button-solution tool is tailored specifically for the MySQL database, tuning MySQL SQL queries without the users’ intervention. Users don’t have to perform manual rewriting or use the hit-and-trial method for each troublesome query, since it’s all handled by our embedded AI engine.
  • Tosska SQL Tuning Expert for Oracle® (TSE™ and TSE Pro™) version 4 – This tool comes with features exclusive to this family of tools, such as SQL rewrite, index exploration, and injecting Oracle hints to help tune SQL queries and boost their performance. It may or may not access your source code depending on the requirements, and the tool comes with a smart Indexes Advisor that offers cost-efficient indexes as per the workload. Make sure you get it when you download free tool and take advantage of this cutting-edge technology!
  • Tosska In-Memory Maestro (TIM™) for Oracle® – The TIM™ transforms the in-memory SQL optimization process into an automatic one and gives suggestions according to the SQL workload in question through our proprietary A.I. engine. It also offers a user-friendly simulation feature that virtually assesses table objects present in the memory for a SQL workload but doesn’t occupy those table objects.