Home Database Mysql Tutorial Summary of sql database statement optimization analysis and optimization techniques (sql optimization tool)

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

Aug 03, 2018 pm 05:37 PM

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');
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles