我有以下查询:
select count(inspection_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and inspection_result= 1 and part_type = "39000048" and location = "1" union all
select count(inspection_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and inspection_result= 0 and part_type = "39000048" and location = "1" union all select count(defect_1_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_1_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_2_value_1_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_2_value_1_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_2_value_2_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_2_value_2_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_3_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_3_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_4_value_1_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_4_value_1_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_4_value_2_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_4_value_2_result= 0 and part_type = "39000048" and location = "1" union all
select count(defect_5_result) from copygtron where (inspection_time between "2015-07-15" and "2015-07-21") and defect_5_result= 0 and part_type = "39000048" and location = "1"...
我必须根据用户输入执行此查询3,4,5 ...或10个月或几周。根据110万个数据和传入数据将是每小时72 000个,并且执行此查询需要大约6秒,这是不好的。它必须减少到20毫秒。分区已经完成了12个月,并且inspect_time和缺陷列减少了。
任何人都可以帮助我吗?
答案 0 :(得分:1)
解析你的SQL。找出什么是慢,添加索引和/或优化您的查询。在最坏的情况下,您可能需要制作摘要表或对表进行分片