简体   繁体   English

SQL查询具有许多联接的大量数据

[英]SQL query for large amount of data with many joins

I have written a sql query for my requirement. 我已经为我的要求编写了一个sql查询。
This is working fine for me. 这对我来说很好。 This is taking 0.0006 sec to execute. 这需要0.0006秒才能执行。

I want to know from sql experts "will this work fine with large amount of data?". 我想从sql专家那里知道“这对大量数据是否可以正常工作?”。

I have written my query below. 我在下面写了我的查询。

SELECT HM_customers.id,
       HM_customers.username,
       HM_customers.firstname,
       HM_customers.lastname,
       HM_customers.company,
       HM_customers_address_bank.field_data
FROM   HM_orders
       JOIN HM_order_items
         ON HM_order_items.order_id = HM_orders.id
       JOIN HM_bid
         ON HM_order_items.bid_id = HM_bid.bid_id
       JOIN HM_customers
         ON HM_bid.user_id = HM_customers.id
       JOIN HM_customers_address_bank
         ON HM_customers_address_bank.id = HM_customers.default_billing_address
WHERE  HM_orders.id = '4' 

Any expert can advice me or let me know how can I improve this query. 任何专家都可以为我提供建议或让我知道如何改进此查询。 Please suggest me if any issue in this query. 如果此查询有任何问题,请提出建议。

NOTE:- This is a simple query. 注意:-这是一个简单的查询。 But I want to know, will this work with large amount of data with less time 但我想知道,这将以更少的时间处理大量数据吗?

You don't need to include the orders table: 您不需要包括orders表:

SELECT c.id,
       c.username,
       c.firstname,
       c.lastname,
       c.company,
       cb.field_data
FROM   HM_order_items oi
       JOIN HM_bid b
         ON oi.bid_id = b.bid_id
       JOIN HM_customers c
         ON b.user_id = c.id
       JOIN HM_customers_address_bank cb
         ON cb.id = c.default_billing_address
WHERE  oi.order_id = '4'; 

Your query can also result in duplicate rows, if a customer bids on the same items multiple times. 如果客户多次对同一项目进行出价,则查询还会导致重复的行。 If you put in a select distinct , then you will incur overhead of duplicate elimination. 如果您select distinct一个不重复的,则将产生重复消除的开销。 If this becomes a problem, you will probably want to restructure the query as an exists . 如果这成为问题,则您可能希望将查询重组为exists

There are few points worth noting 有几点值得注意

1) The reference to an outer table column in the WHERE clause prevents the OUTER JOIN from returning any non-matched rows, which implicitly converts the query to an INNER JOIN. 1)引用WHERE子句中的外部表列可防止OUTER JOIN返回任何不匹配的行,从而将查询隐式转换为INNER JOIN。 This is probably a bug in the query or a misunderstanding of how OUTER JOIN works. 这可能是查询中的错误,或者是对OUTER JOIN的工作方式的误解。

2) Selecting all columns with the * wildcard will cause the query's meaning and behavior to change if the table's schema changes, and might cause the query to retrieve too much data. 2)如果表的模式发生更改,则使用*通配符选择所有列将导致查询的含义和行为发生变化,并可能导致查询检索过多的数据。 You should only choose columns you need. 您只应选择所需的列。

Please make your driven table to 'HM_customers' as all your data is coming from this table and change your join like this way, hopefully this will help you :) 由于您的所有数据都来自该表,因此请将您的从动表设置为“ HM_customers”,并以这种方式更改您的联接,希望对您有所帮助:)

SELECT hmCust.id,
       hmCust.username,
       hmCust.firstname,
       hmCust.lastname,
       hmCust.company,
       hmCustAdd.field_data
FROM   HM_customers hmCust
       INNER JOIN HM_bid hmBid
               ON hmBid.user_id = hmCust.id
       INNER JOIN HM_customers_address_bank hmCustAdd
               ON hmCustAdd.id = hmCust.default_billing_address
       INNER JOIN HM_order_items hmOrderItem
               ON hmOrderItem.order_id = hmBid.bid_id
       INNER JOIN HM_orders hmOrder
               ON hmOrder.id = hmOrderItem.order_id
WHERE  hmOrder.id = '4' 

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

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