Mar*_*rko 0 mysql optimization subquery
我需要有关MySQL中子选择性能的建议.由于我无法更改的原因,我无法使用JOIN创建问题过滤器,我只能在WHERE中添加另一个AND子句.
什么是以下表现:
select tasks.*
from tasks
where
some criteria
and task.project_id not in (select id from project where project.is_template = 1);
Run Code Online (Sandbox Code Playgroud)
相比:
select tasks.*
from tasks, project
where
some criteria
and task.project_id = project.id and project.is_template <> 1;
Run Code Online (Sandbox Code Playgroud)
请注意,is_template = 1的项目数量相对较少,并且可能存在大量项目,其中is_template <> 1.
如果我不能改变除过滤器之外的任何东西,还有其他方法可以在没有子选择的情况下实现相同的结果吗?
我认为第二个更有效,因为它只需要一个选择,但可以肯定的是,你应该解析每个查询并检查结果.
EXPLAIN select tasks.*
from tasks
where
some criteria
and task.project_id not in (select id from project where project.is_template = 1);
EXPLAIN select tasks.*
from tasks, project
where
some criteria
and task.project_id = project.id and project.is_template <> 1;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3611 次 |
| 最近记录: |