MySql重写子查询以加入
我对此查询有问题:MySql重写子查询以加入,mysql,sql,query-optimization,Mysql,Sql,Query Optimization,我对此查询有问题: SELECT Group_concat(DISTINCT titlegredcd) targetGroup, tblevent.id, generalsubcategorynm, generalcategorynm, coursename, (SELECT Count(*) FROM tblinternalstudent, tb
SELECT Group_concat(DISTINCT titlegredcd)
targetGroup,
tblevent.id,
generalsubcategorynm,
generalcategorynm,
coursename,
(SELECT Count(*)
FROM tblinternalstudent,
tblstaff
WHERE eventid = tblevent.id
AND tblinternalstudent.staffnoic = tblstaff.staffnoic) Total,
Date_format(startdate, '%d %b')
startdateDisplay,
Date_format(enddate, '%d %b')
enddateDisplay,
class,
organizer,
subcategorymainorder,
subcategorysuborder,
tblrefgeneralsubcategory.generalcategorycd,
tblrefgeneralsubcategory.generalsubcategorycd
FROM tblevent,
tblcourse,
tblrefgeneralsubcategory,
tblrefgeneralcategory,
tblinternalstudent a,
tblstaff b
WHERE tblrefgeneralsubcategory.generalcategorycd IN
( 'G0001', 'G0002', 'G0003' )
AND tblcourse.generalcategorycd =
tblrefgeneralsubcategory.generalcategorycd
AND tblcourse.generalsubcategorycd =
tblrefgeneralsubcategory.generalsubcategorycd
AND tblevent.courseid = tblcourse.courseid
AND tblrefgeneralsubcategory.generalcategorycd =
tblrefgeneralcategory.generalcategorycd
AND a.eventid = tblevent.id
AND a.staffnoic = b.staffnoic
AND startdate >= '2012-01-01'
AND enddate <= '2012-12-31'
GROUP BY tblevent.id,
generalsubcategorynm,
generalcategorynm,
coursename,
total,
startdatedisplay,
enddatedisplay,
class,
organizer,
subcategorymainorder,
subcategorysuborder,
tblrefgeneralsubcategory.generalcategorycd,
tblrefgeneralsubcategory.generalsubcategorycd
ORDER BY tblrefgeneralsubcategory.generalsubcategorycd,
coursename
原始查询的解释:
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: a
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 38760
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: b
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 44
ref: edu_db.a.StaffNoIC
rows: 1
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: tblEvent
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 32
ref: edu_db.a.EventId
rows: 1
Extra: Using where
*************************** 4. row ***************************
id: 1
select_type: PRIMARY
table: tblcourse
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 30
ref: edu_db.tblEvent.courseid
rows: 1
Extra: Using where
*************************** 5. row ***************************
id: 1
select_type: PRIMARY
table: tblRefGeneralSubCategory
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 34
ref: edu_db.tblcourse.GeneralSubCategoryCd,edu_db.tblcourse.GeneralCategoryCd
rows: 1
Extra:
*************************** 6. row ***************************
id: 1
select_type: PRIMARY
table: tblRefGeneralCategory
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 17
ref: edu_db.tblcourse.GeneralCategoryCd
rows: 1
Extra:
*************************** 7. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: tblinternalstudent
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 38760
Extra: Using where
*************************** 8. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: tblStaff
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 44
ref: edu_db.tblinternalstudent.StaffNoIC
rows: 1
Extra: Using where; Using index
第二个查询的解释:
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 8849
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: e
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 32
ref: internalstaff.eventid
rows: 1
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: c
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 30
ref: edu_db.e.courseid
rows: 1
Extra: Using where
*************************** 4. row ***************************
id: 1
select_type: PRIMARY
table: gs
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 34
ref: edu_db.c.GeneralSubCategoryCd,edu_db.c.GeneralCategor
rows: 1
Extra:
*************************** 5. row ***************************
id: 1
select_type: PRIMARY
table: g
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 17
ref: edu_db.gs.GeneralCategoryCd
rows: 1
Extra: Using where
*************************** 6. row ***************************
id: 2
select_type: DERIVED
table: i
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 38760
Extra: Using filesort
*************************** 7. row ***************************
id: 2
select_type: DERIVED
table: s
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 44
ref: edu_db.i.StaffNoIC
rows: 1
******************************************1。划船***************************
身份证号码:1
选择类型:主
表:
类型:全部
可能的_键:NULL
键:空
密钥长度:空
ref:NULL
行数:8849
额外:使用临时设备;使用文件排序
*************************** 2. 划船***************************
身份证号码:1
选择类型:主
表:e
类型:eq_ref
可能的_键:主
关键词:小学
钥匙:32
参考:internalstaff.eventid
行数:1
额外:使用where
*************************** 3. 划船***************************
身份证号码:1
选择类型:主
表:c
类型:eq_ref
可能的_键:主
关键词:小学
钥匙:30
参考:edu_db.e.courseid
行数:1
额外:使用where
*************************** 4. 划船***************************
身份证号码:1
选择类型:主
表:gs
类型:eq_ref
可能的_键:主
关键词:小学
钥匙:34
参考:edu_db.c.一般子类别CD,edu_db.c.一般子类别
行数:1
额外:
*************************** 5. 划船***************************
身份证号码:1
选择类型:主
表:g
类型:eq_ref
可能的_键:主
关键词:小学
钥匙:17
参考:edu_db.gs.GeneralCategoryCd
行数:1
额外:使用where
*************************** 6. 划船***************************
身份证号码:2
选择类型:派生
表:i
类型:全部
可能的_键:NULL
键:空
密钥长度:空
ref:NULL
行数:38760
额外:使用文件排序
*************************** 7. 划船***************************
身份证号码:2
选择类型:派生
表:s
类型:eq_ref
可能的_键:主
关键词:小学
钥匙:44
参考编号:edu_db.i.StaffNoIC
行数:1
尝试此查询,也可能存在一些关系问题,因为您在此处没有明确说明
SELECT
te.id,
Date_format(startdate, '%d %b') AS startdateDisplay,
Date_format(enddate, '%d %b') AS enddateDisplay,
te.class,
te.organizer,
tgs.generalcategorycd,
tgs.generalsubcategorycd,
tgs.generalsubcategorynm,
tgs.subcategorymainorder,
tgs.subcategorysuborder,
trc.generalcategorynm,
count(tic.*) AS `Total`,
GROUP_CONCAT(DISTINCT tic. titlegredcd) AS TargetGroup
FROM tblevent AS te
LEFT JOIN tblinternalstudent AS tis
ON tis.eventid = te.id
LEFT JOIN tblstaff AS ts
ON ts.staffnoic = tis.staffnoic
LEFT JOIN tblcourse AS tc
ON tc.courseid = te.courseid
LEFT JOIN tblrefgeneralsubcategory AS tgs
ON tgs.generalcategorycd = tc.generalcategorycd
LEFT JOIN tblrefgeneralcategory AS trc
ON trc.generalcategorycd = tgs.generalcategorycd
WHERE te.startdate >= '2012-01-01'
AND te.enddate <= '2012-12-31'
AND trc.generalcategorycd IN('G0001', 'G0002', 'G0003')
GROUP BY te.id, tgs.generalsubcategorynm, trc.generalcategorynm, te.startdatedisplay, te.enddatedisplay te.class, te.organizer, tgs.subcategorymainorder, tgs.subcategorysuborder, trc.generalcategorycd, tgs.generalsubcategorycd
ORDER BY tgs.generalsubcategorycd, tc.coursename
选择
特迪,
日期格式(startdate,“%d%b”)作为startdate显示,
日期格式(enddate,“%d%b”)作为enddateDisplay,
特级,
组织者,
tgs.generalcategorycd,
tgs.generalsubcategorycd,
tgs.generalsubcategorynm,
tgs.subcategory主订单,
tgs.subcategoritysuborder,
trc.generalcategorynm,
将(tic.*)计为“总计”,
作为目标集团的集团(不同的议会标题)
从tblevent到te
左连接tblinternalstudent AS tis
在tis.eventid=te.id上
左连接tblstaff作为ts
关于ts.staffnoic=tis.staffnoic
左连接TBL课程作为tc
在tc.courseid=te.courseid上
左连接tblrefgeneralsubcategory作为tgs
在tgs.generalcategorycd=tc.generalcategorycd上
左连接tblrefgeneralcategory作为trc
在trc.generalcategorycd=tgs.generalcategorycd上
其中te.startdate>='2012-01-01'
和te.enddate请为每个语句和相关表的索引发布解释。为什么要使用cartisian查询。为什么不使用连接?并发布您的模式和所需的输出。使用联接可以大大简化此查询,而且速度更快。
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 8849
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: e
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 32
ref: internalstaff.eventid
rows: 1
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: c
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 30
ref: edu_db.e.courseid
rows: 1
Extra: Using where
*************************** 4. row ***************************
id: 1
select_type: PRIMARY
table: gs
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 34
ref: edu_db.c.GeneralSubCategoryCd,edu_db.c.GeneralCategor
rows: 1
Extra:
*************************** 5. row ***************************
id: 1
select_type: PRIMARY
table: g
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 17
ref: edu_db.gs.GeneralCategoryCd
rows: 1
Extra: Using where
*************************** 6. row ***************************
id: 2
select_type: DERIVED
table: i
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 38760
Extra: Using filesort
*************************** 7. row ***************************
id: 2
select_type: DERIVED
table: s
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 44
ref: edu_db.i.StaffNoIC
rows: 1
SELECT
te.id,
Date_format(startdate, '%d %b') AS startdateDisplay,
Date_format(enddate, '%d %b') AS enddateDisplay,
te.class,
te.organizer,
tgs.generalcategorycd,
tgs.generalsubcategorycd,
tgs.generalsubcategorynm,
tgs.subcategorymainorder,
tgs.subcategorysuborder,
trc.generalcategorynm,
count(tic.*) AS `Total`,
GROUP_CONCAT(DISTINCT tic. titlegredcd) AS TargetGroup
FROM tblevent AS te
LEFT JOIN tblinternalstudent AS tis
ON tis.eventid = te.id
LEFT JOIN tblstaff AS ts
ON ts.staffnoic = tis.staffnoic
LEFT JOIN tblcourse AS tc
ON tc.courseid = te.courseid
LEFT JOIN tblrefgeneralsubcategory AS tgs
ON tgs.generalcategorycd = tc.generalcategorycd
LEFT JOIN tblrefgeneralcategory AS trc
ON trc.generalcategorycd = tgs.generalcategorycd
WHERE te.startdate >= '2012-01-01'
AND te.enddate <= '2012-12-31'
AND trc.generalcategorycd IN('G0001', 'G0002', 'G0003')
GROUP BY te.id, tgs.generalsubcategorynm, trc.generalcategorynm, te.startdatedisplay, te.enddatedisplay te.class, te.organizer, tgs.subcategorymainorder, tgs.subcategorysuborder, trc.generalcategorycd, tgs.generalsubcategorycd
ORDER BY tgs.generalsubcategorycd, tc.coursename