已编辑:所以查询确实有效,但在我的localhost机器上运行需要一分钟。不可否认,我的本地主机上的表格相当大(每个大约8000行),但在生产中,每个表格的行数可能超过25,000行。有没有办法优化这个,所以它不需要那么长时间?如其中一条评论所示,两个表都已编入索引。
我有两个表,jos_eimcart_customers_addresses和jos_eimcart_customers。我想从customers表中提取所有记录,并在地址表中包含可用的地址信息。我有我认为是一个相当普通的左外连接查询,但它在phpMyAdmin中保持超时,即使它没有那么多应该找到的结果。任何人都可以指出我做错了什么吗?我没有收到mySQL错误。
select
c.firstname,
c.lastname,
c.email as customer_email,
a.email as address_email,
c.phone as customer_phone,
a.phone as address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,a.zip,
c.last_signin
from jos_eimcart_customers c
left outer join jos_eimcart_customers_addresses a
on c.id = a.customer_id
order by c.last_signin desc
答案 0 :(得分:4)
您需要group by子句:
SELECT
c.firstname,
c.lastname,
c.email AS customer_email,
a.email AS address_email,
c.phone AS customer_phone,
a.phone AS address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,
a.zip,
c.last_signin
FROM jos_eimcart_customers c
LEFT OUTER JOIN jos_eimcart_customers_addresses a ON c.id = a.customer_id
GROUP BY
c.firstname,
c.lastname,
c.email AS customer_email,
a.email AS address_email,
c.phone AS customer_phone,
a.phone AS address_phone,
a.company,
a.address1,
a.address2,
a.city,
a.state,
a.zip,
c.last_signin
ORDER BY c.last_signin DESC
答案 1 :(得分:0)
您的查询没有任何问题(假设计数与您说的一样)。你的服务器还有其他东西在进行。
是否有机会等待锁?