- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下查询,我想对其进行优化。
SELECT
a.household_id household_id,
age_of_youngest_woman,
b.number_of_children,
c.number_of_men,
fertility_cond_prob_number_of_children.cond_prob cond_prob_number_of_children,
fertility_cond_age.cond_prob cond_prob_age,
fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob total_cond_prob,
random() <= (874. / 1703.) is_newborn_male
FROM
(
SELECT household_id, MIN(age) age_of_youngest_woman
FROM person
WHERE
(user_id = 1) and
(gender = 'FEMALE') and
(age >= 18)
GROUP BY household_id
) a
LEFT JOIN
(
SELECT household_id, COUNT(*) number_of_children
FROM person
WHERE
(user_id = 1) and
(gender = 'CHILD')
GROUP BY household_id
) b ON (a.household_id = b.household_id)
LEFT JOIN
(
SELECT household_id, COUNT(*) number_of_men
FROM person
WHERE
(user_id = 1) and
(gender = 'MALE') and
(age >= 18)
GROUP BY household_id
) c ON (a.household_id = c.household_id)
LEFT JOIN fertility_cond_prob_number_of_children ON (fertility_cond_prob_number_of_children.number_of_children = b.number_of_children)
LEFT JOIN fertility_cond_age ON (fertility_cond_age.age = age_of_youngest_woman)
WHERE
(c.number_of_men > 0) and
(random() <= (fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob))
EXPLAIN ANALYZE
返回以下信息:
Merge Join (cost=20366.67..853430.69 rows=34797455 width=44) (actual time=1330.609..1641.402 rows=224 loops=1)
Merge Cond: (c.household_id = public.person.household_id)
-> Sort (cost=4806.12..4829.66 rows=9416 width=16) (actual time=492.839..546.397 rows=25098 loops=1)
Sort Key: c.household_id
Sort Method: external merge Disk: 640kB
-> Subquery Scan on c (cost=3972.76..4184.62 rows=9416 width=16) (actual time=232.953..367.689 rows=25259 loops=1)
-> HashAggregate (cost=3972.76..4090.46 rows=9416 width=8) (actual time=232.946..288.922 rows=25259 loops=1)
Filter: (count(*) > 0)
-> Seq Scan on person (cost=0.00..3737.68 rows=31344 width=8) (actual time=7.366..137.853 rows=38497 loops=1)
Filter: ((age >= 18) AND (user_id = 1) AND ((gender)::text = 'MALE'::text))
Rows Removed by Filter: 64856
-> Materialize (cost=15560.55..67482.77 rows=739113 width=44) (actual time=836.591..1049.115 rows=352 loops=1)
-> Merge Join (cost=15560.55..65634.99 rows=739113 width=44) (actual time=836.577..1047.666 rows=352 loops=1)
Merge Cond: (public.person.household_id = b.household_id)
Join Filter: (random() <= (fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob))
Rows Removed by Join Filter: 11054
-> Sort (cost=4728.64..4747.85 rows=7684 width=20) (actual time=451.992..506.614 rows=26755 loops=1)
Sort Key: public.person.household_id
Sort Method: external merge Disk: 888kB
-> Hash Join (cost=3912.57..4232.73 rows=7684 width=20) (actual time=208.538..357.160 rows=26755 loops=1)
Hash Cond: ((min(public.person.age)) = fertility_cond_age.age)
-> HashAggregate (cost=3908.20..4010.65 rows=10245 width=12) (actual time=208.048..263.094 rows=26755 loops=1)
-> Seq Scan on person (cost=0.00..3737.68 rows=34104 width=12) (actual time=1.612..111.773 rows=42369 loops=1)
Filter: ((age >= 18) AND (user_id = 1) AND ((gender)::text = 'FEMALE'::text))
Rows Removed by Filter: 60984
-> Hash (cost=2.50..2.50 rows=150 width=12) (actual time=0.464..0.464 rows=150 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 6kB
-> Seq Scan on fertility_cond_age (cost=0.00..2.50 rows=150 width=12) (actual time=0.019..0.233 rows=150 loops=1)
-> Materialize (cost=10831.91..11120.48 rows=57715 width=24) (actual time=380.522..455.086 rows=14412 loops=1)
-> Sort (cost=10831.91..10976.20 rows=57715 width=24) (actual time=380.504..411.816 rows=14412 loops=1)
Sort Key: b.household_id
Sort Method: external merge Disk: 480kB
-> Merge Join (cost=4205.69..5081.12 rows=57715 width=24) (actual time=221.294..301.093 rows=14412 loops=1)
Merge Cond: (fertility_cond_prob_number_of_children.number_of_children = b.number_of_children)
-> Sort (cost=135.34..140.19 rows=1940 width=12) (actual time=0.098..0.107 rows=7 loops=1)
Sort Key: fertility_cond_prob_number_of_children.number_of_children
Sort Method: quicksort Memory: 17kB
-> Seq Scan on fertility_cond_prob_number_of_children (cost=0.00..29.40 rows=1940 width=12) (actual time=0.015..0.051 rows=25 loops=1)
-> Sort (cost=4070.35..4085.23 rows=5950 width=16) (actual time=221.176..247.951 rows=14412 loops=1)
Sort Key: b.number_of_children
Sort Method: quicksort Memory: 819kB
-> Subquery Scan on b (cost=3578.32..3697.32 rows=5950 width=16) (actual time=118.096..193.664 rows=14412 loops=1)
-> HashAggregate (cost=3578.32..3637.82 rows=5950 width=8) (actual time=118.090..147.604 rows=14412 loops=1)
-> Seq Scan on person (cost=0.00..3479.30 rows=19806 width=8) (actual time=30.973..70.129 rows=20025 loops=1)
Filter: ((user_id = 1) AND ((gender)::text = 'CHILD'::text))
Rows Removed by Filter: 83328
我该怎么做才能提高查询的性能?
我尝试添加索引,但这让事情变得更糟(查询在没有索引的情况下运行得更快)。
更新 1:
查询
SELECT
a.household_id household_id,
age_of_youngest_woman,
a.number_of_children,
a.number_of_men,
fertility_cond_prob_number_of_children.cond_prob cond_prob_number_of_children,
fertility_cond_age.cond_prob cond_prob_age,
fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob total_cond_prob,
random() <= (874. / 1703.) is_newborn_male
FROM
(SELECT
household_id,
MIN(CASE WHEN
(gender = 'FEMALE') and
(age >= 18)
THEN age
END) age_of_youngest_woman,
COUNT(CASE WHEN (gender = 'CHILD')
THEN 1
END) number_of_children,
COUNT(CASE WHEN (gender = 'MALE') and
(age >= 18)
THEN 1
END) number_of_men
FROM person
WHERE user_id = 1
GROUP BY household_id) a
JOIN fertility_cond_prob_number_of_children ON (fertility_cond_prob_number_of_children.number_of_children = a.number_of_children)
JOIN fertility_cond_age ON (fertility_cond_age.age = a.age_of_youngest_woman)
WHERE
(a.number_of_men > 0) and
(random() <= (fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob))
具有以下性能特点:
Hash Join (cost=21783.55..21871.65 rows=6 width=44) (actual time=701.418..3042.547 rows=247 loops=1)
Hash Cond: ((min(CASE WHEN (((person.gender)::text = 'FEMALE'::text) AND (person.age >= 18)) THEN person.age ELSE NULL::integer END)) = fertility_cond_age.age)
Join Filter: (random() <= (fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob))
Rows Removed by Join Filter: 18741
-> Nested Loop (cost=21779.17..21866.82 rows=19 width=36) (actual time=696.983..2949.993 rows=25647 loops=1)
Join Filter: ((count(CASE WHEN ((person.gender)::text = 'CHILD'::text) THEN 1 ELSE NULL::integer END)) = fertility_cond_prob_number_of_children.number_of_children)
Rows Removed by Join Filter: 615528
-> Seq Scan on fertility_cond_prob_number_of_children (cost=0.00..29.40 rows=1940 width=12) (actual time=0.007..0.098 rows=25 loops=1)
-> Materialize (cost=21779.17..21779.23 rows=2 width=28) (actual time=27.894..76.814 rows=25647 loops=25)
-> HashAggregate (cost=21779.17..21779.20 rows=2 width=50) (actual time=696.954..764.681 rows=25647 loops=1)
Filter: (count(CASE WHEN (((person.gender)::text = 'MALE'::text) AND (person.age >= 18)) THEN 1 ELSE NULL::integer END) > 0)
Rows Removed by Filter: 8112
-> Seq Scan on person (cost=0.00..21648.46 rows=4357 width=50) (actual time=13.910..343.198 rows=106158 loops=1)
Filter: (user_id = 1)
-> Hash (cost=2.50..2.50 rows=150 width=12) (actual time=0.480..0.480 rows=150 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 6kB
-> Seq Scan on fertility_cond_age (cost=0.00..2.50 rows=150 width=12) (actual time=0.016..0.235 rows=150 loops=1)
Total runtime: 3045.405 ms
表定义:
CREATE TABLE fertility_cond_prob_number_of_children(number_of_children integer, cond_prob double precision);
CREATE TABLE fertility_cond_age(age integer, cond_prob double precision);
CREATE TABLE fertility_households(household_id bigint, user_id bigint, age_of_woman integer, number_of_children integer);
CREATE TABLE person (
id SERIAL,
user_id bigint NOT NULL,
age integer NOT NULL,
monthly_income double precision NOT NULL,
gender character varying(10),
household_id bigint);
最佳答案
尝试这样的事情:
SELECT
a.household_id,
a.age_of_youngest_woman,
a.number_of_children,
a.number_of_men,
fertility_cond_prob_number_of_children.cond_prob cond_prob_number_of_children,
fertility_cond_age.cond_prob cond_prob_age,
fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob total_cond_prob,
random() <= (874. / 1703.) is_newborn_male
FROM
(SELECT household_id,
MIN(CASE WHEN (gender = 'FEMALE')
and (age >= 18)
THEN age
END) age_of_youngest_woman,
COUNT(CASE WHEN (gender = 'CHILD')
THEN 1
END) number_of_children,
COUNT(CASE WHEN (gender = 'MALE')
and (age >= 18)
THEN 1
END) number_of_men
FROM person
WHERE user_id = 1
GROUP BY household_id) a
JOIN fertility_cond_prob_number_of_children ON (fertility_cond_prob_number_of_children.number_of_children = a.number_of_children)
JOIN fertility_cond_age ON (fertility_cond_age.age = a.age_of_youngest_woman)
WHERE
(a.number_of_men > 0) and
(random() <= (fertility_cond_prob_number_of_children.cond_prob * fertility_cond_age.cond_prob))
我使用一些 CASE 语句将 3 个内表扫描更改为 1 个扫描,并将左连接替换为简单连接(由于 WHERE 子句而没有区别)。它应该会加快整个查询的速度。
您可能需要更正一些拼写错误才能正确运行,我没有测试过。
关于postgresql - Postgres 查询的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13444234/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!