为什么这个左外连接查询拒绝在 phpMyAdmin 中完成?
编辑:所以查询确实有效,但在我的本地主机上运行需要一分钟多的时间。诚然,我的本地主机上的表要大得多(每个表大约 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
EDITED: so the query does work, but on my localhost machine it took over a minute to run. Admittedly the tables are considerably larger on my localhost (about 8000 rows each), but in production the tables could have upwards of 25,000 rows each. Is there any way to optimize this so it doesn't take as long? As indicated in one of the comments, both tables are indexed.
I have two tables, jos_eimcart_customers_addresses and jos_eimcart_customers. I want to pull all records from the customers table, and include address information where available from the addresses table. I have what I thought was a fairly ordinary left outer join query, but it keeps timing out in phpMyAdmin, even though there aren't that many results it should be finding. Can anyone point out if I'm doing something wrong? I'm not getting a mySQL error.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个 group by 子句:
You need a group by clause:
您的查询没有任何问题(假设计数如您所说)。您的服务器中正在发生其他事情。
它有可能正在等待锁吗?
There is nothing wrong with your query (assuming the counts are as you say they are). Something else is going on in your server.
Any chance it is waiting on a lock?