请问您能帮我解决这个疑问吗?
SELECT p.patid, MAX(c1.eventdate) as eventdate
from patient as p
left join op_adv_effects._clinical as c1 on p.patid = c1.patid
where c1.eventdate < p.case_index
group by p.patid
以下是2个表的SHOW CREATE TABLE的输出:
patient CREATE TABLE `patient` (
`patid` int(10) unsigned NOT NULL,
`case_index` date NOT NULL,
PRIMARY KEY (`patid`,`case_index`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs
_clinical CREATE TABLE `_clinical` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`patid` int(10) unsigned NOT NULL,
`eventdate` date NOT NULL,
`medcode` mediumint(8) unsigned DEFAULT NULL,
`adid` mediumint(8) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_clin_eventdate_medcode` (`patid`,`eventdate`,`medcode`),
KEY `idx_clin_eventdate_adid` (`patid`,`eventdate`,`adid`)
) ENGINE=InnoDB AUTO_INCREMENT=62407536 DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs
&#34;解释&#34;返回以下内容:
*************************** 1. row ********************
id: 1
select_type: SIMPLE
table: p
type: index
possible_keys: PRIMARY
key: PRIMARY
key_len: 7
ref: NULL
rows: 182939
Extra: Using index
*************************** 2. row ********************
id: 1
select_type: SIMPLE
table: c1
type: ref
possible_keys: idx_clin_eventdate_medcode,idx_clin_eventdate_adid
key: idx_clin_eventdate_medcode
key_len: 4
ref: gprd_opadveff_extra_elisa.p.patid
rows: 171
Extra: Using where; Using index
为什么不使用idx_clin_eventdate_medcode的前两个字段,即(patid,eventdate),而只使用patid(参见ref列)?
如果我将where条件更改为相等,则可以正常工作:
SELECT p.patid, MAX(c1.eventdate) as eventdate
from patient as p
left join op_adv_effects._clinical as c1 on p.patid = c1.patid
where c1.eventdate = p.case_index
group by p.patid
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: p
type: index
possible_keys: PRIMARY
key: PRIMARY
key_len: 7
ref: NULL
rows: 182939
Extra: Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: c1
type: ref
possible_keys: idx_clin_eventdate_medcode,idx_clin_eventdate_adid
key: idx_clin_eventdate_medcode
key_len: 7
ref: gprd_opadveff_extra_elisa.p.patid,gprd_opadveff_extra_elisa.p.cas
e_index
rows: 1
Extra: Using index
某些建议变体的结果相同:
explain SELECT patid,
(SELECT eventdate
FROM op_adv_effects._clinical
WHERE patid = p.patid
AND eventdate < p.case_index
ORDER BY eventdate DESC
LIMIT 1 ) AS eventdate
FROM patient AS p;
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: p
type: index
possible_keys: NULL
key: PRIMARY
key_len: 7
ref: NULL
rows: 182939
Extra: Using index
*************************** 2. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: _clinical
type: ref
possible_keys: idx_clin_eventdate_medcode,idx_clin_eventdate_adid
key: idx_clin_eventdate_medcode
key_len: 4
ref: gprd_opadveff_extra_elisa.p.patid
rows: 171
Extra: Using where; Using index; Using filesort
explain SELECT patid,
( SELECT MAX(eventdate)
FROM op_adv_effects._clinical
WHERE patid = p.patid
AND eventdate < p.case_index) AS eventdate
FROM patient AS p;
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: p
type: index
possible_keys: NULL
key: PRIMARY
key_len: 7
ref: NULL
rows: 182939
Extra: Using index
*************************** 2. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: _clinical
type: ref
possible_keys: idx_clin_eventdate_medcode,idx_clin_eventdate_adid
key: idx_clin_eventdate_medcode
key_len: 4
ref: gprd_opadveff_extra_elisa.p.patid
rows: 171
Extra: Using where; Using index
查询是更复杂的查询的一部分,如下所述。但是,这只是几个复杂查询的一个示例,它们应该在eventdate上使用索引的缺失部分。因此,它非常重要。
CREATE TABLE bmi_lp
(PRIMARY KEY (patid))
ENGINE=INNODB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs
SELECT tmp.patid, a2.data3 as bmi_lp, tmp.eventdate as bmi_lp_date
from (
SELECT p.patid, MAX(c.eventdate) as eventdate
from patient as p
left join op_adv_effects._clinical as c1 on p.patid = c1.patid
left join op_adv_effects._additional as a1 on c1.patid = a1.patid
where c1.adid <> 0 and c1.adid = a1.adid
and a1.enttype = 13
and a1.data3 is not null
and c1.eventdate < p.case_index
group by p.patid
order by p.patid) as tmp
left join op_adv_effects._clinical as c2 on tmp.patid = c2.patid
left join op_adv_effects._additional as a2 on c2.patid = a2.patid
where tmp.eventdate = c2.eventdate and c2.adid = a2.adid
答案 0 :(得分:1)
由于WHERE
你正在做INNER JOIN
。你打算这么做吗?
无论如何,由于<
目前无法使用索引,如果您有一个使用不同顺序的索引,它可以正常工作。
例如,在PostgreSQL中你可以这样做:
CREATE INDEX idx_clin_eventdate_medcode ON _clinical (patid ASC, eventdate DESC);
在MySQL中,DESC
和ASC
运算符很不幸(至少每个MySQL版本都达到5.7)。因此,除非您可以反转查询(使用>
而不是<
),否则MySQL无法有效地使用该索引。
请注意,不使用索引甚至可能更快,这取决于具体情况。因为它只经过171行我不会太担心。
答案 1 :(得分:0)
尝试一下:
SELECT patid,
( SELECT MAX(eventdate)
FROM op_adv_effects._clinical
WHERE patid = p.patid
AND eventdate < p.case_index
) AS eventdate
FROM patient AS p;
(需要GROUP BY
。)
这是使用LIMIT 1
代替MAX
的变体:
SELECT patid,
( SELECT eventdate
FROM op_adv_effects._clinical
WHERE patid = p.patid
AND eventdate < p.case_index
ORDER BY eventdate DESC
LIMIT 1
) AS eventdate
FROM patient AS p;
输出中有多少行?