Improve SQL Queries & Database for Better Efficiency: Part 2

This is the second blog in our two-part series to explain the best ways to optimize your database, which is best done by enhancing the SQL queries being used. Without much ado, let’s pick up where we left off –

Give Preference to WHERE, instead of HAVING (when defining filters)

A query is efficient when it saves resources by fetching only what’s needed from the database. According to the Order of Operations defined in SQL, WHERE queries are calculated before HAVING statements.

Therefore, it is advisable to give preference to WHERE over HAVING when the goal is to filter a query on the basis of conditions for greater efficiency. 

For instance, let us suppose a hundred sales have been made during the year 2019, and a user wishes to put in a query to determine what the number of sales was for the same time period. They may write something like this:

SELECT Clients.ClientID, Clients.Name, Count(Sales.SalesID)

FROM Clients

   INNER JOIN Sales

   ON Clients.ClientID = Sales.ClientID

GROUP BY Clients.ClientID, Clients.Name

HAVING Sales.LastSaleDate BETWEEN #1/1/2019# AND #12/31/2019#

This statement would return at least a thousand sales records from the Sales table, then filter these thousand records to find the hundred records generated in the year 2019, and lastly, tally the data in the dataset.

If we compare the above with the same instance using the WHERE clause instead, there is a limit placed on the number of records fetched:

SELECT Clients.ClientID, Clients.Name, Count(Sales.SalesID)

FROM Clients

  INNER JOIN Sales

  ON Clients.ClientID = Sales.CustomerID

WHERE Sales.LastSaleDate BETWEEN #1/1/2019# AND #12/31/2019#

GROUP BY Clients.ClientID, Clients.Name

This statement would return the hundred records from the year 2019, after which it would count the records in the dataset, thereby getting rid of the first step in the HAVING clause.

Keep wildcards strictly at the end of a statement

A wildcard creates the largest search possible when looking for plaintext information like names or designations. However, the wider a search, the less efficient it is, and a leading wildcard worsen the performance – particularly when it’s used with an ending wildcard.

That’s because the database has to find every single record that remotely matches the selected field. Take this query to fetch cities beginning with ‘Ch’, for instance:

SELECT Cities FROM Clients

WHERE Cities LIKE ‘%Ch%’

This statement will not just fetch the expected results of Chicago, Chester, and Chelsea, but will also return unintended results, like Richardson, Canal Winchester, and Cannon Beach.

A more productive statement would be:

SELECT Cities FROM Clients

WHERE Cities LIKE ‘Ch%’

This query will lead only to the expected results of Chicago, Chester, and Chelsea.

Use LIMIT to sample query results

The use of a LIMIT query will make sure the results of new SQL queries are relevant and desirable. As the name suggests, its function is to limit the quantity of records to the number mentioned, saving a lot of resources in the process.

Considering the 2019 sales query from above, let us suppose a limit of 15 records:

SELECT Clients.ClientID, Clients.Name, Count(Sales.SalesID)

FROM Clients

  INNER JOIN Sales

  ON Clients.ClientID = Sales.ClientID

WHERE Sales.LastSaleDate BETWEEN #1/1/2019# AND #12/31/2019#

GROUP BY Clients.ClientID, Clients.Name

LIMIT 15

The results will indicate if the data set is worth using or not.

Adjust Your Timing a Bit

If you’re looking to minimize the impact of your analytical queries on the production database, consult with an Oracle Database Administrator regarding the scheduling of your SQL queries so that they can be run during off-peak hours.

Specific hours when there are fewest concurrent users, generally in the middle of the night, should be chosen to run such resource consuming queries. If your SQL queries are more likely to include the following criteria, consider running it during off-peak timings:

  • Selecting from huge tables (where there are over a million records)
  • Queries with Cartesian or Cross Joins
  • Looping queries
  • SELECT DISTINCT queries
  • Subqueries that are nested
  • Search queries involving wildcards in long text or memo areas
  • Numerous schema statements

Query with Confidence!

Keeping these and other SQL tips into consideration will certainly enable you to construct efficient, smart queries that will operate swiftly and fetch your team the game-changing insights it needs.

Oracle Database and SQL: Tips for MySQL Database Performance Tuning

