为什么这个左外连接查询拒绝在 phpMyAdmin 中完成?

发布于 2024-09-28 23:28:34 字数 752 浏览 7 评论 0原文

编辑:所以查询确实有效,但在我的本地主机上运行需要一分钟多的时间。诚然,我的本地主机上的表要大得多(每个表大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-10-05 23:28:34

您需要一个 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

You need a group by clause:

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
つ可否回来 2024-10-05 23:28:34

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

它有可能正在等待锁吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文