如何使用多个 OR、AND、IN 语句优化查询? [英] How to optimize a query with multiple OR, AND, IN statements?

查看:67
本文介绍了如何使用多个 OR、AND、IN 语句优化查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SQL 查询,如下所示:

I have a an SQL query as below:

    Declare @ConnectionType int = 5, 
    @UserId int = 2

    select * from CallDetails 
    Where ((@ConnectionType = 0 AND CallDetails.DeviceType IN (0,1,2,3,4,5,7,8))
            OR (@ConnectionType = 1 AND CallDetails.DeviceType = 4)
            OR (@ConnectionType = 2 AND CallDetails.DeviceType IN (0,1,2,3,7))
            OR (@ConnectionType = 3 AND CallDetails.DeviceType = 5)
            OR (@ConnectionType = 4 AND CallDetails.DeviceType IN (0,1,2,3,4,7))
            OR (@ConnectionType = 5 AND CallDetails.DeviceType IN (4,5))
            OR (@ConnectionType = 6 AND CallDetails.DeviceType IN (0,1,2,3,5,7))
            OR (@ConnectionType = 7 AND CallDetails.DeviceType IN (8))
            OR (@ConnectionType = 8 AND CallDetails.DeviceType IN (0,1,2,3,7,8))
            OR (@ConnectionType = 9 AND CallDetails.DeviceType IN (5,8))
            OR (@ConnectionType = 10 AND CallDetails.DeviceType IN (4,8))
            OR (@ConnectionType = 11 AND CallDetails.DeviceType IN (0,1,2,3,4,8))
            OR (@ConnectionType = 12 AND CallDetails.DeviceType IN (0,1,2,3,5,8))
            OR (@ConnectionType = 13 AND CallDetails.DeviceType IN (4,5,8)) 
            OR (@ConnectionType = 14 AND CallDetails.DeviceType IN (0,1,2,3,7,4,5))
            OR @ConnectionType IS NULL)

查询的另一部分是:

            AND (@UserId IS NULL OR @ConnectionType IN (1,3,5,7,9,10,13)

            OR (@ConnectionType = 0 AND (CallDetails.DeviceType IN (4,5,8) OR (CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 2 AND ((CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 4 AND (CallDetails.DeviceType = 4 OR (CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 6 AND (CallDetails.DeviceType = 5 OR (CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 8 AND (CallDetails.DeviceType = 8 OR (CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 11 AND (CallDetails.DeviceType IN (4,8) OR (CallDetails.UserId = @UserId)))
            OR (@ConnectionType = 12 AND (CallDetails.DeviceType IN (5,8) OR (CallDetails.UserId = @UserId))) 
            OR (@ConnectionType = 14 AND (CallDetails.DeviceType IN (4,5) OR (CallDetails.UserId = @UserId)))
        )

@ConnectionType 是多个设备的组合,在此基础上决定DeviceType.一旦将来添加任何其他设备 @ConnectionType 组合将增加等等.此查询也用于多个 Store Procedures.如何优化此查询?

@ConnectionType is a combination of Multiple devices and at this basis DeviceType will be decided. Once in future any other device will be added @ConnectionType combinations will be increased and so on. This query is being used in multiple Store Procedures too. How can I optimize this query ?

推荐答案

正如我所提到的,看起来您最好使用查找表.您的表可能看起来如此简单(请注意,我没有添加索引、外键约束等,但您可能需要/需要它们):

As I mentioned, this looks like you would be better off with a lookup table. Your table could look as simple as this (note I add no indexes, foreign key constraints, etc, but you will likely want/need them):

CREATE TABLE dbo.ConnectionLookup (ConnectionType int,
                                   DeviceType int);

INSERT INTO dbo.ConnectionLookup (ConnectionType,
                                  DeviceType)
VALUES(0,0),
      (0,1),
      (0,2),
      (0,3),
      (0,4),
      (0,5),
      (0,7),
      (0,8),
      (1,4),
      ...
      (14,0),
      (14,1),
      (14,2),
      (14,3),
      (14,4),
      (14,5),
      (14,7);

然后,您可以在查找表上执行JOIN:

Then, instead, you can perform a JOIN on the lookup table:

DECLARE @ConnectionType int = 5;
        --@UserId int = 2; --Commented out, as never used.

SELECT {Your Columns}
FROM dbo.CallDetails CD
     JOIN dbo.ConnectionLookup CL On CD.DeviceType = CL.DeviceType
WHERE CL.ConnectionType = @ConnectionType;

然而,这有点猜测,因为缺乏样本数据和预期结果,但应该(尽管如此)让您走上正确的道路

This is, however, a bit of a guess, as there is a lack of sample data and expected results but should (none the less) get you on the right path

这篇关于如何使用多个 OR、AND、IN 语句优化查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