我正在运行一个看起来像这样的查询
SELECT parent.field, child.field
FROM parent
JOIN child ON (child.id = parent.id
OR child.id = parent.otherid)
Run Code Online (Sandbox Code Playgroud)
然而,这是非常慢的(大约100k记录,并且JOIN到真实版本中的其他表),但尽管尝试了索引
parent.id (PRIMARY),
parent.otherid,
child.id (PRIMARY),
and a composite index of parent.id and parent.otherid
Run Code Online (Sandbox Code Playgroud)
在进行此连接时,我无法让MySQL使用任何这些索引.
我读到MySQL每个连接只能使用一个索引,但是当JOIN包含OR条件时,它是否可以在任何地方找到它是否可以使用复合索引.
有没有人知道是否可以使此查询引用索引?如果是这样,怎么样?
我的解决方案
(所以我不会让我回答下面的问题)
一堆调整,并提出了一个相当不错的解决方案,保留了JOIN和聚合其他表的能力.
SELECT parent.field, child.field
FROM parent
JOIN (
SELECT parent.id as parentid,
# Prevents the need to union
IF(NOT ISNULL(parent.otherid) AND parent.otherid <> parent.id,
parent.otherid,
parent.id) as getdataforid
FROM parent
WHERE (condition)
) as foundrecords
ON foundrecords.parentid = parent.id
JOIN child ON child.id = parent.getdataforid …Run Code Online (Sandbox Code Playgroud) 我在服务器上发布了一个集合,并在客户端上自动订阅.我想在会话中设置'selected'项目并让模板更新以仅显示所选项目,但似乎只能通过往返服务器来完成(这是完全没必要的).
共同:
var Missions = new Meteor.Collection('missions');
Run Code Online (Sandbox Code Playgroud)
客户:
Template.missionList.missions = function() {
var currMission = Session.get('selectedMission');
var searchMission = {};
if(currMission)
{
searchMission['_id'] = currMission;
}
return Missions.find(searchMission);
};
Template.missionList.events({
'click div.mission': function (e, t) {
Session.set('selectedMission',
this._id == Session.get('selectedMission') ? null : this._id
);
}
});
Template.mission.isSelected = function() {
return this._id == Session.get('selectedMission');
};
Meteor.autosubscribe(function () {
Meteor.subscribe("missions");
});
Run Code Online (Sandbox Code Playgroud)
服务器:
Meteor.publish('missions', function() {
// there are really some filters here, but removed for simplicity
return Missions.find();
});
Run Code Online (Sandbox Code Playgroud)
模板: …