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.

Tosska Technologies announces the availability of Tosska SQL Tuning Expert (TSEM™) for MySQL® version 1.1.0

performance tune sql query

Probably the only AI-based machine SQL tuning product for the MySQL® database

Apr. 10, 2020

Hong Kong, Apr. 10, 2020 – Tosska Technologies Limited (Tosska), an IT company that provides database and SQL performance related tools using Artificial Intelligent technology, today announces the general availability of Tosska SQL Tuning Expert (TSEM™) for MySQL® version 1.1.0, making the easy-to-use “one-click machine tuning” product for SQL performance improvement now available for the MySQL® database.

SQL Tuning is a classical problem for every DBAs. It exists in every databases including the MySQL® database that now have thousands of mission critical applications running on it everyday. It’s been a long time DBAs in the MySQL world has been looking for an easy-to-use yet powerful tool for their SQL tuning needs. With Tosska SQL Tuning Expert (TSEM™) for MySQL®, MySQL DBAs’ dream now come true. MySQL DBAs now can do one-click SQL tuning without human intervention. Making this tedious, and sometimes impossible SQL tuning task possible and easy.

“Users are thrilled with the benefits that our Tosska SQL Tuning Expert for Oracle® product series have brought to them in their day-to-day SQL Tuning works. Many have been asking us when we can bring the power of this one-click easy-to-use SQL Tuning tool to the MySQL® world. Today I am proud to announce the general availability of Tosska SQL Tuning Expert (TSEM™) for MySQL®, which probably is the only product in the world now that provide AI-based machine SQL tuning capability for the MySQL® database.” said KaMing Ng, CEO of Tosska.

“There have been noise in the MySQL® market that they need a robust SQL tuning tool to ease their work. Unfortunately none of those existing SQL tuning tools in the market meets that need. As such we decided to build a MySQL® specific SQL tuning tool of our own. Tosska SQL Tuning Expert (TSEM™) for MySQL® uses the same AI SQL machine tuning technology in Tosska’s TSE™ for Oracle® product series but is modified and optimized specifically for the MySQL database. Besides that, we have also added Tosska proprietary tree plan for MySQL® database that not only has rich satistics information like the Tubular Explain from MySQL®, but also has a heirachical structure like what is displaying in Visual plan. The beauty is that all such information can be displayed in a small window for easy reading.” said Richard To, CTO of Tosska.

Tosska SQL Tuning Expert (TSEM™) for MySQL® is now available for free trial download from Tosska’s website. Please visit Tosska’s website www.tosska.com for details.

   
About Tosska
Tosska Technologies is a company that focuses in providing solutions for database and SQL related performance optimization and improvements. Our mission is to help users to smooth out the hurdle by our new technologies. Furthermore, Tosska is one of the very few companies in the world that focus in using artificial intelligence technology to solve various database performance problems. It is our goal to help our customers to reduce their hardware investment; increase their database applications service level and free up their human resources for more strategic activities with our innovative technologies. For more information visit www.tosska.com or email us at enquire@tosska.com.

Press and Media Inquiries
KaMing Ng
Chief Executive Officer
Tosska Technologies Limited
Phone: +852-21501987
Email: enquire@tosska.com

All Trademarks mentioned on this Site are the property of their respective owners.

Reveal the power of CBO

download free sql server

Example to Unveil the Power of Oracle Cost-Based SQL Optimizer

A user who has a SQL statement takes a long time to execute, actually the SQL is not very complicated, but it has a very complex execution plan.

