<bdo id='wn7gc'></bdo><ul id='wn7gc'></ul>

<small id='wn7gc'></small><noframes id='wn7gc'>

    <i id='wn7gc'><tr id='wn7gc'><dt id='wn7gc'><q id='wn7gc'><span id='wn7gc'><b id='wn7gc'><form id='wn7gc'><ins id='wn7gc'></ins><ul id='wn7gc'></ul><sub id='wn7gc'></sub></form><legend id='wn7gc'></legend><bdo id='wn7gc'><pre id='wn7gc'><center id='wn7gc'></center></pre></bdo></b><th id='wn7gc'></th></span></q></dt></tr></i><div id='wn7gc'><tfoot id='wn7gc'></tfoot><dl id='wn7gc'><fieldset id='wn7gc'></fieldset></dl></div>
    1. <tfoot id='wn7gc'></tfoot>
    2. <legend id='wn7gc'><style id='wn7gc'><dir id='wn7gc'><q id='wn7gc'></q></dir></style></legend>

      SQL Server - 计算列上的索引?

      时间:2023-10-25

          <tbody id='Anyvs'></tbody>

            <bdo id='Anyvs'></bdo><ul id='Anyvs'></ul>
                <i id='Anyvs'><tr id='Anyvs'><dt id='Anyvs'><q id='Anyvs'><span id='Anyvs'><b id='Anyvs'><form id='Anyvs'><ins id='Anyvs'></ins><ul id='Anyvs'></ul><sub id='Anyvs'></sub></form><legend id='Anyvs'></legend><bdo id='Anyvs'><pre id='Anyvs'><center id='Anyvs'></center></pre></bdo></b><th id='Anyvs'></th></span></q></dt></tr></i><div id='Anyvs'><tfoot id='Anyvs'></tfoot><dl id='Anyvs'><fieldset id='Anyvs'></fieldset></dl></div>
                <legend id='Anyvs'><style id='Anyvs'><dir id='Anyvs'><q id='Anyvs'></q></dir></style></legend>

                <small id='Anyvs'></small><noframes id='Anyvs'>

                <tfoot id='Anyvs'></tfoot>

              1. 本文介绍了SQL Server - 计算列上的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我多次加入一个表,每次,我都根据其中一列的 SUBSTRING 的结果加入(或过滤)(它是一个字符串,但用零填充左,并且我不在乎最后四位数字).因此,即使此列已编入索引并且我的查询将使用该索引,它也会执行表扫描,因为 SUBSTRING 本身未编入索引,因此 SQL Server 在加入之前必须为每一行计算它.

                I'm joining to a table dozens of different times, and every time, I join (or filter) based on the results of a SUBSTRING of one of the columns (it's a string, but left-padded with zeros, and I don't care about the last four digits). As a result, even though this column is indexed and my query would use the index, it does a table scan because the SUBSTRING itself isn't indexed, so SQL Server has to calculate it for every row before as it's joining.

                我正在寻找有关如何加快此过程的任何想法.目前,表上有一个视图(它是一个SELECT * FROM",只是为了给表一个友好的名称),我正在考虑向计算的视图添加一列,然后对其进行索引.不过,我愿意接受其他建议 - 有什么想法吗?

                I'm looking for any ideas on how to speed up this process. Currently, there's a view on the table (It's a "SELECT * FROM", just to give the table a friendly name), and I'm considering adding a column to the view that's computed, and then indexing that. I'm open to other suggestions, though - any thoughts?

                更多细节:我应该一开始就分享这个.该表从我们的计费系统接收复制,因此编辑基础表以添加计算列不是一种选择.任何计算列都必须添加到表的视图中.此外,前导零并不总是前导零 - 它们有时是我不感兴趣的其他数据.我想真正的问题是我如何加入 VARCHAR 列中间的数据,而还使用索引?全文搜索?"

                MORE DETAIL: I should have shared this to begin with. The table receives replication from our billing system, so editing the underlying table to add a computed column is not an option. Any computed column would have to be added to the view on the table. Also, the leading zeros aren't always leading zeros - they're sometimes other data that I'm not interested in. I suppose the real question is "How can I join to data in the middle of a VARCHAR column while also making use of an index? Full-text Search?"

                澄清我的例子我正在简化,但基本上,假设我正在尝试使用以下值查找列中的值:

                Clarifying my example I'm simplifying, but essentially, let's say I'm trying to look up values in a column with the following values:

                00000012345MoreStuff
                00000012345Whatever
                19834212345
                Houses12345837443GGD
                00000023456MoreStuff
                

                我对 SUBSTRING(7,5)="12345" 的行感兴趣,所以我想要第 1-4 行,而不是第 5 行.我的提议是在我的SELECT *"中添加一列" 包含此子字符串的视图,然后基于它进行索引.这更有意义吗?

                I'm interested in rows where SUBSTRING(7,5)="12345", so I'd want rows 1-4, but not row 5. What I'm proposing is adding a column to my "SELECT *" view that has this substring in it, and then indexing based on that. Does that make more sense?

                推荐答案

                假设您的字段采用这种格式:

                Assuming you have your fields in this format:

                00Data0007
                000000Data0011
                0000Data0015
                

                ,您可以执行以下操作:

                , you can do the following:

                • 创建一个计算列:ndata AS RIGHT(REVERSE(data), LEN(data) - 4)

                这会将您的列转换为以下内容:

                This will transform your columns into the following:

                ataD00
                ataD000000
                ataD0000
                

              2. 在该列上创建索引

              3. Create an index on that column

                发出此查询以搜索字符串 Data:

                Issue this query to search for the string Data:

                SELECT  *
                FROM    mytable
                WHERE   ndata LIKE N'ataD%'
                        AND SUBSTRING(ndata, LEN(N'ataD') + 1, LEN(ndata)) = REPLICATE('0', LEN(ndata) - LEN('ataD'))
                

                第一个条件将使用索引进行粗过滤.

                The first condition will use an index for coarse filtering.

                第二个将确保所有前导字符(成为计算列中的尾随字符)都为零.

                The second will make sure that all leading characters (that became the trailing characters in the computed column) are nothing but zeros.

                有关性能详细信息,请参阅我博客中的此条目:

                See this entry in my blog for performance detail:

                • SQL Server:使用索引引导通配符匹配
                • SQL Server: leading wildcard match using an index

                更新

                如果您只想在 SUBSTRING 上建立索引而不更改架构,则可以选择创建视图.

                If you just want an index on SUBSTRING without changing your schema, creating a view is an option.

                CREATE VIEW v_substring75
                WITH SCHEMABINDING
                AS
                SELECT  s.id, s.data, SUBSTRING(data, 7, 5) AS substring75
                FROM    mytable
                
                CREATE UNIQUE CLUSTERED INDEX UX_substring75_substring_id ON (substring75, id)
                
                SELECT  id, data
                FROM    v_substring75
                WHERE   substring75 = '12345'
                

                这篇关于SQL Server - 计算列上的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

              4. 上一篇:在 SQL Server 中,我应该为标识列创建索引,还是自动创建? 下一篇:在空表中执行缓慢的查询.(删除大量插入后)

                相关文章

                  • <bdo id='lzRo5'></bdo><ul id='lzRo5'></ul>
                  <legend id='lzRo5'><style id='lzRo5'><dir id='lzRo5'><q id='lzRo5'></q></dir></style></legend>
                  <tfoot id='lzRo5'></tfoot>

                  1. <i id='lzRo5'><tr id='lzRo5'><dt id='lzRo5'><q id='lzRo5'><span id='lzRo5'><b id='lzRo5'><form id='lzRo5'><ins id='lzRo5'></ins><ul id='lzRo5'></ul><sub id='lzRo5'></sub></form><legend id='lzRo5'></legend><bdo id='lzRo5'><pre id='lzRo5'><center id='lzRo5'></center></pre></bdo></b><th id='lzRo5'></th></span></q></dt></tr></i><div id='lzRo5'><tfoot id='lzRo5'></tfoot><dl id='lzRo5'><fieldset id='lzRo5'></fieldset></dl></div>
                  2. <small id='lzRo5'></small><noframes id='lzRo5'>