Query to find instances of two types of events occurring more than one second apart











up vote
2
down vote

favorite












I have 3 tables on my database: BusinessObject and CustomEvent, and EventType. BusinessObject having a lot of columns, CustomEvent having the following:



Id
EventTypeId
DateEvent
CommentEvent
UserId
BusinessObjectId


I want to get all the BusinessObject having two CustomEvent with EventTypeId in (10, 11) such has the difference between the two DateEvent are more than 1 second. From the 6 first month of this year.



I came up with the following query but it's quite slow (~40 seconds). I assume there's a (lot of) ways to speed this up, but my SQL level is not good enough.



Here's the query:



select bo.Id
from BusinessObject bo
where
(
select top 1 DATEDIFF(second,
(select top 1 DateEvent from CustomEvent where EventTypeId = 11 and BusinessObjectid = bo.BusinessObjectId),
(select top 1 DateEvent from CustomEvent where EventTypeId = 10 and BusinessObjectid = bo.BusinessObjectId))
from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId
where EventTypeId = 11
and innerBO.CreationDate < '20180701'
and innerBO.CreationDate > '20180101'
and innerBO.BusinessObjectId = bo.BusinessObjectId
) > 1


FYI, the query returns 4.138 rows. Here's the tables volumes:



BusinessObject => 302k lines



CustomEvent => 4.326k lines



How could I speed this up?










share|improve this question




























    up vote
    2
    down vote

    favorite












    I have 3 tables on my database: BusinessObject and CustomEvent, and EventType. BusinessObject having a lot of columns, CustomEvent having the following:



    Id
    EventTypeId
    DateEvent
    CommentEvent
    UserId
    BusinessObjectId


    I want to get all the BusinessObject having two CustomEvent with EventTypeId in (10, 11) such has the difference between the two DateEvent are more than 1 second. From the 6 first month of this year.



    I came up with the following query but it's quite slow (~40 seconds). I assume there's a (lot of) ways to speed this up, but my SQL level is not good enough.



    Here's the query:



    select bo.Id
    from BusinessObject bo
    where
    (
    select top 1 DATEDIFF(second,
    (select top 1 DateEvent from CustomEvent where EventTypeId = 11 and BusinessObjectid = bo.BusinessObjectId),
    (select top 1 DateEvent from CustomEvent where EventTypeId = 10 and BusinessObjectid = bo.BusinessObjectId))
    from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId
    where EventTypeId = 11
    and innerBO.CreationDate < '20180701'
    and innerBO.CreationDate > '20180101'
    and innerBO.BusinessObjectId = bo.BusinessObjectId
    ) > 1


    FYI, the query returns 4.138 rows. Here's the tables volumes:



    BusinessObject => 302k lines



    CustomEvent => 4.326k lines



    How could I speed this up?










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have 3 tables on my database: BusinessObject and CustomEvent, and EventType. BusinessObject having a lot of columns, CustomEvent having the following:



      Id
      EventTypeId
      DateEvent
      CommentEvent
      UserId
      BusinessObjectId


      I want to get all the BusinessObject having two CustomEvent with EventTypeId in (10, 11) such has the difference between the two DateEvent are more than 1 second. From the 6 first month of this year.



      I came up with the following query but it's quite slow (~40 seconds). I assume there's a (lot of) ways to speed this up, but my SQL level is not good enough.



      Here's the query:



      select bo.Id
      from BusinessObject bo
      where
      (
      select top 1 DATEDIFF(second,
      (select top 1 DateEvent from CustomEvent where EventTypeId = 11 and BusinessObjectid = bo.BusinessObjectId),
      (select top 1 DateEvent from CustomEvent where EventTypeId = 10 and BusinessObjectid = bo.BusinessObjectId))
      from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId
      where EventTypeId = 11
      and innerBO.CreationDate < '20180701'
      and innerBO.CreationDate > '20180101'
      and innerBO.BusinessObjectId = bo.BusinessObjectId
      ) > 1


      FYI, the query returns 4.138 rows. Here's the tables volumes:



      BusinessObject => 302k lines



      CustomEvent => 4.326k lines



      How could I speed this up?










      share|improve this question















      I have 3 tables on my database: BusinessObject and CustomEvent, and EventType. BusinessObject having a lot of columns, CustomEvent having the following:



      Id
      EventTypeId
      DateEvent
      CommentEvent
      UserId
      BusinessObjectId


      I want to get all the BusinessObject having two CustomEvent with EventTypeId in (10, 11) such has the difference between the two DateEvent are more than 1 second. From the 6 first month of this year.



      I came up with the following query but it's quite slow (~40 seconds). I assume there's a (lot of) ways to speed this up, but my SQL level is not good enough.



      Here's the query:



      select bo.Id
      from BusinessObject bo
      where
      (
      select top 1 DATEDIFF(second,
      (select top 1 DateEvent from CustomEvent where EventTypeId = 11 and BusinessObjectid = bo.BusinessObjectId),
      (select top 1 DateEvent from CustomEvent where EventTypeId = 10 and BusinessObjectid = bo.BusinessObjectId))
      from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId
      where EventTypeId = 11
      and innerBO.CreationDate < '20180701'
      and innerBO.CreationDate > '20180101'
      and innerBO.BusinessObjectId = bo.BusinessObjectId
      ) > 1


      FYI, the query returns 4.138 rows. Here's the tables volumes:



      BusinessObject => 302k lines



      CustomEvent => 4.326k lines



      How could I speed this up?







      performance sql sql-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 20 at 14:57









      200_success

      128k15149412




      128k15149412










      asked Jul 20 at 13:03









      Thomas Ayoub

      408313




      408313






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You need to work on below statement of your code. You are INNER joining two tables but not with both tables reference.



          from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId


          Please mention your Query-result requirement, so according to that we can help with code sample.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199914%2fquery-to-find-instances-of-two-types-of-events-occurring-more-than-one-second-ap%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You need to work on below statement of your code. You are INNER joining two tables but not with both tables reference.



            from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId


            Please mention your Query-result requirement, so according to that we can help with code sample.






            share|improve this answer

























              up vote
              0
              down vote













              You need to work on below statement of your code. You are INNER joining two tables but not with both tables reference.



              from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId


              Please mention your Query-result requirement, so according to that we can help with code sample.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You need to work on below statement of your code. You are INNER joining two tables but not with both tables reference.



                from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId


                Please mention your Query-result requirement, so according to that we can help with code sample.






                share|improve this answer












                You need to work on below statement of your code. You are INNER joining two tables but not with both tables reference.



                from CustomEvent ce inner join BusinessObject innerBO on ce.BusinessObjectId = bo.BusinessObjectId


                Please mention your Query-result requirement, so according to that we can help with code sample.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 10 at 3:28









                JERRY

                1305




                1305






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199914%2fquery-to-find-instances-of-two-types-of-events-occurring-more-than-one-second-ap%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Сан-Квентин

                    Алькесар

                    Josef Freinademetz