gpt4 book ai didi

php - 在 PHP 中组合多个 MySQL 表的更好方法?

转载 作者:行者123 更新时间:2023-11-29 06:37:49 24 4
gpt4 key购买 nike

我正在开发一个系统,为所有访问我的网站的用户生成一个 ID。如果用户创建帐户、申请信贷、购买产品...等等,它会将用户信息与数据库上的多个表中的此 ID 链接起来。我的问题是,合并所有信息需要很长时间,因为截至目前我们拥有超过 60,000,000 条记录。我之前创建的 VIEW 完全浪费了性能时间(最多需要 3 分钟),因此我决定创建一个查询来获取数据并将非空字段与 PHP 结合起来,总共得到了 10-15 个结果秒也使用分页。如果可能的话,我希望它能够进一步改进。有什么建议吗?

    SELECT DISTINCT
u.user_id,
u.operating_system_name AS os,
u.browser_name AS browser,
u.ip_address,
u.hardware_type AS hardware,
u.city AS city,
u.state AS state,
u.country AS country,
u.date AS date,

m.id_member AS member,
s.id_supplier AS supplier,
c.id_entry AS credit,
p.id_partner AS partner,
cu.employee_ID AS careersus,
cm.empleado_ID AS careersmx,
o.id_order AS orderid,

o.recipient_name AS fn_o,
m.first_name AS fn_m,
c.first_name AS fn_c,
p.first_name AS fn_p,
s.contact_first_name AS fn_s,
cu.firstname AS fn_cu,
cm.nombre AS fn_cm,

o.last_name AS ln_o,
m.last_name AS ln_m,
c.last_name AS ln_c,
p.last_name AS ln_p,
s.contact_last_name AS ln_s,
cu.lastname AS ln_cu,
cm.apellido AS ln_cm

FROM tb_users_ids AS u
LEFT JOIN tb_orders AS o ON u.user_id = o.user_id
LEFT JOIN tb_members AS m ON u.user_id = m.user_id
LEFT JOIN financing AS c ON c.user_id = u.user_id
LEFT JOIN tb_partners AS p ON p.user_id = u.user_id
LEFT JOIN suppliers AS s ON s.user_id = u.user_id
LEFT JOIN tb_careers_us AS cu ON cu.user_id = u.user_id
LEFT JOIN tb_careers_mx AS cm ON cm.user_id = u.user_id
WHERE u.user_id<>''
ORDER BY u.date DESC
LIMIT ".$offset.", ".$rowsperpage;

最佳答案

您必须为要比较的所有字段创建索引。因此,如果您的查询类似于 table1.user_id = table2.user_id ,请在 table1 和 table2 中为 user_id 定义索引。在所有 table 上都这样做。

然后它会非常非常快。

关于php - 在 PHP 中组合多个 MySQL 表的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53014265/

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