sql資料庫語句最佳化分析與最佳化技巧總結(sql優化工具)
通常sql資料庫需要進行最佳化分析,而且還有一定的技巧,sql優化的幾種方法這裡就不做詳細介紹了,本文將會sql語句優化進行總結,後面還附了優化工具SQL Tuning Expert for Oracle及使用方法,首先我們要遵循資料庫最佳化的幾個原則:
#1.盡量避免在列上做運算,這樣會導致索引失敗;
2.使用join是應該用小結果集來驅動大結果集,同時把複雜的join查詢拆分成多個query。不然join的越多表,就會導致越多的鎖定和阻塞。
3.注意like模糊查詢的使用,避免使用%%,例如select * from a where name like '�%';
代替語句:select * from a where name > = 'de' and name
4.僅列出需要查詢的字段,不要使用select * from ...,節省內存;
5.使用批量插入語句,節省交互;
insert into a (id ,name) values(2,'a'), (3,'s');
6.limit基數比較大時,使用between ... and ...
7.不要使用rand函數隨機取得記錄;
8.避免使用null ,這就需要在建表時,盡量設定為not null,提升查詢效能;
9,不要使用count(id),而應該是count(*)
10.不要做無謂的排序,盡可能在索引中完成排序;
我們先來看一個sql:
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
上面的sql我們在from 中使用了子查詢,這樣對查詢是非常不利的;
更好的一種做法是下面的語句:
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
注意:
1、from 語句中一定不要使用子查詢;
2、使用更多的where加以限制,縮小查找範圍;
3.合理利用索引;
4、透過explain查看sql效能;
##使用工具SQL Tuning Expert for Oracle 最佳化SQL語句
DBA,估計很多人都沒有信心。
https://tosska.com/tosska-sql-tuning-expert-tse-oracle-free-download/
本工具發明人Richard To, Dell的前首席工程師, 擁有超過20年的SQL最佳化經驗.相關影片:
#以上是sql資料庫語句最佳化分析與最佳化技巧總結(sql優化工具)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

全表掃描在MySQL中可能比使用索引更快,具體情況包括:1)數據量較小時;2)查詢返回大量數據時;3)索引列不具備高選擇性時;4)複雜查詢時。通過分析查詢計劃、優化索引、避免過度索引和定期維護表,可以在實際應用中做出最優選擇。

是的,可以在 Windows 7 上安裝 MySQL,雖然微軟已停止支持 Windows 7,但 MySQL 仍兼容它。不過,安裝過程中需要注意以下幾點:下載適用於 Windows 的 MySQL 安裝程序。選擇合適的 MySQL 版本(社區版或企業版)。安裝過程中選擇適當的安裝目錄和字符集。設置 root 用戶密碼,並妥善保管。連接數據庫進行測試。注意 Windows 7 上的兼容性問題和安全性問題,建議升級到受支持的操作系統。

InnoDB的全文搜索功能非常强大,能够显著提高数据库查询效率和处理大量文本数据的能力。1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

聚集索引和非聚集索引的區別在於:1.聚集索引將數據行存儲在索引結構中,適合按主鍵查詢和範圍查詢。 2.非聚集索引存儲索引鍵值和數據行的指針,適用於非主鍵列查詢。

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL 和 MariaDB 可以共存,但需要謹慎配置。關鍵在於為每個數據庫分配不同的端口號和數據目錄,並調整內存分配和緩存大小等參數。連接池、應用程序配置和版本差異也需要考慮,需要仔細測試和規劃以避免陷阱。在資源有限的情況下,同時運行兩個數據庫可能會導致性能問題。

MySQL 數據庫中,用戶和數據庫的關係通過權限和表定義。用戶擁有用戶名和密碼,用於訪問數據庫。權限通過 GRANT 命令授予,而表由 CREATE TABLE 命令創建。要建立用戶和數據庫之間的關係,需創建數據庫、創建用戶,然後授予權限。

數據集成簡化:AmazonRDSMySQL與Redshift的零ETL集成高效的數據集成是數據驅動型組織的核心。傳統的ETL(提取、轉換、加載)流程複雜且耗時,尤其是在將數據庫(例如AmazonRDSMySQL)與數據倉庫(例如Redshift)集成時。然而,AWS提供的零ETL集成方案徹底改變了這一現狀,為從RDSMySQL到Redshift的數據遷移提供了簡化、近乎實時的解決方案。本文將深入探討RDSMySQL零ETL與Redshift集成,闡述其工作原理以及為數據工程師和開發者帶來的優勢。
