Is it a slow query? Can it be improved?(它是一个缓慢的查询吗?可以改进吗?)
问题描述
我正在浏览 SQLZOO "SELECT 内的 SELECT 教程",这是执行以下操作的查询之一工作(任务7)
I was going through SQLZOO "SELECT within SELECT tutorial" and here's one of the queries that did the job (task 7)
世界(名称、大陆、地区、人口、gdp)
world(name, continent, area, population, gdp)
SELECT w1.name, w1.continent, w1.population
FROM world w1
WHERE 25000000 >= ALL(SELECT w2.population FROM world w2 WHERE w2.continent=w1.continent)
我的问题是关于此类查询的有效性.子查询将针对主查询的每一行(国家)运行,因此重复地重新填充给定大陆的 ALL 列表.
My questions are about effectiveness of such query. The sub-query will run for each row (country) of the main query and thus repeatedly re-populating the ALL list for a given continent.
- 我应该担心还是 Oracle 优化会以某种方式解决它?
- 是否可以在没有相关子查询的情况下对其重新编程?
推荐答案
如果你想在没有关联子查询的情况下重写查询,这里有一种方法:
If you want to rewrite the query without a correalted subquery, here is one way:
SELECT w1.name, w1.continent, w1.population
FROM world w1
JOIN
( SELECT continent, MAX(population) AS max_population
FROM world
GROUP BY continent
) c
ON c.continent = w1.continent
WHERE 25000000 >= c.max_population ;
我并不是说这会更快.Oracle 的优化器非常好,这是一个简单的整体查询,但是您编写它.这是另一个简化:
I do not imply that this will be faster. Oracle's optimizer is pretty good and this is a simple overall query, however you write it. Here's another simplification:
SELECT w1.name, w1.continent, w1.population
FROM world w1
JOIN
( SELECT continent
FROM world
GROUP BY continent
HAVING MAX(population) <= 25000000
) c
ON c.continent = w1.continent ;
这篇关于它是一个缓慢的查询吗?可以改进吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:它是一个缓慢的查询吗?可以改进吗?
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
