如何改进这个 mysql 数据透视查询?

Posted

技术标签:

【中文标题】如何改进这个 mysql 数据透视查询?【英文标题】:How can I improve this mysql pivot query? 【发布时间】:2016-10-11 18:06:34 【问题描述】:

我正在使用 Drupal 站点,我需要提取网络表单提交的报告。

Table结构是这样的:

webform_submissions
- sid
- nid
- serial
- uid
- is_draft
- submitted
- remote_addr

webform_submitted_data
- nid
- sid
- cid
- no
- data

因此,当提交时,每个表中都会出现这样的内容: webform_submissions

sid: 1, nid:100, serial: 1, uid: 0, is_draft: 0, submitted: [timestamp], remote_addr: 0.0.0.0

webform_submitted_data

nid: 100, sid: 1, cid: 1, no: 0, data: John
nid: 100, sid: 1, cid: 2, no: 0, data: Doe

还有另一个表将 cid 映射到表单字段名称,但我并不担心。

对于 webform_submissions 表中与 nid 匹配的每一行,我需要一行包含 webform_submitted_data 中的所有相应数据。

结果集 nid:100,cid_1:约翰,cid_2:Doe

我需要的实际表单数据大约有 30 个字段。

这是我能想到的最好的查询:

SELECT ws.nid, wd_c2.data, wd_c3.data
FROM    webform_submissions ws, 
        webform_submitted_data wd_c2,
        webform_submitted_data wd_c3
WHERE   ws.nid = 168
AND     ws.sid = wd_c3.sid
AND     wd_c3.cid = 3
AND     ws.sid = wd_c2.sid
AND     wd_c2.cid = 2;

该查询在我的本地 mysql 上需要将近 20 秒才能提取 8 条记录。我只能假设我的方法不是一个好方法。有什么更好的方法可以将 webform_submitted_data 表中与提交相关的所有行放入我的结果集中的单行列中。

FULL QUERY 使用 Joins 更新(返回空集)。

SELECT ws.nid, 
wd_c2.data  as 'First Name', 
wd_c3.data  as 'Last Name', 
wd_c4.data  as 'Email Address', 
wd_c5.data  as 'Primary Phone', 
wd_c6.data  as 'Secondary Phone', 
wd_c7.data  as 'Address', 
wd_c8.data  as 'City', 
wd_c9.data  as 'State', 
wd_c10.data as 'Zip Code', 
wd_c11.data as 'Cf', 
wd_c12.data as 'AR', 
wd_c13.data as 'G', 
wd_c14.data as 'PE', 
wd_c15.data as 'PD', 
wd_c16.data as 'AN', 
wd_c17.data as 'V', 
wd_c18.data as 'S', 
wd_c19.data as 'V2', 
wd_c20.data as 'S', 
wd_c21.data as 'Si', 
wd_c22.data as 'D', 
wd_c23.data as 'M', 
wd_c24.data as 'L', 
wd_c25.data as 'V', 
wd_c26.data as 'C', 
wd_c27.data as 'O', 
wd_c28.data as 'M', 
wd_c29.data as 'P', 
wd_c30.data as 'C', 
wd_c31.data as 'C', 
wd_c32.data as 'C', 
wd_c33.data as 'E', 
wd_c34.data as 'Ey', 
wd_c41.data as 'TD', 
wd_c42.data as 'RN', 
wd_c43.data as 'TP', 
wd_c45.data as 'LM', 
wd_c47.data as 'PIp', 
wd_c48.data as 'wd', 
wd_c49.data as 'Cr', 
wd_c50.data as 'N', 
wd_c51.data as 'A'