Mimic SQL text :
  SELECT TO_CHAR(SYSDATE, ‘yyyy-mm-dd’) AS STAT_DATE,
        SYSDATE AS STAT_TIME,
        X.TABLE_NAME,
        NVL(X.NUM_ROWS, 0) AS TABLE_ROWS,
        NVL(ROUND(X.NUM_ROWS * X.AVG_ROW_LEN / 1024, 2), 0) AS TABLE_SIZES,
        Y.CREATED AS CREATE_TIME,
        Z.COMMENTS AS TABLE_COMMENT,
        H .COL_CNT
  FROM ALL_TABLES X,
        ALL_OBJECTS Y,
        ALL_TAB_COMMENTS Z,
        (SELECT H .TABLE_NAME, COUNT(1) AS COL_CNT
          FROM ALL_TAB_COLS H
          WHERE H.OWNER = ‘TOSSKA’
          GROUP BY H .TABLE_NAME) H
  WHERE X.TABLE_NAME = Y.OBJECT_NAME
       AND X.TABLE_NAME(+) = Z.TABLE_NAME
       AND X.TABLE_NAME = H.TABLE_NAME
       AND Y.OBJECT_TYPE IN (‘TABLE PARTITION’, ‘TABLE’)
       AND X. OWNER = ‘TOSSKA’
       AND Y. OWNER = ‘TOSSKA’
       AND Z. OWNER = ‘TOSSKA’
  ORDER BY X.TABLE_NAME

Execution Plan:
The following partial plan steps list about 10% of a total of 214 steps of execution plan.

User input the SQL into Tosska SQL Tuning Expert for Oracle, and press Tune to start exploring if there are only potential better query plans from Oracle. User goes back to his daily work and let the computer do the rest of tuning job on his behalf. Finally, there are 5 better alternatives of SQL which are found within an hour.

Product Highlight
Tosska SQL Tuning Expert equipped with an AI engine which can try most effective combinations of Hints injection to the problematic SQL. The engine can deeply explore any hidden good execution plans that Oracle CBO cannot discover at the stage of online SQL optimization, but those good plans actually exist in the plans space for the given SQL statement.


Benchmark Result:
Original SQL takes 14 minutes and 7 seconds.
The best alternative SQL with hints injected is SQL 45 and it takes 4 seconds only.
SQL45 is running more than 99 times faster than the Original SQL.

Observation of the query plans generated by Oracle CBO
Tosska SQL Tuning Expert is a Hints-Injection-Based SQL tuning tool without the need of rewriting user’s SQL text. So, various hints injected to a SQL statement and the corresponding query plans generated by Oracle are all potential query plans that Oracle SQL optimizer can provide for the SQL statement.
Let’s review those generated query plans and why Oracle cannot find the best query plan at the beginning.

First observation:
Original SQL’s cost is 1330, but the cost of SQL 130, SQL 135 and SQL 45 are all lower than Original SQL’s cost, why can’t Oracle pick up these lower cost plans?
Reason:
Oracle cannot explore all potential query plans that it can generate within a short time, otherwise it will take even longer time to optimize a given SQL that might not be fully compensated by an unforeseeable better query plan.

Second observation:
SQL 127 and SQL 129 have 3 times higher cost than Original SQL, but the speed is much faster than Original SQL, it means the cost estimation of these two SQL are exceptionally wrong.
Reason:
It is the limitation of cost estimation algorithm used in database SQL optimizer. Theoretically, there is no 100% accurate SQL cost estimation algorithm in the market that can handle various environments, and the problem is especially true for complex SQL statements like the above SQL statement.

Conclusion:
Oracle has the most sophisticated SQL optimizer in the market. There are a lot of better query plans that Oracle are potential can run faster for your SQL statements. So, whenever you are thinking to upgrade your hardware or cloud service spending, you should first explore the potential power that Oracle SQL optimizer can provide for your SQL statements, and what you need is only a right tool that can unveil the potential power of your Oracle SQL optimizer.

Tosska SQL Tuning Expert (TSE™) for Oracle®

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

Tosska Technologies announces the availability of Tosska SQL Tuning Expert Pro (TSE Pro™) for Oracle® version 2.0.8

performance tune sql query

Bringing the automatic SQL tuning technology to new heights

Dec. 20, 2018

Hong Kong, Dec. 20, 2018 – Tosska Technologies Limited (Tosska), an IT company that provides database and SQL performance related tools using Artificial Intelligent technology, today announces the general availability of Tosska SQL Tuning Expert Pro (TSE Pro™) version 2.0.8, an advanced version of their “one-click machine tuning” product for SQL performance improvement for Oracle®.

