在php中组合多个mysql表的更好方法?

ig9co6j1  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(375)

我正在开发一个系统,为所有访问我的网站的用户生成一个id。如果用户创建帐户,申请信贷,购买产品。。。等等,它在数据库的多个表中用这个id链接用户信息。我的问题是,综合所有的信息需要很长时间,因为到目前为止我们有超过6000000条记录。我之前创建了一个视图,它完全消耗了性能时间(最多需要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;
xqk2d5yq

xqk2d5yq1#

必须为所有要比较的字段创建索引。如果你的问题是 table1.user_id = table2.user_id 定义索引 user_id 在你的表1和表2中。在所有的table上都做。
那就很快了。

avkwfej4

avkwfej42#

请将用户id作为所有表的索引。这将使您的查询更快

相关问题