更新时间:2023-01-29 19:54:50
如果可能,尽量远离生成数据。它使非常简单的查询变得复杂,但最重要的是:它使优化器无法结束。
If at all possible, try to stay away from generating data on the fly. It makes very simple queries ridiculusly complex, but above all: it confuses the optimizer to no end.
如果需要一系列整数,请使用静态的整数表。如果您需要一系列日期,数月或其他日期,请使用日历表。除非你正在处理一些真正非凡的要求,否则静态表就是要走的路。
If you need a series of integers, use a static table of integers. If you need a series of dates, months or whatever, use a calendar table. Unless you are dealing with some truly extraordinary requirements, a static table is the way to go.
我举了一个例子,说明如何创建数字表和最小日历表(仅日期)在此答案。
I gave an example on how to create a table of numbers and a minimal calendar table(only dates) in this answer.
如果你有这些表,就很容易解决你的查询。
If you have those tables in place, it becomes easy to solve your query.