douchao1957 2016-09-28 15:44
浏览 22
已采纳

在行ID中搜索大型Mysql数据库的确切日期

I have a large mysql database which is about 10gb large. One of the tables in the database is called

clients

In that table there is a colum named

case

The date this client is created is mixed into the number within this column.

Here is an example of an entry in case

011706-0001

The 06 part means this client was created in 2006. I need to pull all the clients that were created in 2015 and 2016. So I need to query for anything that case has a 15 or 16 before the dash.

For example, 000015-0000 or 000016-0000

Is there a way to do this with only mysql? My thought process was I would have to query the whole column then use php to preg_match()

I am worried that based on the size of the database this would cause problems.

  • 写回答

1条回答 默认 最新

  • dongmao9217 2016-09-28 15:52
    关注

    To locate rows that have a case column value that contains '06-' (the characters 0 and 6 followed by a dash ...

    One option is to use a LIKE comparison operator:

     SELECT ...
       FROM clients t
      WHERE t.case LIKE '%06-%'
      ORDER BY ...
    

    The percent sign characters are wildcards in the LIKE comparison, which match any number of characters (zero, one or more.)

    MySQL will need to evaluate that condition for every row in the table. MySQL can't make use of an index range scan operation with that.

     SELECT ...
       FROM clients t
      WHERE t.case LIKE '%15-%'
         OR t.case LIKE '%16-%'
      ORDER BY ...
    

    That will evaluate to true for any values that include the sequence of three characters '15-' or '16-'.

    If there's a more standard format for the values in the case column, where the value always starts with exactly six characters representing date 'mmddyy-nnnnn' and you only want to match the 5th thru 7th characters, you could use the underscore wildcard character which matches any one character (in the LIKE comparison) for example... using four underscores

      t.case LIKE '____16-%'
    

    Or you could use a SUBSTR function to extract the three characters from the case value, and perform an equality comparison...

      SUBSTR(t.case,5,3) = '15-'
    
      SUBSTR(t.case,5,3) IN ('15-','16-')
    

    It's also possible to make use of a REGEXP comparison in place of the LIKE comparison.


    In terms of performance, all of the above approaches are going to need to crank through every row in the table, to evaluate the comparison condition.

    If that date value was stored as a separate column, as a DATE datatype, and there was an index with that as the leading column, then MySQL could make effective use of a range scan operation, for a query like this...

      WHERE t.casedate >= '2015-01-01'
        AND t.casedate <  '2017-01-01'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流