gpt4 book ai didi

php - 慢查询 - 多个连接和大量数据

转载 作者:行者123 更新时间:2023-11-29 04:14:15 24 4
gpt4 key购买 nike

我确信有更好的方法可以做到这一点。我有三个包含大量数据的主表:records_main、sales 和 appointments。每个都有近 20,000 条记录。

我需要连接这三个表以及其他一些不是两个大的表。

    $SQL = "SELECT DISTINCT appointments.id AS aid, appointments.date, appointments.estimate_price, appointments.next_action, appointments.next_action_date, appointments.result, appointments.result_type, appointments.notes,
customer.id AS cid, customer.homeowner1_fname, customer.homeowner1_lname, customer.address, customer.city, customer.state, customer.zipcode, customer.phone1, customer.phone1_type, customer.phone2, customer.phone2_type, customer.phone3, customer.phone3_type, customer.phone4, customer.phone4_type, customer.phone5, customer.phone5_type, customer.lead_source, customer.lead_category, customer.primary_interest, customer.secondary_interest, customer.additional_interest1, customer.additional_interest2,
originator.employee_id AS originator_employee_id,originator.fname AS originator_fname,originator.lname AS originator_lname,
setter.employee_id AS setter_employee_id,setter.fname AS setter_fname,setter.lname AS setter_lname,
resetter.employee_id AS resetter_employee_id, resetter.fname AS resetter_fname, resetter.lname AS resetter_lname,
salesrep.employee_id AS salesrep_employee_id, salesrep.fname AS salesrep_fname, salesrep.lname AS salesrep_lname,
salesrep2.employee_id AS salesrep2_employee_id, salesrep2.fname AS salesrep2_fname, salesrep2.lname AS salesrep2_lname
FROM
core_records_appointments as appointments
INNER JOIN core_records_main as customer ON appointments.customer = customer.id
LEFT JOIN core_employees_main as originator ON appointments.originator = originator.id
LEFT JOIN core_employees_main as setter ON appointments.setter = setter.id
LEFT JOIN core_employees_main as resetter ON appointments.resetter = resetter.id
LEFT JOIN core_employees_main as salesrep ON appointments.sales_representative = salesrep.id
LEFT JOIN core_employees_main as salesrep2 ON appointments.sales_representative2 = salesrep2.id
";

这需要几秒钟,但最终会加载。我提出的最后一个连接似乎将查询打断了:

LEFT JOIN core_records_sales as sales ON appointments.day = sales.day_sold AND appointments.customer = sales.customer

在此之后我设置了一个限制和一个分组依据

我可以做些什么来改进它?我将它与 jqgrid 一起使用,不确定是否有帮助

最佳答案

作为第一种方法,尝试使用 EXPLAIN 语法对数据库执行查询。这将为您提供对该语句的分析,并告诉您它是否使用了索引。如果它不使用索引或没有足够的索引,请尝试将它们添加到表中。

关于php - 慢查询 - 多个连接和大量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1834933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com