Until now we have learned a lot about Oracle database and SQL performance tuning. Now, let’s discuss MySQL database performance tuning. Just like any other relational database, MySQL could also be a nightmare to many. It can crawl to a halt at a moment’s notice and in the next moment, you will find it leaving your apps in the lurch and your business on the line. The fact is, regular errors underlie most MySQL performance issues. 

To ensure your MySQL server bustles along at great speed, providing consistent and stable performance, it’s imperative to eradicate such mistakes that are often confused by some subtlety in your configuration trap or workload. Just like any other performance tuning tips for Oracle database and SQL, MySQL too has some of its own performance tuning techniques that we have described in this blog. Let’s get started without wasting more words-

Oracle Database and SQL- Know How to Tune MySQL Database Performance

Fortunately, many MySQL performance problems have similar solutions which make both tuning MySQL and troubleshooting a manageable task. 

Here are a few tips for getting great performance out of MySQL.

Tip 1# Profile Your Workload

The best way you can understand how your server invests its time is to profile its workload. By doing so you could expose the most expensive queries for further tuning. When you issue a query against the server, you don’t have to care much about anything else except how quickly it completes. Therefore, time is the most crucial metric to consider here. 

The most significant way of profiling your workload is using a tool such as MySQL Enterprise Monitor’s query analyzer. Such a tool is meant to capture queries that are executed by the server. Furthermore, it returns a table of tasks sorted by decreasing order of response time, quickly bubbling up the most time-consuming and expensive jobs to the top so that you can find where you need to put your efforts. Workload-profiling tools group identical queries together that allow you to find slow queries as well as the queries that are fast but executed multiple times. 

Tip 2# Recognize the Four Basic Resources

For functioning, a database server requires four basic resources- Network, CPU, Disk, and memory. If any of these resources are weak, overloaded, or erratic, then the database server is most likely to perform badly. Recognizing these fundamental resources is crucial in two specific areas: selecting troubleshooting and hardware issues. 

While you choose hardware for MySQL, you must ensure the components perform well all around. Just as essential, ensure to balance them reasonably well against each other. Often, businesses choose servers having fast CPUs and disks but that are starved for memory. In some cases, adding memory is a cheap way of enhancing performance by order of magnitude, specifically on workloads that are disk-bound. This might seem counterintuitive, but in various cases, disks are overused as there isn’t sufficient memory to hold the server’s working set of data. 

Another good example of this perspective relates to CPUs. In most conditions, MySQL performs well with fast CPUs as every query runs in a single thread and can’t be parallelized across CPUs. 

During troubleshooting, examine the performance and utilization of all the four resources with much care and determine whether they are performing badly or are simply being asked to execute excessive work. This knowledge can help solve issues instantly. 

Tip 3# Don’t use MySQL as a Queue

Queues and queue-like access patterns can sneak into your application without even letting you know. For instance, if you set the status of an item so that a specific work process can claim it before working on it, then you are ignorantly formulating a queue. Marking emails as unsent, sending them and then remarking them as sent in a common example. 

Queues create problems for two big reasons: they serialize your workload which prevents tasks from being done in parallel, and they mostly result in a table that includes work in process as well as earlier data from jobs that were executed long ago. Both add concealment to the application and load to MySQL. 

Tip 4# Firstly Filter Results by the Cheapest 

The best way of optimizing MySQL is to do imprecise, cheap work first, then the precise and hard work on the smaller that will result in a set of data. 

For instance, consider you are searching for something within an assigned radius of a geographical point. The initial tool in many programmers’ toolbox is the great-circle (Haversine) formula. This will compute the distance along the sphere’s surface. The trouble with this technique is that the formula needs a variety of operations related to trigonometry, which are very intensive toward the CPU. Great-circle calculations tend to perform slowly and make the machine’s CPU utilization skyrocket. 

Before you implement the great-circle formula, cut down your records to a small subset of the total, and scrape the resulting set to a precise circle. A square that comprises the circle either precisely or precisely is a simple way of doing this. That way, the world outside the square never gets hit with all those expensive trig functions. 

Tip 5# Understand the Two Scalability Death Traps

Scalability isn’t as fuzzy as you may think. In fact, there are explicit mathematical definitions of scalability that are defined as equations. Such equations emphasize why systems don’t scale and why they should. Consider the Universal Scalability Law, a definition that is convenient in expressing and quantifying a system’s scalability qualities. It explains scaling issues in terms of two elementary costs: crosstalk and serialization. 

