1

编辑:所以查询确实有效,但在我的本地主机上运行需要一分钟。诚然,我的本地主机上的表要大得多(每个大约 8000 行),但在生产中,每个表可能有超过 25,000 行。有什么办法可以优化这个,所以它不需要那么长时间?如其中一条评论所示,两个表都已编入索引。

我有两个表,jos_eimcart_customers_addresses 和 jos_eimcart_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
4

2 回答 2

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
于 2010-12-28T20:31:16.170 回答
0

您的查询没有任何问题(假设计数如您所说)。您的服务器中正在发生其他事情。

它有没有可能在等待锁定?

于 2010-10-21T19:48:46.877 回答