我試圖想出如何訂購連接查詢以提高其性能。
假設我們有兩個表要連接,必須對其應用一些過濾器。
是不是也一樣:
table1_result = select * from table1 where field1 = 'A';
table2_result = select * from table2 where field1 = 'A';
result = select * from table1 as one inner join table2 as two on one.field1 = two.field1;
這樣做:
result = select * from table1 as one inner join table2 as two on one.field1 = two.field1
where one.field1 = 'A' and two.field1 = 'A';
甚至這樣做:
result = select * from table1 as one inner join table2 as two on one.field1 = two.field1 and one.field1 = 'A';
太感謝了!!
uj5u.com熱心網友回復:
一些用于改進查詢的常見優化技術如下:
Index連接中使用的列(如果有)foreign keys,通常像資料庫一樣MySql已經索引它們。Index的columns在使用conditions或WHERE條款。- 避免
*并明確選擇您真正需要的列。 - 大多數情況下加入的順序無關緊要,因為
DB-Engines足夠智能來決定。
所以最好分析兩個連接表的結構,有索引。
如果有人進一步感興趣,更改conditions順序如何有助于獲得更好的性能。我在這里有一個詳細的答案mysql Slow query issue。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/389801.html
下一篇:python讀取鍵盤鍵性能不佳