FROM webform_submissions ws 
INNER JOIN  webform_submitted_data wd_c2    on ws.sid = wd_c2.sid
INNER JOIN  webform_submitted_data wd_c3    on ws.sid = wd_c3.sid
INNER JOIN  webform_submitted_data wd_c4    on ws.sid = wd_c4.sid
INNER JOIN  webform_submitted_data wd_c5    on ws.sid = wd_c5.sid
INNER JOIN  webform_submitted_data wd_c6    on ws.sid = wd_c6.sid
INNER JOIN  webform_submitted_data wd_c7    on ws.sid = wd_c7.sid
INNER JOIN  webform_submitted_data wd_c8    on ws.sid = wd_c8.sid
INNER JOIN  webform_submitted_data wd_c9    on ws.sid = wd_c9.sid
INNER JOIN  webform_submitted_data wd_c10   on ws.sid = wd_c10.sid
INNER JOIN  webform_submitted_data wd_c11   on ws.sid = wd_c11.sid
INNER JOIN  webform_submitted_data wd_c12   on ws.sid = wd_c12.sid
INNER JOIN  webform_submitted_data wd_c13   on ws.sid = wd_c13.sid
INNER JOIN  webform_submitted_data wd_c14   on ws.sid = wd_c14.sid
INNER JOIN  webform_submitted_data wd_c15   on ws.sid = wd_c15.sid
INNER JOIN  webform_submitted_data wd_c16   on ws.sid = wd_c16.sid
INNER JOIN  webform_submitted_data wd_c17   on ws.sid = wd_c17.sid
INNER JOIN  webform_submitted_data wd_c18   on ws.sid = wd_c18.sid
INNER JOIN  webform_submitted_data wd_c19   on ws.sid = wd_c19.sid
INNER JOIN  webform_submitted_data wd_c20   on ws.sid = wd_c20.sid
INNER JOIN  webform_submitted_data wd_c21   on ws.sid = wd_c21.sid
INNER JOIN  webform_submitted_data wd_c22   on ws.sid = wd_c22.sid
INNER JOIN  webform_submitted_data wd_c23   on ws.sid = wd_c23.sid
INNER JOIN  webform_submitted_data wd_c24   on ws.sid = wd_c24.sid
INNER JOIN  webform_submitted_data wd_c25   on ws.sid = wd_c25.sid
INNER JOIN  webform_submitted_data wd_c26   on ws.sid = wd_c26.sid
INNER JOIN  webform_submitted_data wd_c27   on ws.sid = wd_c27.sid
INNER JOIN  webform_submitted_data wd_c28   on ws.sid = wd_c28.sid
INNER JOIN  webform_submitted_data wd_c29   on ws.sid = wd_c29.sid
INNER JOIN  webform_submitted_data wd_c30   on ws.sid = wd_c30.sid
INNER JOIN  webform_submitted_data wd_c31   on ws.sid = wd_c31.sid
INNER JOIN  webform_submitted_data wd_c32   on ws.sid = wd_c32.sid
INNER JOIN  webform_submitted_data wd_c33   on ws.sid = wd_c33.sid
INNER JOIN  webform_submitted_data wd_c34   on ws.sid = wd_c34.sid
INNER JOIN  webform_submitted_data wd_c41   on ws.sid = wd_c41.sid
INNER JOIN  webform_submitted_data wd_c42   on ws.sid = wd_c42.sid
INNER JOIN  webform_submitted_data wd_c43   on ws.sid = wd_c43.sid
INNER JOIN  webform_submitted_data wd_c45   on ws.sid = wd_c45.sid
INNER JOIN  webform_submitted_data wd_c47   on ws.sid = wd_c47.sid
INNER JOIN  webform_submitted_data wd_c48   on ws.sid = wd_c48.sid
INNER JOIN  webform_submitted_data wd_c49   on ws.sid = wd_c49.sid
INNER JOIN  webform_submitted_data wd_c50   on ws.sid = wd_c50.sid
INNER JOIN  webform_submitted_data wd_c51   on ws.sid = wd_c51.sid
INNER JOIN  webform_submitted_data wd_c52   on ws.sid = wd_c52.sid

WHERE   ws.nid = 16818

AND     wd_c2.cid = 2
AND     wd_c3.cid = 3
AND     wd_c4.cid = 4
AND     wd_c5.cid = 5
AND     wd_c6.cid = 6
AND     wd_c7.cid = 7
AND     wd_c8.cid = 8
AND     wd_c9.cid = 9
AND     wd_c10.cid =    10
AND     wd_c11.cid =    11
AND     wd_c12.cid =    12
AND     wd_c13.cid =    13
AND     wd_c14.cid =    14
AND     wd_c15.cid =    15
AND     wd_c16.cid =    16
AND     wd_c17.cid =    17
AND     wd_c18.cid =    18
AND     wd_c19.cid =    19
AND     wd_c20.cid =    20
AND     wd_c21.cid =    21
AND     wd_c22.cid =    22
AND     wd_c23.cid =    23
AND     wd_c24.cid =    24
AND     wd_c25.cid =    25
AND     wd_c26.cid =    26
AND     wd_c27.cid =    27
AND     wd_c28.cid =    28
AND     wd_c29.cid =    29
AND     wd_c30.cid =    30
AND     wd_c31.cid =    31
AND     wd_c32.cid =    32
AND     wd_c33.cid =    33
AND     wd_c34.cid =    34
AND     wd_c41.cid =    41
AND     wd_c42.cid =    42
AND     wd_c43.cid =    43
AND     wd_c45.cid =    45
AND     wd_c47.cid =    47
AND     wd_c48.cid =    48
AND     wd_c49.cid =    49
AND     wd_c50.cid =    50
AND     wd_c51.cid =    51
AND     wd_c52.cid =    52
LIMIT 20

【问题讨论】:

【参考方案1】:

