【问题标题】:Spring model to use multiple tablesSpring模型使用多个表
【发布时间】:2017-06-20 17:53:54
【问题描述】:

我有一个包含超过 5000 万行的数据库表。通过索引,我将选择单行的请求时间从 50 秒减少到 2-5 秒。因为我仍然需要检索多个单行。通过对仅包含上个月记录的视图执行选择,我能够进一步减少( 不幸的是,设备可能在上个月没有产生记录,但我仍然需要它的最新记录。

如果主表上没有结果,是否有可能告诉 spring mvc “回退”到另一个表?类似:

@Entity
@Table
(
schema="tbl",
name="name_of_view_for_last_month",
fallbackname="name_of_actual_table"
)

【问题讨论】:

    标签: mysql spring spring-mvc spring-jdbc


    【解决方案1】:
    select 
      case check.exists
      when 1 then check.id 
      when 0 then (select id 
                   from name_of_actual_table
                   where <condition to get single row>)
      end as result
    from (
        select count(id) as exist, id
        from name_of_view_for_last_month last
        where <condition to get single row>) check
    

    像上面描述的那样在 SQL 级别上解决它怎么样?

    在 Spring 级别,您可以为完整数据表和月份视图创建 2 个 dao 类。

    在服务中调用monthDataDao,如果没有返回,则使用相同的请求调用fullDataDao

    【讨论】:

    • 非常感谢您为我指明了正确的方向。不幸的是,你的代码不是直接的 MySQL(或者至少不能在工作台上工作),但我现在有了一个很好的基础。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2017-04-28
    • 2012-03-13
    • 2021-11-05
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多