它是一个缓慢的查询吗?可以改进吗?

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.

  1. 我应该担心还是 Oracle 优化会以某种方式解决它?
  2. 是否可以在没有相关子查询的情况下对其重新编程?

推荐答案

如果你想在没有关联子查询的情况下重写查询,这里有一种方法:

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 ;

这篇关于它是一个缓慢的查询吗?可以改进吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)