确保您在 webform_submissions(nid, sid)、webform_submitted_data(sid, cid) 上有正确的索引(并且为了更好地准备使用连接符号而不是 where/and 子句)

    SELECT ws.nid, wd_c2.data, wd_c3.data
    FROM        webform_submissions ws   
    INNER JOIN  webform_submitted_data wd_c2  on ws.sid = wd_c2.sid
    INNER JOIN  webform_submitted_data wd_c3  on ws.sid = wd_c3.sid
    WHERE   ws.nid = 168
    AND     wd_c3.cid = 3
    AND     wd_c2.cid = 2;

例如对于索引

 CREATE INDEX ws_id on  webform_submissions(nid, sid)

尝试使用左连接(所以如果一个值与查询不匹配)

SELECT ws.nid, 
wd_c2.data  as 'First Name', 
wd_c3.data  as 'Last Name', 
wd_c4.data  as 'Email Address', 
wd_c5.data  as 'Primary Phone', 
wd_c6.data  as 'Secondary Phone', 
wd_c7.data  as 'Address', 
wd_c8.data  as 'City', 
wd_c9.data  as 'State', 
wd_c10.data as 'Zip Code', 
wd_c11.data as 'Cf', 
wd_c12.data as 'AR', 
wd_c13.data as 'G', 
wd_c14.data as 'PE', 
wd_c15.data as 'PD', 
wd_c16.data as 'AN', 
wd_c17.data as 'V', 
wd_c18.data as 'S', 
wd_c19.data as 'V2', 
wd_c20.data as 'S', 
wd_c21.data as 'Si', 
wd_c22.data as 'D', 
wd_c23.data as 'M', 
wd_c24.data as 'L', 
wd_c25.data as 'V', 
wd_c26.data as 'C', 
wd_c27.data as 'O', 
wd_c28.data as 'M', 
wd_c29.data as 'P', 
wd_c30.data as 'C', 
wd_c31.data as 'C', 
wd_c32.data as 'C', 
wd_c33.data as 'E', 
wd_c34.data as 'Ey', 
wd_c41.data as 'TD', 
wd_c42.data as 'RN', 
wd_c43.data as 'TP', 
wd_c45.data as 'LM', 
wd_c47.data as 'PIp', 
wd_c48.data as 'wd', 
wd_c49.data as 'Cr', 
wd_c50.data as 'N', 
wd_c51.data as 'A'

FROM webform_submissions ws 
LEFT JOIN  webform_submitted_data wd_c2    on ws.sid = wd_c2.sid
LEFT JOIN  webform_submitted_data wd_c3    on ws.sid = wd_c3.sid
LEFT JOIN  webform_submitted_data wd_c4    on ws.sid = wd_c4.sid
LEFT JOIN  webform_submitted_data wd_c5    on ws.sid = wd_c5.sid
LEFT JOIN  webform_submitted_data wd_c6    on ws.sid = wd_c6.sid
LEFT JOIN  webform_submitted_data wd_c7    on ws.sid = wd_c7.sid
LEFT JOIN  webform_submitted_data wd_c8    on ws.sid = wd_c8.sid
LEFT JOIN  webform_submitted_data wd_c9    on ws.sid = wd_c9.sid
LEFT JOIN  webform_submitted_data wd_c10   on ws.sid = wd_c10.sid
LEFT JOIN  webform_submitted_data wd_c11   on ws.sid = wd_c11.sid
LEFT JOIN  webform_submitted_data wd_c12   on ws.sid = wd_c12.sid
LEFT JOIN  webform_submitted_data wd_c13   on ws.sid = wd_c13.sid
LEFT JOIN  webform_submitted_data wd_c14   on ws.sid = wd_c14.sid
LEFT JOIN  webform_submitted_data wd_c15   on ws.sid = wd_c15.sid
LEFT JOIN  webform_submitted_data wd_c16   on ws.sid = wd_c16.sid
LEFT JOIN  webform_submitted_data wd_c17   on ws.sid = wd_c17.sid
LEFT JOIN  webform_submitted_data wd_c18   on ws.sid = wd_c18.sid
LEFT JOIN  webform_submitted_data wd_c19   on ws.sid = wd_c19.sid
LEFT JOIN  webform_submitted_data wd_c20   on ws.sid = wd_c20.sid
LEFT JOIN  webform_submitted_data wd_c21   on ws.sid = wd_c21.sid
LEFT JOIN  webform_submitted_data wd_c22   on ws.sid = wd_c22.sid
LEFT JOIN  webform_submitted_data wd_c23   on ws.sid = wd_c23.sid
LEFT JOIN  webform_submitted_data wd_c24   on ws.sid = wd_c24.sid
LEFT JOIN  webform_submitted_data wd_c25   on ws.sid = wd_c25.sid
LEFT JOIN  webform_submitted_data wd_c26   on ws.sid = wd_c26.sid
LEFT JOIN  webform_submitted_data wd_c27   on ws.sid = wd_c27.sid
LEFT JOIN  webform_submitted_data wd_c28   on ws.sid = wd_c28.sid
LEFT JOIN  webform_submitted_data wd_c29   on ws.sid = wd_c29.sid
LEFT JOIN  webform_submitted_data wd_c30   on ws.sid = wd_c30.sid
LEFT JOIN  webform_submitted_data wd_c31   on ws.sid = wd_c31.sid
LEFT JOIN  webform_submitted_data wd_c32   on ws.sid = wd_c32.sid
LEFT JOIN  webform_submitted_data wd_c33   on ws.sid = wd_c33.sid
LEFT JOIN  webform_submitted_data wd_c34   on ws.sid = wd_c34.sid
LEFT JOIN  webform_submitted_data wd_c41   on ws.sid = wd_c41.sid
LEFT JOIN  webform_submitted_data wd_c42   on ws.sid = wd_c42.sid
LEFT JOIN  webform_submitted_data wd_c43   on ws.sid = wd_c43.sid
LEFT JOIN  webform_submitted_data wd_c45   on ws.sid = wd_c45.sid
LEFT JOIN  webform_submitted_data wd_c47   on ws.sid = wd_c47.sid
LEFT JOIN  webform_submitted_data wd_c48   on ws.sid = wd_c48.sid
LEFT JOIN  webform_submitted_data wd_c49   on ws.sid = wd_c49.sid
LEFT JOIN  webform_submitted_data wd_c50   on ws.sid = wd_c50.sid
LEFT JOIN  webform_submitted_data wd_c51   on ws.sid = wd_c51.sid
LEFT JOIN  webform_submitted_data wd_c52   on ws.sid = wd_c52.sid

