热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

提高在where子句中使用isnull的性能-Improveperformancethatusesisnullinawhereclause

Thereisareportwhosequeryisusingisnullinthewhereclauseanditscausingsomeperformance

There is a report whose query is using isnull in the where clause and it's causing some performance issues. Here is a very simplified example. http://www.sqlfiddle.com/#!6/79759/11

有一个报告,其查询在where子句中使用isnull,这会导致一些性能问题。这是一个非常简单的例子。 http://www.sqlfiddle.com/#!6/79759/11

The query is using a multi-valued parameter, usually just returning one or two values. (I've tried to replicate the SSRS multi-value param using a table variable in sqlfiddle)

查询使用多值参数,通常只返回一个或两个值。 (我试图使用sqlfiddle中的表变量复制SSRS多值参数)

select 
  isnull(descrL,descr) as division,
  product
from Upper
left join Lower on product = productL
where isnull(DescrL,Descr) in (@params)

There is an Upper division that has all the products. Some of the products exist on a child division. If it exists in the lower division, that is the division that should be shown.

有一个上级部门,拥有所有产品。一些产品存在于儿童部门。如果它存在于较低的分区中,那就是应该显示的分区。

The company parameter can accept the upper division, lower division, or both.

公司参数可以接受上级,下级或两者。

Any ideas on how the query can be changed for better performance?

有关如何更改查询以获得更好性能的任何想法?

2 个解决方案

#1


2  

select  descrL as division, product
from Lower 
where DescrL like @params + '%'
union
Select descr, product
from Upper
where Descr like @params = '%'

This would be very performant. If the match is found in the top select, it should be suppressed in the bottom select since it's a union instead of a union all. Notice the like instead of in. If you do an in there is just no way to use an index on DescrL or Descr to find the results. If you do a like, indexes are used fine. You may have to adjust your application logic to make that work.

这将是非常高效的。如果在顶部选择中找到匹配,则应在底部选择中对其进行抑制,因为它是联合而不是联合全部。请注意,而不是in。如果你执行in,则无法使用DescrL或Descr上的索引来查找结果。如果你这样做,索引就可以了。您可能必须调整应用程序逻辑才能使其正常工作。

If you can't do that, then there is this solution. You'll have to add fn_split to your db. http://msdn.microsoft.com/en-us/library/aa496058(v=sql.80).aspx

如果你不能那样做,那就有这个解决方案。您必须将fn_split添加到您的数据库。 http://msdn.microsoft.com/en-us/library/aa496058(v=sql.80).aspx

    select  descrL as division, product
    from Lower l 
    join dbo.fn_Split(@params, ',') p1
        on l.DescrL = p1.value
    union
    Select descr, product
    from Upper u 
    join dbo.fn_Split(@params, ',') p2
        on u.Descr = p2.value

If you really want to eke out the very last bit of performance you can declare a table variable and only run the fn_split once in order to populate the table variable, and then make the two joins be to the table variable. So here you are also taking advantage of the index on the columns, which is the main thing you need to make the query faster. Always do 'include actual execution plan' in sql server when you run it, and look at the results and make sure you're seeing index seeks instead of table scans.

如果你真的想要完成性能的最后一点,你可以声明一个表变量,只运行fn_split一次以填充表变量,然后使两个连接成为表变量。所以在这里你也可以利用列上的索引,这是使查询更快的主要内容。在运行它时,始终在sql server中“包含实际执行计划”,并查看结果并确保您看到索引搜索而不是表扫描。

EDIT: I went over to your sqlfiddle link. Didn't see it earlier. This works. The union by itself won't suppress dupes, since you are also selecting the division, sorry. So you have to use a not in, or as I prefer, a left outer where null.

编辑:我去了你的sqlfiddle链接。没有早点看到它。这很有效。工会本身不会压制愚蠢,因为你也选择了部门,对不起。因此,您必须使用not in或者我更喜欢左侧外部为null。

select descrL as division, productL as product
    from Lower l 
    join @params p1
        on l.DescrL = p1.division
    union
    Select u.descr, u.product
    from Upper u 
    join @params p2
        on u.Descr = p2.division
    left join (select productL as product
                from Lower l2 
               join @params p3
                 on l2.DescrL = p3.division) sub1
     on u.product = sub1.product
        where sub1.product is null
order by 2

#2


1  

I'm not sure, but would a cte work in your situation?

我不确定,但在你的情况下是否会有效?

;with BothDivs as
(
  select 
  isnull(descrL,descr) as division,
  product
  from Upper
  left join Lower on product = productL
)
select division, product
from BothDivs
where division in (@params)

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 在Ubuntu 16.04 LTS上配置Qt Creator开发环境
    本文详细介绍了如何在Ubuntu 16.04 LTS系统中安装和配置Qt Creator,涵盖了从下载到安装的全过程,并提供了常见问题的解决方案。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 本文介绍如何通过创建替代插入触发器,使对视图的插入操作能够正确更新相关的基本表。涉及的表包括:飞机(Aircraft)、员工(Employee)和认证(Certification)。 ... [详细]
author-avatar
铁合金在线
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有