Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're query is... big. Can you explain what it acomplishes for you? It looks like it pulls each visitor ID and whether or not they are an operator where they are not an operator and they have a specific profile setup. That doesn't make a ton of sense, so I must be missing something there.</p> <p>Here's my attempt, based on my understanding of what you're trying to do:</p> <pre><code>select distinct visitor.ID, IF(operator.id IS NOT NULL, 1, 0) AS clazz from Visitor left outer join Operator on visitor.ID = operator.id where not exists (select 'x' from Operator OperatorTwo where OperatorTwo.id = visitor.ID) and exists (select 'x' from VisitorProfileField, ProfileField where VisitorProfileField.profileFieldID = ProfileField.ID and VisitorProfileField.profileFieldID.visitorID = visitor.ID and VisitorProfileField.profileFieldID.numberVal = 1 and VisitorProfileField.profileFieldID.stringVal = 'Manual' and ProfileField .name = 'subscription86') </code></pre> <p>The joined table named "operator1_1_" doesn't appear to be used, you should be able to remove that. If you are using it just to make sure there is a record for the visitor in that table, I'd use an exists instead of a join. I dropped that.</p> <p>I've switched your not in to a not exists, which I think may be easier for MySQL to optimize. I used an IF instead of a case because you only have two, and it was shorter to type. I don't know if either one is faster/easier on MySQL.</p> <p>I can tell you that in my experience MySQL performance dies with subqueries in suqueries. It seems to give up optimising them and starts running them row by row. I bet that if you used a temporary table of results (just for testing purposes) you would find your query runs much faster.</p> <p><strong>Edit:</strong></p> <p>Bill went further than I did, I didn't go far enough. I like Bill's query and agree with his conclusions about the CASE statement, which was kind of throwing me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload