我有一个表有近90列,并且还使用了Range Partition。该表有近50个记录。当我在Query中使用GROUP BY
时,它在explain语句中显示“using temporary”和“filesort”。
查询:
SELECT
subscribe_time * 0.000001 AS start_date,
subscribe_time * 0.000001 AS end_date,
(
IFNULL(
ROUND(
SUM(
CASE WHEN(
subscribe_duration > 20000 AND subscribe_status = '1'
) THEN 1 ELSE 0
END
) /(
SUM(
CASE WHEN(subscribe_status = '1') THEN 1 ELSE 0
END
)
) * 100,
2
),
0
)
) AS subscribe_avg
FROM
tbl_subscription a
WHERE
1 AND subscribe_time > 0 AND subscribe_time BETWEEN "1525113000000000" AND "1524680999999999"
GROUP BY
EXTRACT(
YEAR
FROM
FROM_UNIXTIME(subscribe_time * 0.000001)
),
EXTRACT(
MONTH
FROM
FROM_UNIXTIME(subscribe_time * 0.000001)
),
EXTRACT(
WEEK
FROM
FROM_UNIXTIME(subscribe_time * 0.000001)
),
EXTRACT(
DAY
FROM
FROM_UNIXTIME(subscribe_time * 0.000001)
),
sub_user,
subscribe_ip,
subscribe_zone,
subscribe_approval
已经在field1上给出了索引。 field1不是我们的PK。总共有5列索引。
当我写GROUP BY
时,查询花费了大约43秒来执行。当我在那时删除GROUP BY
时,执行时间为0.27秒。
同样,当我们最后写“ORDER BY NULL
”时,会删除filesort。但我不想在查询中使用ORDER BY
。
如何减少查询执行时间?
另外,我也尝试从单个分区中获取数据,但仍然需要查询。查询从单个分区获取数据:
SELECT field1, field2 FROM TABLE_NAME PARTITION(p1) WHERE 1 AND
field1='SOME_VALUE' GROUP BY field1;
提供一些减少查询执行时间的建议。
答案 0 :(得分:0)
GROUP BY
的第一个表达式可能会被
subscribe_time
或者
FLOOR(subscribe_time * 0.000001)
我认为没有必要将它分开来做GROUP BY
。
您能提供SHOW CREATE TABLE
吗? - 我们需要查看索引,分区以及其他可能的细节。特别是,它是否具有
PARTITION BY RANGE(subscribe_time)
INDEX(subscribe_time)