search
HomeDatabaseMysql TutorialSummary 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 (&#39;locked&#39;, &#39;release&#39;) 
                               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 = &#39;OK&#39;
                    left join ws_product_container pc on ii.container_id = pc.container_id
//总起来说关联太多表,设计表时可以多一些冗余字段,减少表之间的关联查询;
                where 
                    ii.inventory_type = &#39;raw_material&#39; and 
                    ii.inventory_status = &#39;in_stock&#39; and 
                    ii.facility_id = &#39;25&#39; and 
                    datediff(now(),ii.last_updated_time) < 3  //违反了第一个原则
                     and p.product_type = &#39;goods&#39;
                     and p.product_name like &#39;%果%&#39;   // 违反原则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 = &#39;{$facility_id}&#39; and 
                        ii.inventory_status = &#39;{$inventory_status}&#39; and 
                        ii.product_type = &#39;{$product_type}&#39; and 
                        ii.inventory_type = &#39;{$inventory_type}&#39; and
                        ii.locked_real_quantity > 0 and 
                        ita.type in (&#39;locked&#39;, &#39;release&#39;) 
                    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.

Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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.

Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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.

Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

2. Enter the SQL with performance problems

Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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%.

Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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. Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL differ from SQLite?How does MySQL differ from SQLite?Apr 24, 2025 am 12:12 AM

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

What are indexes in MySQL, and how do they improve performance?What are indexes in MySQL, and how do they improve performance?Apr 24, 2025 am 12:09 AM

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Explain how to use transactions in MySQL to ensure data consistency.Explain how to use transactions in MySQL to ensure data consistency.Apr 24, 2025 am 12:09 AM

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

In what scenarios might you choose PostgreSQL over MySQL?In what scenarios might you choose PostgreSQL over MySQL?Apr 24, 2025 am 12:07 AM

Scenarios where PostgreSQL is chosen instead of MySQL include: 1) complex queries and advanced SQL functions, 2) strict data integrity and ACID compliance, 3) advanced spatial functions are required, and 4) high performance is required when processing large data sets. PostgreSQL performs well in these aspects and is suitable for projects that require complex data processing and high data integrity.

How can you secure a MySQL database?How can you secure a MySQL database?Apr 24, 2025 am 12:04 AM

The security of MySQL database can be achieved through the following measures: 1. User permission management: Strictly control access rights through CREATEUSER and GRANT commands. 2. Encrypted transmission: Configure SSL/TLS to ensure data transmission security. 3. Database backup and recovery: Use mysqldump or mysqlpump to regularly backup data. 4. Advanced security policy: Use a firewall to restrict access and enable audit logging operations. 5. Performance optimization and best practices: Take into account both safety and performance through indexing and query optimization and regular maintenance.

What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment