如下SQL语句在MySQL中执行需要2秒左右的时间,如果使用UNION ALL进行不去重合并只需要0.4秒,UNION ALL配合DISTINCT来去重速度又变成2秒了,请问如何进行优化?
SELECT
a. KEY,
a. DATA,
a.date_added
FROM
(
(
SELECT
CONCAT('customer_', ca. KEY) AS `key`,
ca. DATA,
ca.date_added
FROM
`cf_customer_activity` ca
)
UNION
(
SELECT
CONCAT('affiliate_', aa. KEY) AS `key`,
aa. DATA,
aa.date_added
FROM
`cf_affiliate_activity` aa
)
) a
ORDER BY
a.date_added DESC
LIMIT 0,
5;
怪我咯2017-04-17 14:50:54
當然先使用QEP
mysql> explain select * from wp_options limit 1\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: wp_options
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 136
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.01 sec)
mysql> show session status like 'Handler_read%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Handler_read_first | 3 |
| Handler_read_key | 3 |
| Handler_read_last | 0 |
| Handler_read_next | 0 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 209 |
+-----------------------+-------+
7 rows in set (0.01 sec)
至於每個欄位的具體意義,自己查資料。
PHP中文网2017-04-17 14:50:54
@prolifes 是對的。也許題主沒表達清楚。
我補充另外一個思路,因為你最後只需要5條數據,從ca中取5條,再從aa中取5條,總共10條排序,這個會很快。
SELECT
a. KEY,
a. DATA,
a.date_added
FROM
(
(
SELECT
CONCAT('customer_', ca. KEY) AS `key`,
ca. DATA,
ca.date_added
FROM
`cf_customer_activity` ca
ORDER BY
a.date_added DESC /* 如果date_added有索引的话,这个会比较快 */
LIMIT 0,5
)
UNION ALL
(
SELECT
CONCAT('affiliate_', aa. KEY) AS `key`,
aa. DATA,
aa.date_added
FROM
`cf_affiliate_activity` aa
ORDER BY
a.date_added DESC /* 如果date_added有索引的话,这个会比较快 */
LIMIT 0,5
)
) a
ORDER BY
a.date_added DESC
LIMIT 0, 5;