我有非常大的客户数据库。在添加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