优化具有多个连接的SQL查询以提高性能

huangapple go评论57阅读模式
英文:

Optimizing SQL query with multiple joins for faster performance

问题

com_cards_charge:
com_cards_charge表(数据库)
com_debt:
com_debt表(数据库)

一些结果:
结果1
结果2

SELECT
*
FROM
com_cards_charge,
com_debt
WHERE
com_debt.com_debt_cards_charge_id = com_cards_charge.com_cards_charge_id
AND debt_Creditor_type IN (0, 15)
AND com_cards_charge_deleted = '0'
GROUP BY
com_cards_charge.com_cards_charge_id
ORDER BY
com_cards_charge_id DESC

此查询加载数据超过6秒。
总行数:21121。

感谢您的帮助。

英文:

com_cards_charge :
com_cards_charge table(DB)
com_debt :
com_debt table(DB)

some of result:
Result1
Result2

SELECT 
      * 
   FROM 
      com_cards_charge, 
      com_debt 
   where 
         com_debt.com_debt_cards_charge_id = com_cards_charge.com_cards_charge_id 
     AND debt_Creditor_type in (0, 15) 
     AND com_cards_charge_deleted = '0'  
   GROUP BY 
      com_cards_charge.com_cards_charge_id  
   order by 
      com_cards_charge_id DESC

This query takes more than 6s to load data.
The total rows: 21121.

Thanks for helping me

答案1

得分: 0

首先,开始编写您的查询,使用JOIN来显示A = B的上下文关系。其次,在FROM子句中的表名旁边添加别名,这样您就可以在查询的其余部分中使用缩写版本。如果在同一查询中多次使用相同的表以实现不同目的,这会有所帮助。

接下来,在您的表上添加以下索引:

table               index on
com_cards_charge   ( com_cards_charge_deleted, com_cards_charge_id )
com_debt           ( com_debt_cards_charge_id, debt_Creditor_type )

然后,使用JOIN和别名更新查询:

SELECT 
      * 
   FROM 
      com_cards_charge cc
         JOIN com_debt cd
            on cc.com_cards_charge_id = cd.com_debt_cards_charge_id
           AND cd.debt_Creditor_type in (0, 15) 
   where 
      cc.com_cards_charge_deleted = '0'  
   GROUP BY 
      cc.com_cards_charge_id  
   order by 
      cc.com_cards_charge_id DESC

此外,请不要将图像粘贴到您的帖子中,而是编辑并手动键入数据的示例(这对于显示您要获取的内容的上下文非常重要)。同样适用于显示表结构。

英文:

First, start to write your queries using JOIN showing context relation on how A = B. Second, add aliases next to your table names in the FROM clause so you can use the shortened version within the rest of the query. Helps if you are using the same table multiple times in the same query for alternate purposes too.

Next, add the following indexes on your tables

table               index on
com_cards_charge   ( com_cards_charge_deleted, com_cards_charge_id )
com_debt           ( com_debt_cards_charge_id, debt_Creditor_type )

And updated the query with joins and alias use

SELECT 
      * 
   FROM 
      com_cards_charge cc
	     JOIN com_debt cd
            on cc.com_cards_charge_id = cd.com_debt_cards_charge_id
           AND cd.debt_Creditor_type in (0, 15) 
   where 
      cc.com_cards_charge_deleted = '0'  
   GROUP BY 
      cc.com_cards_charge_id  
   order by 
      cc.com_cards_charge_id DESC

Also, dont paste images to your posts, EDIT and manually type samples of the data (that is important to show context of what you are trying to get). Same with displaying table structures.

答案2

得分: 0

你可以通过指定需要检索的列来提高速度,而不是使用 SELECT * FROM ...

此外,在SQL查询中使用JOIN关键字通常有助于提高查询优化。

例如:

SELECT com_cards_charge.col1, com_debt.col2 FROM com_cards_charge JOIN com_debt ON com_debt.col3 = com_cards_charge.col3 WHERE ...

这两个步骤可以帮助您,谢谢。

英文:

You can improve speed by specifying columns you need to fetch instead of SELECT * FROM ...

Also, using the JOIN keyword in SQL queries can often help improve query optimization

SELECT com_cards_charge.col1, com_debt.col2 FROM com_cards_charge JOIN com_debt ON com_debt.col3 = com_cards_charge.col3 WHERE ...

These two steps can help you, thank you

huangapple
  • 本文由 发表于 2023年5月29日 19:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定