doz97171 2018-10-26 18:16
浏览 39

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

I am developing a system which generates an ID for all users that visit my website. If the user creates an account, applies for credit, buys a products... etc, it links the user information with this ID across multiple tables on the database. My problem is, combining all the information takes a long time as we have over 60,000 thousand records as of this moment. I previously created a VIEW which completely killed the performance time (taking up to 3 minutes) so I decided to instead create a query to fetch the data and combine the non-empty fields with PHP, which came up with a total of 10-15 seconds using pagination as well. I would like it to improve it more if possible. Any suggestions?

    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;
  • 写回答

2条回答 默认 最新

  • douqian1835 2018-10-26 18:37
    关注

    You have to create indexes to all fields which you compare. So if your query is like table1.user_id = table2.user_id define index for user_id in your table1 and table2. Do it at all tables.

    Then it will very be very fast.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错