


Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)
Usually sql database needs to be optimized and analyzed, and there are certain skills. Several methods of sql optimization will not be introduced in detail here. This article will summarize the sql statement optimization, and the optimization tool SQL Tuning Expert is also attached. for Oracle and how to use it, first we must follow several principles of database optimization:
1. Try to avoid doing operations on columns, which will cause index failure;
2. Use join It is necessary to use a small result set to drive a large result set, and at the same time split the complex join query into multiple queries. Otherwise, the more tables you join, the more locks and congestion will occur.
3. Pay attention to the use of like fuzzy queries and avoid using %%, for example, select * from a where name like '�%';
Replace the statement: select * from a where name > = 'de' and name
4. Only list the fields that need to be queried, do not use select * from..., save memory;
5. Use batch Insert statements to save interaction;
insert into a (id ,name) values(2,'a'), (3,'s');
6. When the limit base is relatively large, use between ... and ...
7. Do not use the rand function to randomly obtain records;
8. Avoid using null, which requires setting it to not null as much as possible when creating a table to improve query performance;
9, do not use count(id), but count(*)
10. Don’t do unnecessary sorting, complete the sorting in the index as much as possible;
Let’s look at a sql first:
select ii.product_id, p.product_name, count(distinct pim.pallet_id) count_pallet_id, if(round(sum(itg.quantity),2) > -1 && round(sum(itg.quantity),2) < 0.005, 0, round(sum(itg.quantity),2)) quantity, round(ifnull(sum(itag.locked_quantity), 0.00000),2) locked_quantity, pc.container_unit_code_name, if(round(sum(itg.qoh),2) > -1 && round(sum(itg.qoh),2) < 0.005, 0, round(sum(itg.qoh),2)) qoh, round(ifnull(sum(itag.locked_qoh), 0.00000),2) locked_qoh, p.unit_code, p.unit_code_name from (select it.inventory_item_id item_id, sum(it.quantity) quantity, sum(it.real_quantity) qoh from ws_inventory_transaction it where it.enabled = 1 group by it.inventory_item_id ) itg left join (select ita.inventory_item_id item_id, sum(ita.quantity) locked_quantity, sum(ita.real_quantity) locked_qoh from ws_inventory_transaction_action ita where 1=1 and ita.type in ('locked', 'release') group by ita.inventory_item_id )itag on itg.item_id = itag.item_id inner join ws_inventory_item ii on itg.item_id = ii.inventory_item_id inner join ws_pallet_item_mapping pim on ii.inventory_item_id = pim.inventory_item_id inner join ws_product p on ii.product_id = p.product_id and p.status = 'OK' left join ws_product_container pc on ii.container_id = pc.container_id //总起来说关联太多表,设计表时可以多一些冗余字段,减少表之间的关联查询; where ii.inventory_type = 'raw_material' and ii.inventory_status = 'in_stock' and ii.facility_id = '25' and datediff(now(),ii.last_updated_time) < 3 //违反了第一个原则 and p.product_type = 'goods' and p.product_name like '%果%' // 违反原则3 group by ii.product_id having qoh < 0.005 order by qoh desc
In the above sql, we used the subtitle in the from Query, this is very detrimental to the query;
A better approach is the following statement:
select t.facility_id, f.facility_name, t.inventory_status, wis.inventory_status_name, t.inventory_type, t.product_type, t.product_id, p.product_name, t.container_id, t.unit_quantity, p.unit_code, p.unit_code_name, pc.container_unit_code_name, t.secret_key, sum(t.quantity) quantity, sum(t.real_quantity) real_quantity, sum(t.locked_quantity) locked_quantity, sum(t.locked_real_quantity) locked_real_quantity from ( select ii.facility_id, ii.inventory_status, ii.inventory_type, ii.product_type, ii.product_id, ii.container_id, ii.unit_quantity, ita.secret_key, ii.quantity quantity, ii.real_quantity real_quantity, sum(ita.quantity) locked_quantity, sum(ita.real_quantity) locked_real_quantity from ws_inventory_item ii inner join ws_inventory_transaction_action ita on ii.inventory_item_id = ita.inventory_item_id where ii.facility_id = '{$facility_id}' and ii.inventory_status = '{$inventory_status}' and ii.product_type = '{$product_type}' and ii.inventory_type = '{$inventory_type}' and ii.locked_real_quantity > 0 and ita.type in ('locked', 'release') group by ii.product_id, ita.secret_key, ii.container_id, ita.inventory_item_id having locked_real_quantity > 0 ) as t inner join ws_product p on t.product_id = p.product_id left join ws_facility f on t.facility_id = f.facility_id left join ws_inventory_status wis on wis.inventory_status = t.inventory_status left join ws_product_container pc on pc.container_id = t.container_id group by t.product_id, t.secret_key, t.container_id
Note:
1. Do not use subqueries in the from statement;
2. Use more where to limit and narrow the search scope;
3. Make reasonable use of indexes;
4. Check sql performance through explain;
Use the tool SQL Tuning Expert for Oracle optimizes SQL statements
For SQL developers and DBAs, it is easy to write a correct SQL based on business needs. But what about the execution performance of SQL? Can it be optimized to run faster? If you are not a senior
DBA, many people may not have confidence.
Fortunately, automated optimization tools can help us solve this problem. This is the Tosska SQL Tuning Expert for Oracle tool that I will introduce today.
Download https://tosska.com/tosska-sql-tuning-expert-tse-oracle-free-download/
The inventor of this tool, Richard To, Former chief engineer at Dell, with more than 20 years of experience in SQL optimization.
1. Create a database connection, which can also be created later. Fill in the connection information and click the “Connect” button.
If you have installed the Oracle client and configured TNS on the Oracle client, you can select "TNS" as the "Connection Mode" in this window, and then select the configured TNS as the "Database Alias" Database alias.
If you have not installed the Oracle client or do not want to install the Oracle client, you can select "Basic Type" as "Connection Mode" and only need the database server IP, port and service Just name.
2. Enter the SQL with performance problems
3. Click the Tune button to automatically generate a large number of equivalents SQL and start execution. Although the testing is not complete yet, we can already see that the performance of SQL 20 has improved by 100%.
Let’s take a closer look at SQL 20, which uses two Hints and stands out for the fastest execution speed. The original SQL takes 0.99 seconds, and the optimized SQL execution time is close to 0 seconds.
Since this SQL is executed tens of thousands of times in the database every day, it can save about 165 seconds of database execution time after optimization.
Finally, replace the problematic SQL in the application source code with the equivalent SQL 20. Recompiled the application and performance improved.
The tuning task was successfully completed!
Related articles:
Sql performance optimization summary and sql statement optimization
SQL statement optimization principles, sql statement optimization
Related videos:
MySQL optimization video tutorial—Boolean education
The above is the detailed content of Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.
