无法弄清楚3-table INNER JOIN(MySQL)的高效SQL - “客户还买”功能

时间:2011-04-04 06:11:37

标签: mysql

我正在尝试将典型的“购买'x'并购买'y''功能的客户添加到我的网站上。这是表结构:

Table: qb_invoice
+--------------------------------+------------------+------+-----+-------------------+----------------+
| Field                          | Type             | Null | Key | Default           | Extra          |
+--------------------------------+------------------+------+-----+-------------------+----------------+
| qbsql_id                       | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| TxnID                          | varchar(40)      | YES  | MUL | NULL              |                |
| Customer_ListID                | varchar(40)      | YES  | MUL | NULL              |                |
| Customer_FullName              | varchar(255)     | YES  |     | NULL              |                |
+--------------------------------+------------------+------+-----+-------------------+----------------+

Table: qb_invoice_invoiceline
+-------------------------+------------------+------+-----+-------------------+----------------+
| Field                   | Type             | Null | Key | Default           | Extra          |
+-------------------------+------------------+------+-----+-------------------+----------------+
| qbsql_id                | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| Invoice_TxnID           | varchar(40)      | YES  | MUL | NULL              |                |
| Item_ListID             | varchar(40)      | YES  | MUL | NULL              |                |
| Item_FullName           | varchar(255)     | YES  |     | NULL              |                |
+-------------------------+------------------+------+-----+-------------------+----------------+

Table: qb_customer
+-------------------------------------+------------------+------+-----+-------------------+----------------+
| Field                               | Type             | Null | Key | Default           | Extra          |
+-------------------------------------+------------------+------+-----+-------------------+----------------+
| qbsql_id                            | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| ListID                              | varchar(40)      | YES  | MUL | NULL              |                |
| Name                                | varchar(41)      | YES  | MUL | NULL              |                |
+-------------------------------------+------------------+------+-----+-------------------+----------------+

鉴于 Item_ListID ,我想要一个快速,高效的查询来返回 Item_ListID 的列表以及订购每件商品的客户数量的COUNT在列表中,所有客户都有最初提供的 Item_ListID

现在我有以下SQL可行,但速度很慢:

SELECT qb_invoice_invoiceline.Item_FullName, count(*) as 'nummy'
FROM qb_invoice_invoiceline
WHERE qb_invoice_invoiceline.Invoice_TxnID = 
ANY (SELECT qb_invoice.TxnID
FROM qb_invoice 
INNER JOIN qb_customer ON qb_invoice.Customer_ListID = qb_customer.ListID
INNER JOIN qb_invoice_invoiceline ON qb_invoice.TxnID = qb_invoice_invoiceline.Invoice_TxnID
WHERE qb_invoice_invoiceline.Item_ListID = '1360000-57')
GROUP BY qb_invoice_invoiceline.Item_ListID
ORDER BY nummy DESC

感谢您的帮助!

这是'解释'输出:

+----+--------------------+------------------------+-------+---------------------------+-------------+---------+-----------------------------------------+-------+----------------------------------------------+
| id | select_type        | table                  | type  | possible_keys             | key         | key_len | ref                                     | rows  | Extra                                        |
+----+--------------------+------------------------+-------+---------------------------+-------------+---------+-----------------------------------------+-------+----------------------------------------------+
|  1 | PRIMARY            | qb_invoice_invoiceline | index | NULL                      | Item_ListID | 123     | NULL                                    | 19690 | Using where; Using temporary; Using filesort |
|  2 | DEPENDENT SUBQUERY | qb_invoice_invoiceline | ref   | Invoice_TxnID,Item_ListID | Item_ListID | 123     | const                                   |     8 | Using where                                  |
|  2 | DEPENDENT SUBQUERY | qb_invoice             | ref   | Customer_ListID,TxnID     | TxnID       | 123     | func                                    |   206 | Using where                                  |
|  2 | DEPENDENT SUBQUERY | qb_customer            | ref   | ListID                    | ListID      | 123     | devdb.qb_invoice.Customer_ListID |    18 | Using where; Using index                     |
+----+--------------------+------------------------+-------+---------------------------+-------------+---------+-----------------------------------------+-------+----------------------------------------------+

1 个答案:

答案 0 :(得分:0)

如果您要加入的varchar字段上没有可用的索引,则查询可能会很慢。您能详细介绍这些表中的索引吗?

我认为该查询将受益于qb_invoice.TxnID和qb_customer.ListID以及qb_invoice_invoiceline.Item_ListID上的索引。