简体   繁体   English

Mysql查询加入5个表

[英]Mysql query joining 5 tables

I am trying to join 5 tables in which i want to get different currency mentioned on different tables against same contract id.我正在尝试加入 5 个表,其中我想根据相同的合同 ID 在不同的表上提及不同的货币。
It is giving me results when i join any three tables but when I add one more table in query the server gets unresponsive until I have to kill the process.当我加入任何三个表时,它会给我结果,但是当我在查询中再添加一张表时,服务器没有响应,直到我不得不终止进程。

Please help me where I am doing a mistake.请帮助我在我做错的地方。

SELECT c.department_id,
  c.contract_id,
  c.seller_id,
  c.buyer_id,
  c.contract_ratecurrency AS contractcurrency,
  b.currency_id           AS billcurrency,
  s.saleinv_currency      AS saleinvcurrency,
  cm.currency_id          AS commissioncurrency,
  sl.currency_id          AS cmlogcurrency,
  c.contract_iscancel
FROM tbl_contracts C
JOIN tbl_contract_bill b ON c.contract_id=b.contract_id
JOIN tbl_contract_saleinvoice s ON c.contract_id =s.contract_id
JOIN tbl_commission_payment cm ON c.department_id = cm.department_id
JOIN tbl_saleinvoice_commission_log sl ON c.department_id = sl.department_id
WHERE (c.contract_ratecurrency <> s.saleinv_currency
       OR c.contract_ratecurrency     <> b.currency_id
       OR s.saleinv_currency          <> b.currency_id
       OR cm.currency_id              <> sl.currency_id
       OR c.contract_ratecurrency     <> cm.currency_id
       OR s.saleinv_currency          <> cm.currency_id
       OR b.currency_id               <> cm.currency_id)
AND (c.contract_iscancel        =0)

requried result should be

ccontractid,csellerid,cbuyerid,ccurrency,bcurrency,scurrency,cmcurrency,slcurrency
101,25,50,1,1,2,3,1
102,28,16,2,3,1,3,2



It looks like you are having performance issues.看起来您遇到了性能问题。 To optimize your database structure you have multiple options:要优化您的数据库结构,您有多种选择:

  1. Adding indexes on your keys.在您的键上添加索引。

    Let's take a look to your join statement:让我们来看看你的 join 语句:

    JOIN tbl_saleinvoice_commission_log sl ON c.department_id = sl.department_id加入 tbl_saleinvoice_commission_log sl ON c.department_id = sl.department_id

    Adding a clustered index on department_id on tbl_saleinvoice_commission_log table will help you a lot in performance wise.在 tbl_saleinvoice_commission_log 表上的 Department_id 上添加聚集索引将在性能方面帮助您很多。 For more information you can check this link.有关更多信息,您可以查看链接。

  2. Partitioning is another way to increase performance, but you need to check your database structure to see whether it works for you or not.分区是另一种提高性能的方法,但您需要检查您的数据库结构,看看它是否适合您。 For more information you can check this link.有关更多信息,您可以查看链接。

Also I believe your tables are one to many, so you might need to check how many rows you are trying to retrieve.此外,我相信您的表是一对多的,因此您可能需要检查要检索的行数。 If your database server is not capable of processing big number of rows you might need to improve your hardware or CPU usage limits of your database daemon.如果您的数据库服务器无法处理大量行,您可能需要改进数据库守护程序的硬件或 CPU 使用限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM