我有2张桌子:
requests
:内容6000万条记录(用作网站日志)
requests_hours
:包含几百行(保持从requests
表开始每分钟更新一次)
我有以下简单查询,但是执行该查询大约需要5分钟,因为Postgres不使用列request_time_utc
的索引,而是执行顺序扫描。
SELECT COUNT(request_id)
FROM requests
WHERE request_time_utc >= (SELECT MAX(request_hour_utc) FROM requests_hours)
但是,如果我只删除子查询(它本身在0.003s内执行)并用一个静态值替换,如下所示,我得到的查询仅在0.008s内执行:
SELECT COUNT(request_id)
FROM requests
WHERE request_time_utc >= '2019-09-30 17:00:00'
查询应该每分钟只记录几行,从1000到7000,因此,对列request_time_utc
的索引扫描肯定比顺序扫描要好得多。
我不明白如何强制PostgreSQL对第一个查询进行索引扫描。
以上查询是为了简化问题;这是原始的:
SELECT
customer_id,
DATE_TRUNC('hour', request_time_utc) AS request_hour_utc,
COUNT(request_id) AS total_requests,
SUM(data_in_size) AS total_data_in_size,
SUM(data_out_size) AS total_data_out_size,
SUM(process_long) AS total_process_long
FROM requests
WHERE request_time_utc >= (SELECT MAX(request_hour_utc) FROM requests_hours)
AND customer_id IS NOT NULL
GROUP BY request_hour_utc , customer_id
ORDER BY request_hour_utc DESC;
答案 0 :(得分:1)
将您的子查询移至CTE,就像这样(我是在火车上用手机编写的,因此您需要登陆正确的查询:-)):
<template>
<input type="file" accept="image/*" @change="onChange" />
<div id="preview">
<img v-if="item.imageUrl" :src="item.imageUrl" />
</div>
</template>
<script>
export default {
name: 'imageUpload',
data() {
return {
item:{
//...
image : null
imageUrl: null
}
}
},
methods: {
onChange(e) {
const file = e.target.files[0]
this.image = file
this.item.imageUrl = URL.createObjectURL(file)
}
}
}
</script>