WHERE   ws.nid = 16818

AND     wd_c2.cid = 2
AND     wd_c3.cid = 3
AND     wd_c4.cid = 4
AND     wd_c5.cid = 5
AND     wd_c6.cid = 6
AND     wd_c7.cid = 7
AND     wd_c8.cid = 8
AND     wd_c9.cid = 9
AND     wd_c10.cid =    10
AND     wd_c11.cid =    11
AND     wd_c12.cid =    12
AND     wd_c13.cid =    13
AND     wd_c14.cid =    14
AND     wd_c15.cid =    15
AND     wd_c16.cid =    16
AND     wd_c17.cid =    17
AND     wd_c18.cid =    18
AND     wd_c19.cid =    19
AND     wd_c20.cid =    20
AND     wd_c21.cid =    21
AND     wd_c22.cid =    22
AND     wd_c23.cid =    23
AND     wd_c24.cid =    24
AND     wd_c25.cid =    25
AND     wd_c26.cid =    26
AND     wd_c27.cid =    27
AND     wd_c28.cid =    28
AND     wd_c29.cid =    29
AND     wd_c30.cid =    30
AND     wd_c31.cid =    31
AND     wd_c32.cid =    32
AND     wd_c33.cid =    33
AND     wd_c34.cid =    34
AND     wd_c41.cid =    41
AND     wd_c42.cid =    42
AND     wd_c43.cid =    43
AND     wd_c45.cid =    45
AND     wd_c47.cid =    47
AND     wd_c48.cid =    48
AND     wd_c49.cid =    49
AND     wd_c50.cid =    50
AND     wd_c51.cid =    51
AND     wd_c52.cid =    52
LIMIT 20

【讨论】:

所以这个查询适用于 2 个数据列,但是当我添加所有 30 个数据列时,它返回一个空集。 您确定您的所有 wd_c*.cid 都与相关值匹配吗? .. 尝试使用左连接 我认为这是正确的方法我还有一些数据问题需要解决,因为一些 sid/cid 组合返回多行。但是非常感谢您的示例和帮助。 @JonDiamond 谢谢 .. 如果您认为这是正确的答案 .. 请将其标记为已接受。 让我们continue this discussion in chat.【参考方案2】:

您正在执行内部联接,这要求记录出现在所有联接的两侧。如果这些字段中的任何一个不存在,则忽略整个记录。你可能想要一个left join

至于效率,数据库的基本经验法则:“决策上下文”中使用的任何字段,例如wherejoin,应该有一个索引。

【讨论】:

以上是关于如何改进这个 mysql 数据透视查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 mysql 中取消透视数据

使用 access 和 mysql 时,有没有办法查看哪些查询用于生成数据透视表? [复制]

如何在 MySQL 中返回数据透视表输出?

具有动态列的 MySQL 数据透视表查询

具有动态列的 MySQL 数据透视表查询

Mysql执行数据透视表查询