简体   繁体   English

我怎样才能简化这个查询不杀死 SQL 服务器

[英]How can I simplify this query to not kill SQL server

So I was tasked to look up several tickets in the database that hit certain criteria.因此,我的任务是在数据库中查找符合特定条件的几张票。 But I think I am at the limitations of what SQL can handle the below has an issue because all tickets that are belonging to company '1646' are like over 12k tickets for that customer alone...但我认为我处于 SQL 可以处理以下问题的局限性,因为属于公司“1646”的所有门票就像仅该客户的 12k 多张门票......

So at first I thought about an inner join statement but then I started to get a headache as things kept looping around itself.所以一开始我想到了一个内部连接语句,但后来我开始头疼,因为事情一直围绕着自己循环。 Below is the script that attempts to run but then my database starts to smoke.. and suffers read/write issues and the lag becomes very real and it is very clear to see it is from me pulling this query, The culprit:下面是尝试运行但随后我的数据库开始冒烟的脚本..并遇到读/写问题并且延迟变得非常真实并且很清楚地看到它是我拉这个查询的原因,罪魁祸首:

     SELECT 
        s.SR_Service_RecID as 'Ticket #',
        t.Description as 'Description',
        s.Summary as 'Summary Description',
        d.Date_Created as 'Entered',
        b.Board_Name as 'Board',
        c.Company_ID as 'Company',
        q.Description as 'Status',
        p.SR_Severity_Name as 'Priority',
        a.Description as 'Type',
        z.Description as 'SubType',
        s.Date_Closed as 'Closed'
    d.SR_Detail_Notes_Markdown as 'Notes',
    From 
        dbo.SR_Service as s,
        dbo.SR_Type as t,
        dbo.SR_Detail as d,
        dbo.SR_Board as b,
        dbo.Company as c,
        dbo.SR_Status as q,
        dbo.SR_Severity as p,
        dbo.SR_Type as a,
        dbo.SR_Subtype as z
    WHERE 
    s.Company_RecID like '1646'
        and s.Entered_By not like 'zadmin'
        and s.Updated_By not like 'zadmin'
        and s.Entered_By not like 'RESTAPI'
        and s.Updated_By not like 'RESTAPI'
        and s.Entered_By not like 'techautomate'
        and s.Updated_By not like 'techautomate'
        and s.Entered_By not like 'template%'
        and s.Updated_By not like 'template%'
        and s.Entered_By not like 'HelpDesk'
        and s.Updated_By not like 'HelpDesk'
        and s.Entered_By not like 'Email Connector'
        and s.Updated_By not like 'Email Connector'
        and d.SR_Detail_Notes_Markdown not like '%Assigned%'
    ORDER BY 
        s.Date_Entered ASC;`

So if anyone has the time or if someone can help me refine this into a better query or help me figure out the figure 8 of inner joins to make this work, or perhaps there is a SQL chad out here that can easily write this into a SQL script I would be most grateful for the help in figuring this out.因此,如果有人有时间或者有人可以帮助我将其细化为更好的查询,或者帮助我找出内部联接的图 8 来完成这项工作,或者这里可能有一个 SQL 可以轻松地将其写入SQL 脚本 如果能帮助我解决这个问题,我将不胜感激。 Whatever the outcome thank you in advance for the help!!无论结果如何,提前感谢您的帮助!!

--EDIT-- Allow me to add a little more detail...Sorry for the confusion guys, I do appreciate the help --EDIT-- 请允许我添加更多细节...抱歉让大家感到困惑,我非常感谢您的帮助

So I am going to add the Friendly names first then the primary key and foreign key relations所以我要先添加友好名称,然后添加主键和外键关系

ticket number = Service_RecID is the primary key in dbo.Service foriegn key in dbo.Detail ticket number = Service_RecID 是 dbo.Service 中的主键。dbo.Detail 中的外键

ticket type = SR_Type_RecID is the primary key in dbo.Type foreign key in dbo.Service -> but I need the description of that id which is in dbo.type ticket type = SR_Type_RecID 是 dbo.Type 中的主键。在 dbo.Service 中键入外键 -> 但我需要 dbo.type 中的那个 id 的描述

Summary Description = is located in dbo.SR_Service the column header is Summary Summary Description = 位于 dbo.SR_Service 列 header 是 Summary

Entered (is the date the ticket was entered) = located in dbo.SR_Detail under the column header Date_created Entered(输入工单的日期)= 位于 dbo.SR_Detail 中 header Date_created 列下

Board (is the service board ticket is assigned to) = SR_Board_RecID primary key in dbo.SR_Board foreign key in SR_Service -> but I need the column header Board_Name located in dbo.SR_Board Board(是分配给服务板的票)= SR_Board_RecID dbo.SR_Board 中的主键 SR_Service 中的外键 -> 但我需要位于 dbo.SR_Board 中的列 header Board_Name

Company = Company_RecID primary key in dbo.Config but the foreign key in dbo.SR_Service Company = Company_RecID dbo.Config 中的主键但 dbo.SR_Service 中的外键

Status = SR_Status_RecID primary key dbo.SR_Status foriegn key is located in dbo.SR_Service Status = SR_Status_RecID 主键 dbo.SR_Status 外键位于 dbo.SR_Service

Priority = SR_Severity_RecID primary key for dbo.SR_Severity foriegn key is located in dbo.SR_Service -> but I need SR_Severity_Name which is the column header in dbo.SR_Severity Priority = SR_Severity_RecID dbo.SR_Severity 外键的主键位于 dbo.SR_Service -> 但我需要 SR_Severity_Name,它是 dbo.SR_Severity 中的列 header

type = SR_Type_RecID primary key in dbo.Type shares foreign key with dbo.Service -> but I need the description associated with SR_Type_RecID located in dbo.SR_Type type = SR_Type_RecID dbo.Type 中的主键与 dbo.Service 共享外键 -> 但我需要与位于 dbo.SR_Type 中的 SR_Type_RecID 关联的描述

subtype = SR_SubType_RecID primary key located in dbo.SR_subtype and the foreign key that is shared is under dbo.SR_service -> but again I need the description that is under the dbo.SR_SubType subtype = SR_SubType_RecID 主键位于 dbo.SR_subtype 中,共享的外键位于 dbo.SR_service 下 -> 但我再次需要 dbo.SR_SubType 下的描述

closed = as it is located in the dbo.SR_Service under column header date_closed closed = 因为它位于 dbo.SR_Service 列 header date_closed 下

notes = on the other hand is located in dbo.Detail under column header SR_Detail_Notes_Markdown and the only keys it shares across the database is a foreign key SR_Service_Rec_ID notes = 另一方面位于 dbo.Detail 列 header SR_Detail_Notes_Markdown 中,它在数据库中共享的唯一键是外键 SR_Service_Rec_ID

I hope that this helps to clarify the parabola of terror I am playing in:)我希望这有助于澄清我正在玩的恐怖抛物线:)

---EDIT 2--- ---编辑2---

I cant seem to get the INNER JOIN statements to work properly我似乎无法让 INNER JOIN 语句正常工作

    INNER JOIN dbo.SR_Service.Service_RecID on dbo.SR_Detail.Service_RecID
    INNER JOIN dbo.Type.SR_Type_RecID on dbo.SR_Service.SR_Type_RecID
    INNER JOIN dbo.Type.Description on dbo.SR_Service.Type_Description
    INNER JOIN dbo.SR_Board.SR_Board_RecID on dbo.Service.SR_BoardRecID
    INNER JOIN dbo.Config.Company_RecID on dbo.Service.Company_RecID
    INNER JOIN dbo.SR_Status.SR_Status_RecID on dbo.Service.SR_Status_RecID
    INNER JOIN dbo.SR_SubType.SR_Type_RecID on dbo.Type.SR_Type_RecID

I think it is how I am declaring my FROM Statement...我认为这就是我声明 FROM 语句的方式......

Can I not just run singular database queries and have the results dump and append to a new database object?我可以不只运行单一数据库查询并将结果转储和 append 到新数据库 object 吗?

The JOIN condition is how one table is related to another based on a common column. JOIN 条件是一个表如何基于公共列与另一个表相关联。 Aside from your specific query, think of orders.除了您的特定查询之外,请考虑订单。

An order is made by a customer.订单由客户下达。 An order has details, order detail products are from a products table.订单有详细信息,订单详细信息产品来自产品表。

Having said that, you might want something like话虽如此,您可能想要类似的东西

select
        c.customername,
        o.orderdate,
        od.qty,
        p.productname
    from
        customer c
            join orders o
                on c.customerid = o.customerid
                join orderDetails od
                    on o.orderid = od.orderid
                    join products p
                        on od.productid = p.productid

FEEDBACK FROM EDITS you provided in your original question您在原始问题中提供的编辑反馈

Now that you have provided respective primary key and foreign keys, we can help a little further.现在您已经提供了各自的主键和外键,我们可以进一步提供帮助。 First, dont try to name your columns with spaces or special characters like 'Ticket #' .首先,不要尝试使用空格或特殊字符(如'Ticket #'来命名您的列。 It is typically easier to name them readable in 'CamelCaseWhereUpperPerWord' is just an example.'CamelCaseWhereUpperPerWord'中将它们命名为可读通常更容易,这只是一个示例。 The output via whatever method should worry about the formatting of such header columns in output. output 通过任何方法都应该担心 output 中此类 header 列的格式。

Next, when applying filters, such as your company number, if its a number, dont put it in quotes, leave it as a number for equality testing.接下来,在应用过滤器时,例如您的公司编号,如果它是数字,请不要将其放在引号中,将其保留为数字以进行相等性测试。 In this case, you are looking for tickets for a single company = 1646. I would ensure an index on the service table including that column as an index in its first position.在这种情况下,您正在寻找一家公司的门票 = 1646。我将确保服务表上的索引包括该列作为其第一个 position 中的索引。

From your edited PK/FK, I have revised the query as below with embedded comments根据您编辑的 PK/FK,我修改了如下带有嵌入式评论的查询

SELECT 
        -- per ticket found
        s.SR_Service_RecID TicketNumber,
        -- dont worry about renaming Summary AS SummaryDescription,
        -- let the OUTPUT process add whatever column headers is more common approach
        -- just get the column and get it to work first, then fine-tune it
        s.Summary,
        s.Date_Closed Closed,

        -- dont worry for now about specific column order, I am trying to match
        -- the hierarchy of data trying to acquire.  You want for a specific company,
        -- so I am just putting that up front for now.
        c.Company_ID Company,

        -- description from the TYPE table
        t.Description type,

        -- pull from sub-type of the type
        st.Description SubType,

        -- now I can pull columns from the SR_Detail table
        d.Date_Created Entered,
        d.SR_Detail_Notes_Markdown Notes,

        -- now any columns from the SR_Board table
        b.Board_Name Board,

        -- columns from the status table
        q.Description Status,

        p.SR_Severity_Name Priority
    From 
        -- or is this table SUPPOSED to be Service vs SR_Service
        SR_Service as s
            -- first, joining to the config table so you can get the descriptive company "name", such as your "Company_ID"
            JOIN Config as c
                on s.Company_RecID = c.Company_RecID

            -- now join the outer hierarchy SR_Service to the SR_Type on its PK/FK relationship
            join SR_Type as t
                on s.SR_Type_RecID = t.SR_Type_RecID

            -- changing the subtype to a LEFT-JOIN in case a sub-type does not exist
            LEFT JOIN SR_Subtype as st
                on s.SR_SubType_RecID = st.SR_SubType_RecID

            -- now join the outer hierarchy SR_Service to the SR_Detail on its PK/FK relationship
            -- or is this table SUPPOSED to be Detail vs SR_Detail
            JOIN SR_Detail as d
                on s.Service_RecID = d.Service_RecID

            -- doing a LEFT-JOIN since an entry may NOT be assigned to a board (yet).
            -- otherwise the ticket will be excluded from final list if no such board assignment
            LEFT JOIN SR_Board as b
                on s.SR_Board_RecID = b.SR_Board_RecID

            -- etc., similar joins to each other lookup table for clear descriptions based on given PK/FK relationship
            JOIN SR_Status as q
                on s.SR_Status_RecID = q.SR_Status_RecID

            JOIN SR_Severity as p
                on s.SR_Severity_RecID = p.SR_Severity_RecID

    WHERE 
        -- NOW, you can apply your filtering conditions here as you have them
            s.Company_RecID = 1646
        and s.Entered_By not like 'zadmin'
        and s.Updated_By not like 'zadmin'
        and s.Entered_By not like 'RESTAPI'
        and s.Updated_By not like 'RESTAPI'
        and s.Entered_By not like 'techautomate'
        and s.Updated_By not like 'techautomate'
        and s.Entered_By not like 'template%'
        and s.Updated_By not like 'template%'
        and s.Entered_By not like 'HelpDesk'
        and s.Updated_By not like 'HelpDesk'
        and s.Entered_By not like 'Email Connector'
        and s.Updated_By not like 'Email Connector'
        and d.SR_Detail_Notes_Markdown not like '%Assigned%'
    ORDER BY 
        s.Date_Entered ASC;

Now that you have a full query, in the future, take one piece at a time.现在你有一个完整的查询,将来,一次一个。 Look at what you wanted.看看你想要什么。 All tickets associated with one customer.与一位客户关联的所有工单。 Query just that.查询就是这样。 As you have a table (or alias), get all the columns you expect from that one before moving on.因为您有一个表(或别名),所以在继续之前获取您期望从该表中获得的所有列。 Ex:前任:

select
        s.SR_Service_RecID TicketNumber,
        s.Summary,
        s.Date_Closed Closed
    from
        SR_Service s
    where
        s.Company_RecID = 1646
    

Great, this gets you the immediate result of the tickets.太好了,这可以让您立即获得门票的结果。 But now you want more stuff.但现在你想要更多的东西。 So, one table at a time, do a join.所以,一次一个表,做一个连接。 How does the second table relate to the first.第二个表如何与第一个相关。 I always try to list my primary (in this case your service table) on the left (or first position) JOINING a second table (right-side) based on what FK/PK relationship.我总是尝试根据 FK/PK 关系在左侧(或第一个位置)列出我的主要(在本例中为您的服务表)加入第二个表(右侧)。 In this case, you want the in-the-clear company name from the Config table.在这种情况下,您需要 Config 表中明确的公司名称。 So, get that extra column based on the join itself.因此,根据连接本身获取额外的列。

select
        s.SR_Service_RecID TicketNumber,
        s.Summary,
        s.Date_Closed Closed,
        -- and now columns from next table
        c.Company_ID Company
    from
        SR_Service s
            JOIN Config as c
                on s.Company_RecID = c.Company_RecID
    where
        s.Company_RecID = 1646

Continue adding one table at a time and one WHERE clause at a time until the entire query is done.继续一次添加一个表和一个 WHERE 子句,直到完成整个查询。 In SQL-Server, by doing a在 SQL-Server,通过做一个

SELECT TOP 10 (rest of query)

will result the list to only 10 records.将导致列表只有 10 条记录。 This way, you can sample "Does this query work as written?"通过这种方式,您可以对“此查询是否按书面方式工作?”进行采样。 Do you get an answer?你得到答案了吗? Have you botched the command already?你已经搞砸了命令吗? Fix it early one piece at a time.尽早修复它,一次修复它。 Column between each column retrieved.检索到的每一列之间的列。 Do I have the proper JOIN condition between the two tables?两个表之间是否有正确的 JOIN 条件? then move on.然后继续。

Hopefully this detailed example of your database scenario will help you learn to write queries easier.希望您的数据库场景的这个详细示例将帮助您学习更轻松地编写查询。 Also see how to better list your table structures (ALL RELEVANT COLUMNS) to help get better responses vs us trying to guess at column names in tables.另请参阅如何更好地列出您的表结构(所有相关列)以帮助获得更好的响应,而不是我们试图猜测表中的列名。

As Martin Smith says, you need to define the table joins.正如 Martin Smith 所说,您需要定义表连接。 As the query stands now, any results would be useless.就目前的查询而言,任何结果都是无用的。

To do this, you must first understand the database schema.为此,您必须首先了解数据库模式。 More specificially the relationship between the entities (= tables).更具体地说,实体(=表)之间的关系。 For this you need to know and understand the business domain and how it is represented in the tables.为此,您需要知道并理解业务领域及其在表中的表示方式。 The sql server experts from this forum will not be able to do this for you without the knowledge of the business domain.本论坛的sql服务器专家在不了解业务领域的情况下将无法为您完成此操作。

After you have achieved the above you can deduce the correct joins from the database schema and improve your query draft accordingly.完成上述操作后,您可以从数据库模式中推断出正确的连接并相应地改进您的查询草稿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM