我有非常大的客戶數據庫。在添加ORDER BY
之前,此查詢是可以的。我怎樣才能優化我的查詢速度?爲什麼這個INNER JOIN/ORDER BY mysql查詢太慢?
$sql = "SELECT * FROM customers LEFT JOIN ids ON customer_ids.customer_id = customers.customer_id AND ids.type = '10' ORDER BY customers.name LIMIT 10";
ids.type
和customers.name
是我的指標
解釋查詢
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE customers ALL NULL NULL NULL NULL 955 Using temporary; Using filesort 1 SIMPLE ids ALL type NULL NULL NULL 3551 Using where; Using join buffer (Block Nested Loop)
如果您可以將其他字段添加好的索引,請這樣做。這是有道理的,因爲按順序排列,它需要整個結果集才能重新排列。另外,你的主鍵是什麼? – 2015-02-08 03:21:20
您的查詢語法錯誤。 'customer_ids'沒有被定義。 – 2015-02-08 03:23:42
他指的是customer_ids被用在他的連接標準中,但事實上並不是加入的表中的一個。你能在上面的查詢中顯示EXPLAIN的結果嗎? – BK435 2015-02-08 03:51:28