【问题标题】:SQL Server select query performance tuningSQL Server 选择查询性能调优
【发布时间】:2019-08-07 00:59:13
【问题描述】:

我的选择查询存在性能问题。查询可能需要超过 4 分钟,这太长了,SQL Server 表有 50 亿条记录。

这里是查询:

Select
    ID,Name,City,locationfile,locationpath
From
    Table
where 
    locationfile like '%euro%'`

输出结果:4 分钟内返回 90,000 行。

ID primaryKey 上的聚集索引

在 locationfile 上创建的非聚集索引包括 (City,locationpath)

这是统计数据:逻辑读取 10 070 208、物理读取 2、预读 10 051 119

如何提高性能才能得到结果?

【问题讨论】:

  • 你看过全文搜索吗?
  • locationfile 上没有任何包含列的索引将允许更少的读取来完成索引 scan,但 like '%euro%' 将始终需要扫描(而不是 lookup) 由于前导通配符。
  • 我们目前可能无法实现全索引文本搜索..,,
  • HABO:您的建议是在没有包含列的位置文件上创建非聚集索引?..
  • We may not implement full index text search at this moment 有什么解释吗?是否还有其他您可能无法实施的事情?

标签: sql-server performance tsql


【解决方案1】:

如果你真的不能实现 FTS,这里是我有点疯狂的强制索引使用的建议:

1) 检查位置文件有多少个字符,以及子字符串“欧元”可以出现在该字符串中的最大字符索引是多少。让 N 成为那个数字。

2) 创建 N 个生成的持久列 locationfile1, locationfile2, ...,其值为 substring(1, len(locationfile)), substring(2, len(locationfile)) 等

3)为所有这些列和位置文件创建索引

4) 使用查询 选择 ID、名称、城市、位置文件、位置路径 从 桌子 在哪里 位置文件,例如 'euro%'

联合

选择 ID、名称、城市、位置文件、位置路径 从 桌子 在哪里 locationfile1 喜欢 'euro%'

联合

选择 ID、名称、城市、位置文件、位置路径 从 桌子 在哪里 locationfile2 like 'euro%'

...等

联合

选择 ID、名称、城市、位置文件、位置路径 从 桌子 在哪里 locationfileN like 'euro%'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多