[英]sql query optimization (oracle)
It has been a while since my sql days thus I am wondering whether it is possible to further optimize the following query? 自从我的sql天以来已经有一段时间了,因此我想知道是否有可能进一步优化以下查询? The goal is to collect all accounts for each accountant including all the bookings and movements associated with it.
目标是收集每个会计师的所有帐户,包括与之关联的所有预订和变动。 Performance/query-time is very important since there are '3 digit'-million datasets...
性能/查询时间非常重要,因为有“ 3位数”百万数据集。
select Accountant.person_id,
Account.account_id,
Account.number,
Account.balance,
Account_Type.type_number,
Booking.booking_id,
Booking.amount,
Movement.movement_date,
Movement.movement_desc
from Accountant
join Account on Accountant.person_id = Account.person_id
join Account_Type on Account.account_type_id = Account_Type.account_type_id
left outer join Booking on Account.account_id = Booking.account_id
left outer join Movement on Booking.movement_id = Movement.movement_id;
The entity model looks something like that: 实体模型看起来像这样:
UPDATE: Since some of you are wondering: Yes I am simply selecting hundreds of million of rows since the query is used to migrate data. 更新:你们当中有些人在想:是的,因为查询用于迁移数据,所以我只是选择亿万行。 The data queried is used to construct a new data structure which is put in another database...
所查询的数据用于构造新的数据结构,该数据结构被放入另一个数据库中。
As others have already mentioned: It is strange, you want to select hundreds of millions of records in one go. 正如其他人已经提到的:奇怪的是,您想一次性选择数亿条记录。
Aside from that: 除此之外:
The join syntax is clearer and recommended. 连接语法更清晰,建议使用。 I don't think you can optimise the query itself any further, but you could check the DB schema, or investigate alternatives.
我认为您无法进一步优化查询本身,但是可以检查数据库架构或研究替代方案。
DB Schema: 数据库架构:
You could also consider a reporting table that is built at the end of every day (or more frequently if required) - and query from that. 您还可以考虑每天结束时建立的报告表(如果需要,可以更频繁地建立)-并从中进行查询。 Oracle supports materialised views that might help.
Oracle支持可能会有所帮助的实例化视图。
Finally, a query that returns n,000,000 records really isn't much use to anyone, so you might reconsider your logic. 最后,对于任何人来说,返回n,000,000条记录的查询实际上都没有多大用处,因此您可能需要重新考虑自己的逻辑。 If you're presenting these records to a user, you need to page them (so limit the query to a page size) - or if you're presenting summary info, rewrite the query to use aggregate functions (sum/max/avg etc.) Don't use code to do what a DB does best.
如果要向用户显示这些记录,则需要分页(因此将查询限制为页面大小)-如果要显示摘要信息,则重写查询以使用聚合函数(sum / max / avg等) 。)不要使用代码来做数据库最擅长的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.