相同表优化的子查询

问题描述 投票:0回答:1

我正在制作一个统计报告项目,以显示搜索关键字的最高计数,该表有大约50万条记录。

表格(简化):

+----------------------+--------------+
| Field                | Type         |
+----------------------+--------------+
| acct                 | varchar(5)   |
| branch               | varchar(2)   |
| page_name            | varchar(20)  |
| access_time          | datetime     |
| query_input          | varchar(500) |
+----------------------+--------------+

page_name可以是3个值:'search' 'detail''cart'

我需要的是由page_name分组的每种类型的query_input,并在一个查询中按降序计数行数。起初我只是让hibernate获取所有记录然后在java中处理它们,但查询花费的时间太长,即使我正在使用无状态会话。

为了减少从hibernate返回的数据的大小,我尝试了这个

(SELECT page_name, query_input, count(*) FROM table_name WHERE acct='XXXXX' AND page_name='search 'GROUP BY query_input ORDER BY COUNT(*) DESC LIMIT 100)
UNION ALL
(SELECT ... AND page_name='detail' ...)
UNION ALL
(SELECT ... AND page_name='cart' ...)

但这会导致数据库循环表3次,有没有办法重新调整查询,所以它只循环一次表,但得到我想要的相同结果?

例如,没有限制:

+----------------------+--------------+---------+
| page_name            | query_input  | count(*)|
+----------------------+--------------+---------+
| search               | CCC          | 10      |
| search               | EEE          | 8       |
| search               | AAA          | 1       |
| search               | BBB          | 1       |
| detail               | DDD          | 12      |
| detail               | FFF          | 11      |
| detail               | HHH          | 1       |
| detail               | GGG          | 1       |
| cart                 | III          | 6       |
| cart                 | JJJ          | 4       |
| cart                 | LLL          | 1       |
| cart                 | KKK          | 1       |
+----------------------+--------------+---------+

限制2:

+----------------------+--------------+---------+
| page_name            | query_input  | count(*)|
+----------------------+--------------+---------+
| search               | CCC          | 10      |
| search               | EEE          | 8       |
| detail               | DDD          | 12      |
| detail               | FFF          | 11      |
| cart                 | III          | 6       |
| cart                 | JJJ          | 4       |
+----------------------+--------------+---------+

更新

我感觉这是无法解决的,因为我意识到我实际上是在尝试“通过它的排序从表中选择”,而对于MySQL,在选择时没有考虑订购......我是否正确

mysql performance hibernate greatest-n-per-group
1个回答
0
投票

试试in运算符:

SELECT page_name, query_input, count(*) 
FROM table_name 
WHERE acct='XXXXX' 
  AND page_name IN ('search', 'detail', 'cart') 
GROUP BY query_input 
ORDER BY COUNT(*) DESC 
LIMIT 100
© www.soinside.com 2019 - 2024. All rights reserved.