English 中文(简体)
QL服务器,T-SQL,在我的问题中,有较快的路标?
原标题:SQL Server, T-SQL is there a faster way to substring the following string in my question?

我有像样的str。

OPEN SYSTsystem SUBGR (GM/BTIB(1111)/BTITDBL(2222)/BTVY(4444)/ACSVTYSAG

载于我的小组专栏数据库。

我想做的是,从这句话中抽取2222。 我使用的法典就是这样。

    SELECT 
        SUBSTRING(GROUPS, CHARINDEX( ( ,GROUPS, CHARINDEX( ( ,GROUPS, CHARINDEX( ( ,GROUPS,0)+1)+1)+1, 4 ) AS GroupNo

    FROM MY_TABLE

    WHERE

    ISNUMERIC(SUBSTRING(GROUPS, CHARINDEX( ( ,GROUPS, CHARINDEX( ( ,GROUPS, CHARINDEX( ( ,GROUPS,0)+1)+1)+1, 4 )) = 1

我需要通过改变我所使用替代方式或改变某种逻辑来加快上述法典的步伐。 你们能否告诉我,我的法典可以改进哪些事情?

问题回答

页: 1

如果你这样做,我会考虑将插入/更新/编辑数据分成一个单独的表格(见MY_TABLE_ELEments,然后从MY_TABLE/code>并入MY_TABLE_ELEments

例如,你可以使用split功能(如果我正确理解你的代码),将每一部分都存于MY_TABLE_ELEments,或仅按数字部分。

一套基于规定的执行。

这比你单行的表现要低,但应更大幅度地分配更大的成果,特别是如果你用固定数字表取代计算动态数字表的计算。

DECLARE @t TABLE
(groups VARCHAR(250))

INSERT @t
VALUES ( OPEN SYSTEMS SUB GR (GM/BTIB(1111)/BTITDBL(2222)/BTVY(4444)/ACSVTYSAG) )

INSERT @t
VALUES ( OPEN SYSTEMS SUB GR (GM/BTIB(1111)/BTITDBL(3333)/BTVY(4444)/ACSVTYSAG) )

DECLARE @chr_delim CHAR(1)
SET @chr_delim =  ( 

-- nums_cte generates a dynamic numbers table
-- replace this with your own numbers table if you have one
;WITH nums_cte 
AS 
( 
        SELECT 1 AS n 
        UNION ALL 
        SELECT n+1 FROM nums_cte 
        WHERE n < 250
) 
,splitCTE    
AS
(
        SELECT  SUBSTRING(s,n,CHARINDEX(@chr_delim, s + @chr_delim,n) -n) AS ELEMENT
                ,s
                ,ROW_NUMBER() OVER (PARTITION BY s
                                    ORDER BY n
                                   ) AS rn
        FROM (SELECT groups AS s FROM @t) AS D
        JOIN nums_cte
        ON n <= LEN(s)
        AND SUBSTRING(@chr_delim + s,n,1) = @chr_delim 
)
SELECT LEFT(ELEMENT,4) AS GroupNo
       ,s AS originalString
FROM splitCTE
WHERE rn = 4
AND   ISNUMERIC(LEFT(ELEMENT,4)) = 1
OPTION (MAXRECURSION 0)




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...