SQL Tuning is a classical problem for every DBAs. It requires in-depth knowledge of SQL tuning skill. With Tosska SQL Tuning Expert Pro (TSE Pro™), DBAs finally have the ultimate SQL tuning tool that can ease their day-to-day tuning job by simple one-click tuning process. TSE Pro™’s Index Adviser also helps DBAs to explore the possibility to further improve SQL performance using indexes yet with good work load balancing for all SQLs involved.

“Continuous improvement to our product is a key R&D directive for Tosska. Though we have received a lot of positive feedback on our TSE Pro™ product, we are keeping on looking for ways to improve it further. The newly announced TSE Pro™ version 2 comes with an enhanced tuning knowledge-base that supports almost double the number of hints comparing to our previous version, bringing the automatic SQL tuning technology to new heights. It creates a new standard of Hints-injection-based SQL tuning ability that no other tools can achieve in the market.” said KaMing Ng, CEO of Tosska. “This increase in supported hints means that more problematic SQL can be improved and the actual test result shows that even very simple SQL statements still have chance to be improved in various environments.”

“We have been keeping our ears open and listen to feedbacks from our users around the world. As such, in TSE Pro™ version 2, we have added a new ‘Tune Top SQLs’ module that can help user to extract high workload SQL statements from SGA and AWR into a ‘Top SQL Repository’ and store in local PC. It is a very useful function for DBA to identify problematic SQL statements without the need of going through thousands of lines in program source codes. Each identified problematic SQL statement can be tuned by just a point-and-click in the Top SQL Repository window, and it is tightly integrated with the Tune SQL function to streamline the flow from problematic SQL identification to SQL optimization. Users can also make any remarks on specific SQL statement as a reminder for follow-up actions.” said Richard To, CTO of Tosska.

TSE Pro™ version 2 is now available for free trial download from Tosska’s website. Please visit Tosska’s website www.tosska.com for details.

   
About Tosska
Tosska Technologies is a company that focuses in providing solutions for database and SQL related performance optimization and improvements. Our mission is to help users to smooth out the hurdle by our new technologies. Furthermore, Tosska is one of the very few companies in the world that focus in using artificial intelligence technology to solve various database performance problems. It is our goal to help our customers to reduce their hardware investment; increase their database applications service level and free up their human resources for more strategic activities with our innovative technologies. For more information visit www.tosska.com or email us at enquire@tosska.com.

Press and Media Inquiries
KaMing Ng
Chief Executive Officer
Tosska Technologies Limited
Phone: +852-21501987
Email: enquire@tosska.com

All Trademarks mentioned on this Site are the property of their respective owners.

Tosska Technologies announces the availability of Tosska SQL Tuning Expert (TSE™) for Oracle® version 1.5.0

query performance tuning

An innovative machine tuning tool for SQL statements further enhanced

June. 23, 2018

Hong Kong, June 23, 2018 – Tosska Technologies Limited (Tosska), an IT company that provides database and SQL performance related tools, today announces the general availability of Tosska SQL Tuning Expert (TSE™) version 1.5.0, the enhanced version of their first “one-click machine tuning” product for SQL performance improvement for Oracle®.

SQL Tuning is a classical problem for every DBAs. Tuning SQL statements is finding the fastest route to execute the SQL statements. In order to tune a SQL statement, you are required to know your database architecture and have in-depth knowledge of SQL tuning skill.

“After the first release of Tosska SQL Tuning Expert (TSE™) back in Dec. 2017, we have seen a high download rate of the product everyday. People are excited about the convenience and power that TSE™ has brought to them in their day-to-day SQL tuning requirements. Many of them have shared with us their successes in using the product and there are cases where SQLs are improved by over 21 times !” said KaMing Ng, CEO of Tosska excitedly. “There are also a lot of cases where TSE™ provided solutions that DBAs have never thought of, if not using the product. Thanks to the innovative proprietary Artificial Intelligent (AI) engine behind the product that can explore rare SQL alternatives that most DBAs won’t thought of just by their experience.”

