2010-08-05 51 views
1

我有mysql的MySQL的3加入

一個問題,我有3個表:

Deposit 
+-------------------+-------------+------+-----+ 
| Field    | Type  | Null | Key | 
+-------------------+-------------+------+-----+ 
| id    | bigint(20) | NO | PRI | 
| status   | int(2)  | NO |  | 
| depositDate  | datetime | NO | MUL | 
| reversePayment_id | bigint(20) | YES | UNI | 
| claim_id   | int(2)  | NO | UNI | 
| payment_id  | bigint(20) | YES | UNI | 
+-------------------+-------------+------+-----+ 

付款

+--------------------------+---------------+------+-----+ 
| Field     | Type   | Null | Key | 
+--------------------------+---------------+------+-----+ 
| id      | int(10)  | NO | PRI | 
| paymentDate    | timestamp  | NO | MUL | 
| pin      | int(10)  | NO | MUL | 
| balanceChange   | decimal(15,2) | YES |  | 

索賠

+------------------------+--------------+------+-----+ 
| Field     | Type   | Null | Key | 
+------------------------+--------------+------+-----+ 
| id      | int(11)  | NO | PRI | 
| fullName    | varchar(100) | NO |  | 
| depositSum    | blob   | NO |  | 
| ip      | varchar(39) | NO |  | 
| status     | int(2)  | NO |  | 
+------------------------+--------------+------+-----+ 

我儘量選擇存款(與索賠)付款或reversePayment兩個日期之間,我執行此查詢與3聯接:

EXPLAIN SELECT this_.id AS id60_3_, ..., fcpayment2_.id AS id59_0_, ..., reversepay3_.id AS id59_1_, ..., cl1_.id AS id61_2_, ... 
FROM Deposit this_ 
INNER JOIN Payment fcpayment2_ ON this_.payment_id = fcpayment2_.id 
LEFT OUTER JOIN Payment reversepay3_ ON this_.reversePayment_id = reversepay3_.id 
INNER JOIN Claim cl1_ ON this_.claim_id = cl1_.id 
WHERE (
(
fcpayment2_.paymentDate >= '2010-08-04 21:00:00' 
AND fcpayment2_.paymentDate <= '2010-08-05 08:01:00' 
) 
OR (
reversepay3_.paymentDate >= '2010-08-04 21:00:00' 
AND reversepay3_.paymentDate <= '2010-08-05 08:01:00' 
) 
) 
ORDER BY this_.depositDate DESC 

結果是

+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+ 
| id | select_type | table  | type | possible_keys              | key  | key_len | ref          | rows | Extra       | 
+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+ 
| 1 | SIMPLE  | cl1_   | ALL | PRIMARY               | NULL  | NULL | NULL         | 426588 | Using temporary; Using filesort | 
| 1 | SIMPLE  | this_  | eq_ref | claim_id,payment_id,FKDB5A0548511B6CDD,FKDB5A054867BA4108   | claim_id | 4  | portal.cl1_.id       |  1 |         | 
| 1 | SIMPLE  | fcpayment2_ | eq_ref | PRIMARY,paymentDate,date           | PRIMARY | 4  | portal.this_.payment_id     |  1 | Using where      | 
| 1 | SIMPLE  | reversepay3_ | eq_ref | PRIMARY               | PRIMARY | 4  | portal.this_.reversePayment_id   |  1 | Using where      | 
+----+-------------+--------------+--------+--------------------------------------------------------------------+----------+---------+-----------------------------------------+--------+---------------------------------+ 

爲什麼結果第一表是CL1_爲什麼MySQL不使用鑰匙?

+0

只是引起了我的注意:你的外鍵不一致。在押金中,您的claim_id字段爲int(2),在您的id字段爲int(11)。付款ID也一樣。您應該嘗試防止這種情況,並確保它們屬於同一類型... – wimvds 2010-08-05 08:00:19

回答

0

因爲您使用了關鍵字'Explain',並且因爲cl1_是您在查詢中爲該表提供的別名。

我不明白你關於密鑰的問題。

+0

我認爲mysql應該是 1.從此選擇記錄_ 2.然後使用cl1_.id(PK)從cl1_中選擇記錄 isn'它呢? – Anev 2010-08-05 07:56:02