Parallel processes that must hold for something serialized to take place are inherently limited in their scalability. Similarly, if such processes are required to chat with each other all the time for coordinating their work, they restrict each other. If you avoid crosstalk and serialization, your application could scale much better.

Improve Oracle Database Performance Tuning with Tosska’s AI-Enabled Tools

improve oracle database performance tuning

Oracle database performance tuning is a crucial method for accelerating the application function and data retrieval process. In order to speed up the performance of the application, the developer or the database administrator has to expedite query response time. This implies that they must have a deep understanding of how well the database is organized and how it satisfies its purpose.

Generally, SQL tuning means minimizing the query plan steps which in turn decreases the wait time and time cost. There are a number of ways you can implement to performance tune your Oracle database. In most of the techniques, you would require changing the source code. However, at Tosska Technologies, you will get products that are AI-based and don’t require touching the source code. Let us introduce you to our amazing products that are extremely helpful in Oracle database performance tuning.

3 Brilliant Tools Offered by Tosska for Improve Oracle Database Performance Tuning

Tosska provides solutions for SQL and database related performance optimizations and improvements. We are one of the very few companies in the world that uses artificial intelligence technology to solve different database performance problems. We help our customers to minimize their hardware investment and increase their level of database application service.

We have enlisted below some tools provided by Tosska Technologies that help to improve Oracle database performance successfully.

  1. Tosska SQL Tuning Expert (TSE™) for Oracle®

It’s an innovative machine or SQL tuning tool that upgrades your SQL statements without involving the user. By just point and click, the tool will help you with the ultimate SQL performance solution. The only thing you are required to do is to enter your problematic SQL statement into the product and press the button.

You are free from doing any testing, guessing or analyzing during the complete SQL tuning process. The enhanced SQL statement will get benchmarked with your original SQL statement side by side without any doubt. It isn’t just another tool but provides users with better statistics analysis, query plan visualization, and more.

2. Tosska SQL Tuning Expert Pro (TSE Pro™) for Oracle®

One of the best parts of having this tool is that it will tune your Oracle database without touching your source code. Surprised? But, it’s true. Tosska SQL Tuning Expert Pro is a tool that helps in upgrading SQL performance without even touching the source code of the program. Besides this, users can even use various other performance query plans for different sizes of production databases without the need of keeping a number of versions of the program source. It is specially designed for package application users who don’t hold the source code of their applications.

With our SQL Tuning Expert Pro, SQL tuning gets even easier for application developers than ever before. You can improve the SQL statement without conducting any time-consuming test, program implementation, and integration test that are essential in the software development cycle. Apart from these brilliant SQL tuning attributes, strong indexes recommendation function is also given that assists users in reviewing and discovering more potential indexes that are extremely crucial but may be missing in the current database schema.

To be more specific, it’s an extremely brilliant and cost-aware index advice engine that lets users analyze their existing database schema- in case any new indexes are available that are helpful in enhancing an assigned SQL workload. The engine is capable of handling up to thousands of SQL statements and offers you a reasonable recommendation that even human experts are not able to accomplish.

3. Tosska In-Memory Maestro (TIM™) for Oracle®

Tosska In-Memory Maestro for Oracle is a tool that automates the process of optimizing In-memory SQL and offers recommendations for In-memory objects for a given SQL workload. Our proprietary artificial intelligence engine is highly beneficial in achieving the aforesaid. The tool also offers a user-friendly In-memory simulation function for users so that they can virtually estimate their in-memory table objects for one or more SQL workload captured from AWR or SGA without actually populating those table objects.

Final Words

Nowadays, most of the highly succeeding companies make use of artificial intelligence to upgrade themselves, then why not you too implement it! With our AI-enabled tool, you can improve oracle database performance easily and in less time. Even, in one of our tools, the source code of the program will remain untouched, yet the problem will be solved. The tools really don’t require the user’s involvement in fixing the queries. Therefore, if you are searching for any such product, then browse our products and get the one that suits you the best.

Tosska Technologies aims at helping users to smooth out the problem with the new and advanced technologies in the market. You should surely give it a try; we assure you will love the product offered by us.