如何改进此 mysql 数据透视查询?
How can I improve this mysql pivot query?
我正在使用 Drupal 站点,我需要提取网络表单提交的报告。
Table结构是这样的:
webform_submissions
- sid
- nid
- serial
- uid
- is_draft
- submitted
- remote_addr
webform_submitted_data
- nid
- sid
- cid
- no
- data
所以当提交时,每个 table 都会有这样的内容:
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
还有另一个 table 将 cid 映射到表单字段名称,但我并不担心。
对于 webform_submissions table 中与 nid 匹配的每一行,我需要包含 webform_submitted_data.
中所有相应数据的行
result_set
nid: 100, cid_1: John, 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 table 中与提交相关的所有行放入我的结果集中的单行列中。
使用联接更新了完整查询(returns 空集)。
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
确保你在 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
您正在执行内部联接,这要求所有联接的两边都存在记录。如果这些字段中的任何一个不存在,则整个记录将被忽略。您可能需要 left join
来代替。
至于效率,数据库的基本经验法则:"decision context" 中使用的任何字段,例如where
或 join
,应该有一个索引。
我正在使用 Drupal 站点,我需要提取网络表单提交的报告。
Table结构是这样的:
webform_submissions
- sid
- nid
- serial
- uid
- is_draft
- submitted
- remote_addr
webform_submitted_data
- nid
- sid
- cid
- no
- data
所以当提交时,每个 table 都会有这样的内容: 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
还有另一个 table 将 cid 映射到表单字段名称,但我并不担心。
对于 webform_submissions table 中与 nid 匹配的每一行,我需要包含 webform_submitted_data.
中所有相应数据的行result_set nid: 100, cid_1: John, 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 table 中与提交相关的所有行放入我的结果集中的单行列中。
使用联接更新了完整查询(returns 空集)。
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
确保你在 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
您正在执行内部联接,这要求所有联接的两边都存在记录。如果这些字段中的任何一个不存在,则整个记录将被忽略。您可能需要 left join
来代替。
至于效率,数据库的基本经验法则:"decision context" 中使用的任何字段,例如where
或 join
,应该有一个索引。