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 ;
这篇关于它是一个缓慢的查询吗?可以改进吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:它是一个缓慢的查询吗?可以改进吗?
基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
