我对这个查询有疑问:
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
处理大约需要 30 分钟,我怀疑问题来自子选择。
我尝试使用加入重写:
SELECT e.id,
e.courseid,
internalstaff.targetgroup,
internalstaff.total,
Date_format(startdate, '%d %b') AS startdateDisplay,
Date_format(enddate, '%d %b') AS enddateDisplay,
class,
organizer,
coursename,
gs.generalcategorycd,
gs.generalsubcategorycd,
gs.generalsubcategorynm,
gs.subcategorymainorder,
gs.subcategorysuborder,
generalcategorynm
FROM tblcourse c,
tblrefgeneralsubcategory gs,
tblrefgeneralcategory g,
tblevent e
JOIN (SELECT i.eventid,
Count(*) AS Total,
Group_concat(DISTINCT titlegredcd) AS TargetGroup
FROM tblinternalstudent i,
tblstaff s
WHERE i.staffnoic = s.staffnoic
GROUP BY i.eventid) AS internalstaff
ON internalstaff.eventid = e.id
WHERE e.courseid = c.courseid
AND gs.generalcategorycd = g.generalcategorycd
AND gs.generalcategorycd IN ( 'G0001', 'G0002', 'G0003' )
AND c.generalcategorycd = gs.generalcategorycd
AND c.generalsubcategorycd = gs.generalsubcategorycd
AND startdate >= '2012-01-01'
AND enddate <= '2012-12-31'
GROUP BY e.id,
generalsubcategorynm,
generalcategorynm,
startdatedisplay,
enddatedisplay,
class,
organizer,
subcategorymainorder,
subcategorysuborder,
generalcategorycd,
generalsubcategorycd
ORDER BY generalsubcategorycd,
coursename
它加载速度更快,并返回正确数量的记录作为原始查询(1802)。但是,当我在 internalstaff 表中添加 BranchCd(和 branchcd = 'some branch code')时,它将返回更少的记录数(519)。
但是在原始查询中,如果(和 branchcd = 'some branch code')被添加到子选择中,它会返回正确数量的记录。
任何人都可以就如何重写加入提出建议,使其像子选择一样工作?
谢谢你。
编辑:抱歉忘了包括表格:
tblinternalstudent (**staffnoic**, eventid)
tblstaff (**staffnoic** titlegredcd, branchcd, unitcd)
tblevent (**id**, courseid, startdate, enddate, class, organizer)
tblcourse (**courseid**, coursename)
tblrefgeneralcategory (**generalcategorycd**, generalcategorynm)
tblrefgeneralsubcategory (**generalsubcategorycd, generalsubcategorynm, generalcategorycd,subcategorymainorder, subcategorysuborder)
原始查询的解释:
*************************** 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