MySQL: need help making a query faster





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I wrote a query that finds me records in discovery table that contain a needle from whitelisted (LIKE %needle%) and don't have a record in logs table (and some other easy to understand filtering):



SELECT * FROM (
SELECT discovery.id, discovery.resource AS url, discovery.event_datetime, whitelisted.keyword
FROM discovery
INNER JOIN whitelisted
ON discovery.`resource` LIKE concat('%', whitelisted.`keyword`, '%')
LEFT JOIN `logs`
ON `logs`.discovery_id = discovery.id
WHERE
discovery.event_datetime >= NOW()
AND
discovery.provider = 'XXX'
AND
`logs`.id IS NULL
AND
discovery.resource NOT LIKE '%SOMETHING-TO-EXCLUDE%'
LIMIT 0, 20
) logless_resources
GROUP BY logless_resources.url
ORDER BY logless_resources.event_datetime ASC


discovery table: 143k rows




  • id UNSIGNED INT AI PRIMARY

  • resource VARCHAR(1024) INDEX

  • provider ENUM

  • event_datetime DATETIME

  • created_at DATETIME


whitelisted table: 535 rows (not thousands, just 0.5k)




  • id UNSIGNED INT AI PRIMARY

  • keyword VARCHAR(128) INDEX

  • provider ENUM

  • created_at DATETIME


logs: 585k rows




  • id UNSIGNED INT AI PRIMARY

  • discovery_id UNSIGNED INT FK to discovery.id


Right now this takes 90 seconds - can it be made faster?









share







New contributor




Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    0















    I wrote a query that finds me records in discovery table that contain a needle from whitelisted (LIKE %needle%) and don't have a record in logs table (and some other easy to understand filtering):



    SELECT * FROM (
    SELECT discovery.id, discovery.resource AS url, discovery.event_datetime, whitelisted.keyword
    FROM discovery
    INNER JOIN whitelisted
    ON discovery.`resource` LIKE concat('%', whitelisted.`keyword`, '%')
    LEFT JOIN `logs`
    ON `logs`.discovery_id = discovery.id
    WHERE
    discovery.event_datetime >= NOW()
    AND
    discovery.provider = 'XXX'
    AND
    `logs`.id IS NULL
    AND
    discovery.resource NOT LIKE '%SOMETHING-TO-EXCLUDE%'
    LIMIT 0, 20
    ) logless_resources
    GROUP BY logless_resources.url
    ORDER BY logless_resources.event_datetime ASC


    discovery table: 143k rows




    • id UNSIGNED INT AI PRIMARY

    • resource VARCHAR(1024) INDEX

    • provider ENUM

    • event_datetime DATETIME

    • created_at DATETIME


    whitelisted table: 535 rows (not thousands, just 0.5k)




    • id UNSIGNED INT AI PRIMARY

    • keyword VARCHAR(128) INDEX

    • provider ENUM

    • created_at DATETIME


    logs: 585k rows




    • id UNSIGNED INT AI PRIMARY

    • discovery_id UNSIGNED INT FK to discovery.id


    Right now this takes 90 seconds - can it be made faster?









    share







    New contributor




    Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I wrote a query that finds me records in discovery table that contain a needle from whitelisted (LIKE %needle%) and don't have a record in logs table (and some other easy to understand filtering):



      SELECT * FROM (
      SELECT discovery.id, discovery.resource AS url, discovery.event_datetime, whitelisted.keyword
      FROM discovery
      INNER JOIN whitelisted
      ON discovery.`resource` LIKE concat('%', whitelisted.`keyword`, '%')
      LEFT JOIN `logs`
      ON `logs`.discovery_id = discovery.id
      WHERE
      discovery.event_datetime >= NOW()
      AND
      discovery.provider = 'XXX'
      AND
      `logs`.id IS NULL
      AND
      discovery.resource NOT LIKE '%SOMETHING-TO-EXCLUDE%'
      LIMIT 0, 20
      ) logless_resources
      GROUP BY logless_resources.url
      ORDER BY logless_resources.event_datetime ASC


      discovery table: 143k rows




      • id UNSIGNED INT AI PRIMARY

      • resource VARCHAR(1024) INDEX

      • provider ENUM

      • event_datetime DATETIME

      • created_at DATETIME


      whitelisted table: 535 rows (not thousands, just 0.5k)




      • id UNSIGNED INT AI PRIMARY

      • keyword VARCHAR(128) INDEX

      • provider ENUM

      • created_at DATETIME


      logs: 585k rows




      • id UNSIGNED INT AI PRIMARY

      • discovery_id UNSIGNED INT FK to discovery.id


      Right now this takes 90 seconds - can it be made faster?









      share







      New contributor




      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I wrote a query that finds me records in discovery table that contain a needle from whitelisted (LIKE %needle%) and don't have a record in logs table (and some other easy to understand filtering):



      SELECT * FROM (
      SELECT discovery.id, discovery.resource AS url, discovery.event_datetime, whitelisted.keyword
      FROM discovery
      INNER JOIN whitelisted
      ON discovery.`resource` LIKE concat('%', whitelisted.`keyword`, '%')
      LEFT JOIN `logs`
      ON `logs`.discovery_id = discovery.id
      WHERE
      discovery.event_datetime >= NOW()
      AND
      discovery.provider = 'XXX'
      AND
      `logs`.id IS NULL
      AND
      discovery.resource NOT LIKE '%SOMETHING-TO-EXCLUDE%'
      LIMIT 0, 20
      ) logless_resources
      GROUP BY logless_resources.url
      ORDER BY logless_resources.event_datetime ASC


      discovery table: 143k rows




      • id UNSIGNED INT AI PRIMARY

      • resource VARCHAR(1024) INDEX

      • provider ENUM

      • event_datetime DATETIME

      • created_at DATETIME


      whitelisted table: 535 rows (not thousands, just 0.5k)




      • id UNSIGNED INT AI PRIMARY

      • keyword VARCHAR(128) INDEX

      • provider ENUM

      • created_at DATETIME


      logs: 585k rows




      • id UNSIGNED INT AI PRIMARY

      • discovery_id UNSIGNED INT FK to discovery.id


      Right now this takes 90 seconds - can it be made faster?







      mysql optimization mysql-5.7





      share







      New contributor




      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 mins ago









      Nino ŠkopacNino Škopac

      1012




      1012




      New contributor




      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Nino Škopac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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',
          autoActivateHeartbeat: false,
          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
          });


          }
          });






          Nino Škopac is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f234785%2fmysql-need-help-making-a-query-faster%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Nino Škopac is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Nino Škopac is a new contributor. Be nice, and check out our Code of Conduct.













          Nino Škopac is a new contributor. Be nice, and check out our Code of Conduct.












          Nino Škopac is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators 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.


          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%2fdba.stackexchange.com%2fquestions%2f234785%2fmysql-need-help-making-a-query-faster%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

          Ronny Ackermann

          Köttigit

          MySQL 8.0.15 starts normally but any connection hangs