- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是 SQL 的新手,需要帮助我有 4 个表:
helmet arm
+------+---------+-----+--------+ +------+---------+-----+--------+
| id | name | def | weight | | id | name | def | weight |
+------+---------+-----+--------+ +------+---------+-----+--------+
| 1 | head1 | 5 | 2.2 | | 1 | arm1 | 4 | 2.7 |
| 2 | head2 | 6 | 2.9 | | 2 | arm2 | 5 | 3.1 |
| 3 | head3 | 7 | 3.5 | | 3 | arm3 | 2 | 1.8 |
+------+---------+-----+--------+ +------+---------+-----+--------+
body leg
+------+---------+-----+--------+ +------+---------+-----+--------+
| id | name | def | weight | | id | name | def | weight |
+------+---------+-----+--------+ +------+---------+-----+--------+
| 1 | body1 | 10 | 5.5 | | 1 | leg1 | 8 | 3.5 |
| 2 | body2 | 5 | 2.4 | | 2 | leg2 | 5 | 2.0 |
| 3 | body3 | 17 | 6.9 | | 3 | leg3 | 8 | 1.8 |
+------+---------+-----+--------+ +------+---------+-----+--------+`
我正在寻找最高的 totaldef which totalweight <= input
像这样:总重量 <= 10
查询:
select
helmet.name as hname, body.name as bname,
arm.name as aname, leg.name as lname,
helmet.poise + body.poise + arm.poise + leg.poise as totalpoise,
helmet.weight + body.weight + arm.weight + leg.weight as totalweight
from
helmet
inner join
body on 1=1
inner join
arm on 1=1
inner join
leg on 1=1
where
helmet.weight + body.weight + arm.weight + leg.weight <= 10
order by
totalpoise desc
limit 5
结果:
+-------+-------+-------+-------+----------+-------------+
| hname | bname | aname | lname | totaldef | totalweight |
+-------+-------+------ +-------+----------+-------------+
| head2 | body2 | arm1 | leg3 | 23 | 9.8 |
| head1 | body2 | arm2 | leg3 | 23 | 9.5 |
| head3 | body2 | arm3 | leg3 | 22 | 9.5 |
| head1 | body2 | arm1 | leg3 | 22 | 9.1 |
| head2 | body2 | arm3 | leg3 | 21 | 8.9 |
+-------+-------+-------+-------+----------+-------------+
问题是每个表大约有 100 行,因此可能的结果是 1 亿多行。查询需要很长时间。我不确定这与我的硬件或数据库或查询类型有关。
P.S:我使用 HDD 并有 8GB 内存。我已经在 MySQL 和 PostgreSQL 上进行了测试。
更新我还没有创建索引。
这是解释计划吗? explain plan
需要多长时间?这取决于输入。在 MySQL 上大约需要几分钟 - 几个小时。
在 PostgreSQL 上大约需要 30 秒 - 2 分钟。
更新 2 我的表格永远不会改变。那么我可以将所有结果存储在一个表中吗?有帮助吗?
更新 3 我考虑分区。它可能快得多,但问题是如果下分区中的某些 [armor set] 的 totaldef 比上分区中的 [armor set] 多。示例:
[head1,arm1,body1,leg1][totaldef 25][totalweight 9.9]
[head2,arm2,body2,leg2][totaldef 20][totalweight 11.0]
所以分区总重量 >10 会错过那个 [armor set],因为它在其他分区中。
这是供任何想要测试的人使用的 CSV 文件。 CSV file
更新 4 我认为最快的方法是创建 materialized view但我想性能的关键是对其进行排序。我不知道哪种排序可以帮助物化 View 或索引,但我对它们都进行了排序并且很有用。
没想到会得到这么多帮助。谢谢。
最佳答案
A materialized view具有适当索引的性能相当不错,在我使用 Postgresql 配置的老化 SSD 桌面上运行 1.8 秒:
create materialized view v as
select
h.name as hname, b.name as bname, a.name as aname, l.name as lname,
total_poise, total_weight
from
helmet h
cross join
body b
cross join
arm a
cross join
leg l
cross join lateral (
select
h.weight + b.weight + l.weight + a.weight as total_weight,
h.poise + b.poise + l.poise + a.poise as total_poise
) total
order by total_poise desc, total_weight
;
create index v_index on v (total_poise desc, total_weight);
执行与分析:
select *
from v
where total_weight <= 10
order by total_poise desc, total_weight
limit 5
;
hname | bname | aname | lname | total_poise | total_weight
-----------------------+--------------------------+------------------------+--------------------------+-------------+--------------
Fume Sorcerer Mask+10 | Moon Butterfly Wings+5 | Velstadt`s Gauntlets+5 | Prisoner`s Waistcloth+10 | 20 | 9.4
Fume Sorcerer Mask+10 | Lion Warrior Cape+10 | Velstadt`s Gauntlets+5 | Prisoner`s Waistcloth+10 | 20 | 9.5
Fume Sorcerer Mask+10 | Red Lion Warrior Cape+10 | Velstadt`s Gauntlets+5 | Prisoner`s Waistcloth+10 | 20 | 9.5
Fume Sorcerer Mask+10 | Moon Butterfly Wings+5 | Velstadt`s Gauntlets+5 | Lion Warrior Skirt+10 | 20 | 9.6
Fume Sorcerer Mask+10 | Moon Butterfly Wings+5 | Velstadt`s Gauntlets+5 | Moon Butterfly Skirt+10 | 20 | 9.6
explain analyze
select *
from v
where total_weight <= 10
order by total_poise desc, total_weight
limit 5
;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.57..11.71 rows=5 width=88) (actual time=1847.680..1847.694 rows=5 loops=1)
-> Index Scan using v_index on v (cost=0.57..11191615.70 rows=5020071 width=88) (actual time=1847.678..1847.691 rows=5 loops=1)
Index Cond: (total_weight <= '10'::double precision)
Planning time: 0.126 ms
Execution time: 1847.722 ms
关于mysql - 是否有可能更快地获得查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42312741/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!