我想從3個不同的表中獲取數據,如下圖所示:SQL - 加入查詢運行緩慢
SELECT urs.id
,urs.username
,urs.b_avg_update
,urs.b_set_min_avg
,urs.b_set_max_avg
,urs.b_min_avg
,urs.b_max_avg
,trs.TIME
,trs.user
,sum(trs.amount)
,trs.type
,ur.user_by
FROM (
SELECT DISTINCT user_by
FROM xeon_users_rented
) AS ur
LEFT JOIN users AS urs ON ur.user_by = urs.username
LEFT JOIN transactions AS trs ON urs.id = trs.user
AND trs.type = 'Recycle'
AND trs.TIME >= UNIX_TIMESTAMP(CURDATE())
GROUP BY ur.user_by
然而,查詢是相當緩慢運行:
Showing rows 0 - 29 (812 in total, request took 15.0095 sec)
當運行一個EXPLAIN
在上面的查詢,這是我得到:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1516 Using temporary; Using filesort
1 PRIMARY urs ALL NULL NULL NULL NULL 14553 Using where; Using join buffer (flat, BNL join)
1 PRIMARY trs ref user,type user 8 littlebu_maindb.urs.id 6 Using where
2 DERIVED xeon_users_rented range NULL idx_user_by 62 NULL 1516 Using index for group-by
我試圖添加INDEXES
上的超速了一點東西,希望某些列:
桌上xeon_users_rented
:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
xeon_users_rented 0 PRIMARY 1 id A 37897 NULL NULL BTREE
xeon_users_rented 0 user 1 user A 37897 NULL NULL BTREE
xeon_users_rented 1 user_by 1 user_by A 1579 NULL NULL BTREE
xeon_users_rented 1 since 1 since A 4210 NULL NULL BTREE
xeon_users_rented 1 expire 1 expire A 7579 NULL NULL BTREE
xeon_users_rented 1 clicks 1 clicks A 176 NULL NULL BTREE
xeon_users_rented 1 clicks_last 1 clicks_last A 5413 NULL NULL BTREE
xeon_users_rented 1 avg 1 avg A 3445 NULL NULL BTREE
xeon_users_rented 1 click_hour 1 click_hour A 46 NULL NULL BTREE
xeon_users_rented 1 click_minute 1 click_minute A 102 NULL NULL BTREE
xeon_users_rented 1 idx_click_hour 1 click_hour A 46 NULL NULL BTREE
xeon_users_rented 1 idx_click_minute 1 click_minute A 112 NULL NULL BTREE
xeon_users_rented 1 idx_id 1 id A 37897 NULL NULL BTREE
xeon_users_rented 1 idx_user 1 user A 37897 NULL NULL BTREE
xeon_users_rented 1 idx_user_by 1 user_by A 1515 NULL NULL BTREE
xeon_users_rented 1 idx_expire 1 expire A 6316 NULL NULL BTREE
xeon_users_rented 1 idx_since 1 since A 4210 NULL NULL BTREE
xeon_users_rented 1 idx_clicks 1 clicks A 176 NULL NULL BTREE
xeon_users_rented 1 idx_clicks_last 1 clicks_last A 5413 NULL NULL BTREE
xeon_users_rented 1 idx_avg 1 avg A 3445 NULL NULL BTREE
桌上users
:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
users 0 PRIMARY 1 id A 14554 NULL NULL BTREE
users 1 idx_username 1 username A 14554 NULL NULL BTREE
users 1 upline 1 upline A 856 NULL NULL BTREE
users 1 upline 2 upline_expire A 856 NULL NULL BTREE
最後但並非最不重要的,transactions
表:
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type
transactions 0 PRIMARY 1 id A 5293 NULL NULL BTREE
transactions 1 user 1 user A 882 NULL NULL BTREE
transactions 1 type 1 type A 9 NULL NULL BTREE
transactions 1 amount 1 amount A 378 NULL NULL BTREE
我看不到什麼是ca使用緩慢。總共沒有1000條記錄。 如何改進我的原始查詢和/或數據庫設計?
你確定這個運行?因爲你在GROUP BY ur.user_by中只有一個字段,而你的選擇只有很多非聚合字段。 –
@JuanCarlosOropeza是的,它運行 - 真的很慢。 – oliverbj
我現在看到了。 Mysql不驗證一些ANSI組的事情。我的建議是將您的查詢拆分成小部分,並逐一添加,直到找到緩慢的地方。 –