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!