0

我创建了以下查询来制作一个“频率图”,显示特定值之间的行数:

SELECT CONCAT('0 - ', M.MaxField1) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1<M.MaxField1
UNION ALL
SELECT CONCAT((M.MaxField1), ' - ', (M.MaxField1*2)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1) AND P.Field1<(M.MaxField1*2)
UNION ALL
SELECT CONCAT((M.MaxField1*2), ' - ', (M.MaxField1*3)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*2) AND P.Field1<(M.MaxField1*3)
UNION ALL
SELECT CONCAT((M.MaxField1*3), ' - ', (M.MaxField1*4)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*3) AND P.Field1<(M.MaxField1*4)
UNION ALL
SELECT CONCAT((M.MaxField1*4), ' - ', (M.MaxField1*5)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*4) AND P.Field1<(M.MaxField1*5)
UNION ALL
SELECT CONCAT((M.MaxField1*5), ' - ', (M.MaxField1*6)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*5) AND P.Field1<(M.MaxField1*6)
UNION ALL
SELECT CONCAT((M.MaxField1*6), ' - ', (M.MaxField1*7)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*6) AND P.Field1<(M.MaxField1*7)
UNION ALL
SELECT CONCAT((M.MaxField1*7), ' - ', (M.MaxField1*8)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*7) AND P.Field1<(M.MaxField1*8)
UNION ALL
SELECT CONCAT((M.MaxField1*8), ' - ', (M.MaxField1*9)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*8) AND P.Field1<(M.MaxField1*9)
UNION ALL
SELECT CONCAT((M.MaxField1*9), ' - ', (M.MaxField1*10)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*9)

有什么建议可以优化这个脚本,因为它非常耗时?

4

2 回答 2

1

试试这个(未经测试):

WITH cte(MaxField1) AS (
    SELECT (MAX(Field1)/10) AS MaxField1 FROM TABLE1
)
SELECT 
    'Part',
    COUNT(*)
FROM (
    SELECT 
        CONCAT((MaxField1*M.bucket), ' - ', (MaxField1*(M.bucket+1))) AS 'Part',
        Field1 / cte.MaxField1 AS bucket
    FROM TABLE1, cte
) AS M
GROUP BY 'Part'

对于没有 CTE 表达式的 MySQL 用户,请尝试以下操作:

SELECT 
    'Part',
    COUNT(*)
FROM (
    SELECT 
        CONCAT((MaxField1*M.bucket), ' - ', (MaxField1*(M.bucket+1))) AS 'Part',
        Field1 / cte.MaxField1 AS bucket
    FROM TABLE1, 
   (SELECT (MAX(Field1)/10) AS MaxField1 FROM TABLE1)cte
) AS M
GROUP BY 'Part'

或荷兰语:

SELECT 
    'Part', 
    COUNT(*) 
FROM ( 
    SELECT 
        CONCAT((MaxPercbeheer*M.bucket), ' - ', (MaxPercbeheer*(M.bucket+1))) AS 'Part', 
        (Percbeheer/cte.MaxPercbeheer) AS bucket, 
        MaxPercbeheer 
    FROM POLIS, (SELECT (MAX(Percbeheer)/10) AS MaxPercbeheer FROM POLIS) cte
) AS M 
GROUP BY 'Part'
于 2013-03-03T14:25:22.960 回答
0

您可以将其转换为一个查询,因为联合部分本质上是相同的查询:

create temporary table range_tmp (start int, end int);
insert into range_tmp values (0,1),(1,2),(2,3),(3,4),(4,5),(5,6),(7,8),(9,10);

SELECT CONCAT((M.MaxField1*t.start), ' - ', (M.MaxField1*t.end)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M, range_tmp t
WHERE (P.Field1>=(M.MaxField1*t.start) or t.start=1) AND (P.Field1<(M.MaxField1*t.end) or t.end=10)
于 2013-03-03T16:03:44.667 回答