“Earlier this year, we have released a new line of SQL Tuning solutions called Tosska SQL Tuning Expert Pro (TSE Pro™), the Professional Versions of the TSE™ series that provides SQL Tuning without the need of source code and with index advise capability. But with the overwhelming success of TSE™, we will NOT stop enhancing our TSE™ product line. That’s why we have released the latest enhanced version of TSE™, version 1.5.0 today,” Mr. Ng continued.

“We have been receiving a lot of positive feedbacks from the public since the availability of TSE™. As a continuous improvement to the capability and user experience of the product, we now make version 1.5.0 general available. TSE™ version 1.5.0 supports the MERGE statement as well as better problem solving ability with more SQL alternatives generation. It now also provides new Bind Variables window and incorporated some minor bug fixes,” said Richard To, CTO of Tosska.

TSE™ version is now available for free download from Tosska’s website. Please visit Tosska’s website www.tosska.com for details.

   
About Tosska
Tosska Technologies is a company that focuses in providing solutions for database and SQL related performance optimization and improvements. Our mission is to help users to smooth out the hurdle by our new technologies. Furthermore, Tosska is one of the very few companies in the world that focus in using artificial intelligence technology to solve various database performance problems. It is our goal to help our customers to reduce their hardware investment; increase their database applications service level and free up their human resources for more strategic activities with our innovative technologies. For more information visit www.tosska.com or email us at enquire@tosska.com.

Press and Media Inquiries
KaMing Ng
Chief Executive Officer
Tosska Technologies Limited
Phone: +852-28248420
Email: enquire@tosska.com

All Trademarks mentioned on this Site are the property of their respective owners.

Tosska Technologies announces the availability of their major SQL performance tuning product – Tosska SQL Tuning Expert Pro (TSE Pro™) for Oracle®

query performance tuning

Intelligent SQL tuning without touching your source code

May. 15, 2018

Hong Kong, May 15, 2018 – Tosska Technologies Limited (Tosska), an IT company that provides database and SQL performance related tools, today announces the general availability of Tosska SQL Tuning Expert Pro (TSE Pro™), the major product of the Tosska SQL Tuning Expert Family of Products that provides state-of-the-art machine tuning capability to improve SQL performance for Oracle® without touching the source code.

SQL Tuning is a classical problem for every DBAs. Tuning SQL statements is finding the fastest route to execute the SQL statements. There have already been a lot of SQL tuning products in the market providing better query plan visualization, better statistics analysis, high cost query plan steps indication or even rule-of-thumb syntax recommendations. All in all, those tools are not helpful if users don’t have in-depth SQL tuning knowledge and are not willing to spend extra effort to tune a SQL apart from their daily duties.

Users are eager for one-button-solution tool that can tune a SQL statement automatically without the need of users’ intervention. The Tosska SQL Tuning Expert Product family aims to help DBAs to do SQL tuning just by multiple easy points and clicks.

“Tosska SQL Tuning Expert Pro is a tool for users to improve SQL performance without touching their program source code. Users can even deploy different performance query plans for various sizes of production databases without the effort of keeping multiple versions of the program source. It is especially suitable for package application users who don’t own the source code of their applications,” said KaMing Ng, CEO of Tosska. “If you are a packaged application user, how can you tune your SQL if you can’t edit a query directly? That’s why we decided to expand the TSE Product family and developed TSE Pro™ to solve this problem.”

“There are multiple features provided by Oracle such SQL Profiles, SQL Plan Baselines and SQL patch that you can use to tell Oracle to fix a SQL’s query plan. But the use of these features is limited by Hints injection only, you cannot rewrite a SQL with different syntax and ask the original SQL to accept a rewritten SQL’s query plan. So, hints-based SQL tuning is becoming more important than ever before. Tosska SQL Tuning Expert Pro is the only tool that can provide the most advanced Auto-Hints-Injection solution in the market to fully automate the process from SQL tuning to plan deployment,” said Richard To, CTO of Tosska. “TSE Pro™ also provides an intelligent workload-based index advisor that helps users to review their existing database schema if there are any new indexes which can help to improve a given SQL workload. The TSE Pro™ uses our proprietary Artificial Intelligent (AI) engine which can handle up to thousands of SQL statements and give you a reasonable recommendation that even human experts cannot achieve.”

