Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:32:22+00:00 2026-06-11T15:32:22+00:00

Can anyone help me optimize this query? It takes 15 seconds. If I move

  • 0

Can anyone help me optimize this query? It takes 15 seconds. If I move the WHERE clauses up to the join itself, still the same result.

SELECT
`users`.`employeeID` as `username`,
`users`.`firstName` as `fname`,
`users`.`lastName` as `lname`
FROM `enrollment`
INNER JOIN `users` ON `enrollment`.`employeeID` = `users`.`employeeID`
WHERE `enrollment`.`number` = [int]
AND
`enrollment`.`term` = [int]
AND
(
`enrollment`.`status` = 'E'
OR
`enrollment`.`status` = 'M'
)
ORDER BY    
`users`.`lastName` ASC,
`users`.`firstName` ASC;

Takes as long as this:

SELECT
`users`.`employeeID` as `username`,
`users`.`firstName` as `fname`,
`users`.`lastName` as `lname`
FROM `enrollment`
INNER JOIN
`users` ON `enrollment`.`employeeID` = `users`.`employeeID`
AND
`enrollment`.`number` = [int]
AND
`enrollment`.`term` = [int]
AND
(
`enrollment`.`status` = 'E'
OR
`enrollment`.`status` = 'M'
)
ORDER BY    
`users`.`lastName` ASC,
`users`.`firstName` ASC;

This is the EXPLAIN results

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  users   ALL PRIMARY NULL    NULL    NULL    52925   Using filesort
1   SIMPLE  enrollment  ref employeeID,number_term  employeeID  9   ezlrn.users.employeeID  2   Using where
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:32:23+00:00Added an answer on June 11, 2026 at 3:32 pm

    To answer exhaustively we would need to check out the query plan, which you obtain issuing an EXPLAIN SELECT...(your query), and the index structure already in place.

    In the hypothesis that you have no indexes yet (except maybe the PRIMARY KEY ones), and that is the problem, let’s see how to whip up something (possibly further improveable):

    SELECT
        `users`.`employeeID` as `username`,
        `users`.`firstName` as `fname`,
        `users`.`lastName` as `lname`
    FROM `enrollment`
        INNER JOIN `users` ON `enrollment`.`employeeID` = `users`.`employeeID`
    WHERE `enrollment`.`number` = [int]
        AND
        `enrollment`.`term` = [int]
        AND
        (
            `enrollment`.`status` = 'E'
        OR
        `enrollment`.`status` = 'M'
        )
        ORDER BY    
            `users`.`lastName` ASC,
            `users`.`firstName` ASC;
    

    You have a JOIN clause beetween enrollment.employeeID and users.employeeID.

    So the first couple of indexes you need is right there:

    -- CREATE INDEX enrollment_ndx ON enrollment(employeeID); -- wait on this one
    CREATE INDEX users_ndx ON users(employeeID);
    

    Then the WHERE clause contains some more conditions that can be usefully indexed. So the final index for enrollment would be:

    CREATE INDEX enrollment_ndx ON enrollment(employeeID, number, term, status);
    

    Try creating the indexes on enrollment and users, and try again (posting the query plan).

    Note: you can also use the syntax enrollment.status IN ('M', 'E') in your query to simplify it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone one help me optimize this query SELECT `debit_side`.`account_code` CODE, GROUP_CONCAT(DISTINCT accounts.name) AS
can anyone can help me optimize this query and help me to tune my
Can anyone help me how to optimize this method? public override VirtualPathData GetVirtualPath(RequestContext requestContext,
Can anyone help me with this CAML query? When I flip the Ascending attribute
Can anyone help me rewrite this query without a distinct clause? select a.rec_type ,
Can anyone help - this is driving me mad. I am calling a mysql
Can anyone help or provide me with some suggestions for the below query. I
Can anyone help me figure this out? The below code works fine and gets
Can anyone help me to convert this to proper JavaScript? <script>var datePostForAll = &#39;<div
Can anyone help me on how i can achieve this style for android ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.