我有一个包含两个表的数据库:
形容词 - ID主键;名称varchar; adjectives_reviews - ID主键; ADJECTIVE_ID外键; REVIEW_ID外键; COUNT int
我想从adjectives_reviews获取一个不同的ADJECTIVE_ID列表,其中包含' COUNT'中的值的总和。所有行的列具有相同的ADJECTIVE_ID。
形容词表有46,000行,而adjectives_reviews有~2,849,708。
我正在使用以下HTSQL查询:
/形容词{名称,总和(adjectives_reviews.count)}
转换为SQL:
SELECT `adjectives`.`name`,
COALESCE(`adjectives_reviews`.`sum`, 0)
FROM `adjectives`
LEFT OUTER JOIN (SELECT CAST(SUM(`adjectives_reviews`.`count`) AS SIGNED INTEGER) AS `sum`,
`adjectives_reviews`.`adjective_id`
FROM `adjectives_reviews`
GROUP BY 2) AS `adjectives_reviews`
ON (`adjectives`.`id` = `adjectives_reviews`.`adjective_id`)
ORDER BY `adjectives`.`id` ASC
此查询生成我想要的结果,但速度太慢。它实际上很慢,HTTP服务器超时,我无法获得我需要的输出CSV文件。我正在寻找一种优化此查询的方法,或者是一种在不使用HTSQL的HTTP接口的情况下将输出作为CSV的方法。
答案 0 :(得分:0)
我能够通过使用本机mysql客户端并在查询中添加3行来将结果显示为CSV:
SELECT `adjectives`.`name`,
COALESCE(`adjectives_reviews`.`sum`, 0)
FROM `adjectives`
LEFT OUTER JOIN (SELECT CAST(SUM(`adjectives_reviews`.`count`) AS SIGNED INTEGER) AS `sum`,
`adjectives_reviews`.`adjective_id`
FROM `adjectives_reviews`
GROUP BY 2) AS `adjectives_reviews`
ON (`adjectives`.`id` = `adjectives_reviews`.`adjective_id`)
ORDER BY `adjectives`.`id` ASC
INTO OUTFILE '/tmp/adjectives.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
跑得相当快。