Free Trial of TSE Pro™ is now available for download from Tosska’s website. Please visit Tosska’s website www.tosska.com for details.

   
About Tosska
Tosska Technologies is a company that focuses in providing solutions for database and SQL related performance optimization and improvements. Our mission is to help users to smooth out the hurdle by our new technologies. Furthermore, Tosska is one of the very few companies in the world that focus in using artificial intelligence technology to solve various database performance problems. It is our goal to help our customers to reduce their hardware investment; increase their database applications service level and free up their human resources for more strategic activities with our innovative technologies. For more information visit www.tosska.com or email us at enquire@tosska.com.

Press and Media Inquiries
KaMing Ng
Chief Executive Officer
Tosska Technologies Limited
Phone: +852-28248420
Email: enquire@tosska.com

All Trademarks mentioned on this Site are the property of their respective owners.

Tosska Technologies announces the availability of their flagship SQL performance tuning product – Tosska SQL Tuning Expert (TSE™) for Oracle®

sql query optimization tool online

An innovative machine tuning tool for SQL statements

Dec. 18, 2017

Hong Kong, December 18, 2017 – Tosska Technologies Limited (Tosska), an IT company that provides database and SQL performance related tools, today announces the general availability of Tosska SQL Tuning Expert (TSE™), their first product that provide machine tuning capability to improve SQL performance for Oracle®.

SQL Tuning is a classical problem for every DBAs. Tuning SQL statements is finding the fastest route to execute the SQL statements. In order to tune a SQL statement, you are required to know your database architecture and have in-depth knowledge of SQL tuning skill.

“There are a lot of SQL tuning tips in the market, but most of them are good only in specific database environment. You need to try those tips one by one manually on your databases. It is not only time-consuming, but there is also no guarantee that you will find the best performance solution for your SQL statements,” said KaMing Ng, CEO of Tosska. “It may be up to days or weeks for a DBA or experienced SQL developer to tune a problematic SQL statement. SQL tuning is a very skillful job that not many developers are able to carry out in an enterprise. Should such expensive and valuable time be saved and used for other even more productive tasks inside a company? That’s why we designed TSE to solve this problem.”

“The design concept behind TSE™ is to provide a machine tuning tool that optimizes SQL statements without the need of user’s involvement. Users just need to input their problematic SQL statements into the tool and press a button and then the tool will take care the rest. Users don’t have to do analysis, guessing or testing during the entire SQL tuning process,” said Richard To, CTO of Tosska. “TSE™ achieves this by using a proprietary technology with an embedded Artificial Intelligent (AI) engine invented by Tosska to mimic a human expert SQL tuning process, in which the engine tries every possible effective Oracle Hints combinations for a SQL statement to improve the execution speed within the given quota. As the permutation of Oracle Hints combinations to a SQL statement is so huge, it is impossible for a human expert to accomplish it for complex SQL statements.”

TSE™ is now available for free download from Tosska’s website. Please visit Tosska’s website www.tosska.com for details.

   
About Tosska
Tosska Technologies is a company that focuses in providing solutions for database and SQL related performance optimization and improvements. Our mission is to help users to smooth out the hurdle by our new technologies. Furthermore, Tosska is one of the very few companies in the world that focus in using artificial intelligence technology to solve various database performance problems. It is our goal to help our customers to reduce their hardware investment; increase their database applications service level and free up their human resources for more strategic activities with our innovative technologies. For more information visit www.tosska.com or email us at enquire@tosska.com.

Press and Media Inquiries
KaMing Ng
Chief Executive Officer
Tosska Technologies Limited
Phone: +852-28248420
Email: enquire@tosska.com

All Trademarks mentioned on this Site are the property of their respective owners.