【问题标题】:One MySQL query for taking the substr of two fields combined without concat on the full fields一个 MySQL 查询,用于获取两个字段的 substr,在完整字段上没有 concat 组合
【发布时间】:2011-04-16 22:17:40
【问题描述】:

表:字段1,字段2

给定偏移量和长度,我希望结果是:

select substr(concat(field1,field2),offset,len) from table

由于 field1 和 field2 是大的 varchars,并且这个查询是一个瓶颈,我不想不断地将两个大字段连接在一起,而宁愿做一些类似的事情

select case when (offset+len) < length(field1) then substr(field1,offset,len) else....

存在三种不同的情况:1) field1 的 substr,2) field2 的 substr 3) substr(field1) 与 substr(field2) 连接

有人有这个脑筋急转弯的解决方案吗?谢谢!

【问题讨论】:

    标签: mysql sql select substr


    【解决方案1】:

    MySQL 有一个IF 函数,它可以使查询比使用 CASE 结构更清晰。

    不管你怎么写,它都很贵。为什么不创建另一列(去冗余!)并在那里保存正确的值?如今,冗余不再是问题,它比任何类型的 concat、case 或 if 都快得多。

    (如果offset 不是静态的,这不是解决方案。如果offset 是,但len 不是,它仍然是一个解决方案。)

    【讨论】:

    • 偏移量是变量 len 是静态的
    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 2012-04-02
    • 2014-07-21
    • 2016-07-27
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多