简体   繁体   English

MySQL选择一年中的某天和一年之间的记录

[英]MySQL select records between day of the year and year

I have a table where I insert the year, month number, week number of the year, and day number of the year with each record. 我有一个表,其中在每个记录中插入年,月号,年的周号和年的天号。

I'm using those fields to filter records instead of full date because I have millions of records, and I want to increase the performance by comparing integers instead of dates. 我使用这些字段来过滤记录而不是完整日期,因为我有数百万条记录,并且我想通过比较整数而不是日期来提高性能。

I was able to select records between dates based the day number and year. 我能够根据天数和年份在日期之间选择记录。 The query works well if the years are the same, but once I change the year the query doesn't work because I'm using AND. 如果年份相同,则查询效果很好,但是一旦更改年份,该查询将不起作用,因为我使用的是AND。

Table looks like this: 表看起来像这样:

  • ID ID
  • Day
  • Week
  • Month
  • Year
  • Full Date 完整日期

Here is the working query 这是工作查询

SELECT COUNT(id) AS records_count
FROM table1
WHERE table1.day >= 176
AND table1.day <= 275
AND table1.year <= 2015
AND table1.year >= 2015

And this is the query that I need to adjust 这是我需要调整的查询

SELECT COUNT(id) AS records_count
FROM table1
WHERE table1.day >= 275
AND table1.day <= 176
AND table1.year <= 2014
AND table1.year >= 2015
SELECT COUNT(id) AS records_count
FROM table1
WHERE (year = 2014 and day >= 275)
   OR (year = 2015 and day <= 176)

And as baao commented - an indexed date column is super fast and easier to query. 正如baao所评论的那样-索引日期列超快速且易于查询。

Your method of storing the dates is exactly wrong for what you want to do. 您存储日期的方法与您要执行的操作完全错误。 The general form would be: 一般形式为:

where (year = 2014 and day >= 275 or year > 2014) and
      (year = 2015 and day <= 176 or year < 2015)

This will work for any pair of years, not just those that are one year apart. 这将适用于任何两年,而不仅仅是相隔一年的年份。

Now, if you stored the dates normally, then you would simply do: 现在,如果您正常存储日期,则只需执行以下操作:

where date >= makedate(2014, 275) and date <= makedate(2015, 176)

What is really, really nice about this structure is that MySQL can use an index on date . 这种结构的真正真正的好处是MySQL可以在date上使用索引。 That isn't possible with your query. 您的查询无法做到这一点。

In general, a micro-optimization such as using integers instead of some other data type is not worth the effort in relational databases. 通常,在关系数据库中进行微优化(例如使用整数代替某些其他数据类型)是不值得的。 Usually, the cost of reading the data is much more expensive than processing within a row. 通常,读取数据的成本比连续处理要昂贵得多。 And, in fact, this example is a great example of it. 而且,实际上,这个例子是一个很好的例子。 Why try to increase the speed of comparisons when you can remove the need for them entirely using an index? 当您可以完全使用索引消除比较的需要时,为什么要尝试提高比较的速度?

Generalized solution (can be from year 2000 to 2020 for example) 广义解(例如,从2000年到2020年)

SELECT COUNT(id) AS cnt
FROM tbl
WHERE ((year > under_min_year and year < above_max_year)
      and ((year <> min_year and year <> max_year)
           or (year = min_year and day > 265)
           or (year = max_year and day < 300)))

问题是您要计算年份小于或等于2014且同一年大于或等于2015的记录。没有小于或等于2014且同时大于或等于2015的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM