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 8685057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:28:13+00:00 2026-06-12T22:28:13+00:00

The query below takes about 0.3 seconds. Why? And how can I solve it.

  • 0

The query below takes about 0.3 seconds. Why? And how can I solve it. The shouldnt take so long…

SELECT tshops.OfficialName
 , tresults.StartTime
 , tresults.Price
 , tresults.shopID
FROM
 tresults
INNER JOIN tshops
ON tshops.shopID = tresults.shopID
WHERE
  tresults.pID = 15
  AND tresults.websiteID = 1
  AND tresults.StartTime = (SELECT max(tresults.StartTime) AS maxstarttime
                        FROM
                          tresults
                        WHERE
                          tshops.shopID = tresults.shopID
                          AND tresults.pID = 15
                          AND tresults.websiteID = 1)
ORDER BY
  tresults.Price DESC

Explain result:

1, PRIMARY, tshops, ALL, PRIMARY, , , , 203, Using temporary; Using filesort
1, PRIMARY, tresults, eq_ref, PRIMARY,idxPID,idxWebsite,idxStartTimeASC,idxStartTimeDESC,fk_shopID, PRIMARY, 20, func,const,pricegrabber.tshops.shopID,const, 1, Using where
2, DEPENDENT SUBQUERY, tresults, ref, idxPID,idxWebsite,fk_shopID, fk_shopID, 4, pricegrabber.tshops.shopID, 891, Using where; Using index

So a full table scan on tshops is done. But why? There’s an index on shopID in both tables…

  • 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-12T22:28:16+00:00Added an answer on June 12, 2026 at 10:28 pm

    I would suspect the subselect which refers to the outer select is confusing matters.

    Try doing a join against the subselect, using a GROUP BY on the shopID. Normally I would expect MySQL to optimise things to do this anyway, but worth a try.

    Something like this (please excuse any typos)

    SELECT tshops.OfficialName
     , tresults.StartTime
     , tresults.Price
     , tresults.shopID
    FROM  tresults
    INNER JOIN tshops
    ON tshops.shopID = tresults.shopID
    INNER JOIN (SELECT shopID, MAX(tresults.StartTime) AS MaxStartTime 
                FROM tresults 
                WHERE tresults.pID = 15 
                AND tresults.websiteID = 1 
                GROUP BY shopID) Sub1
    ON tresults.StartTime = Sub1.MaxStartTime
    WHERE
      tresults.pID = 15
      AND tresults.websiteID = 1
    ORDER BY
      tresults.Price DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the query below. Using a LEFT OUTER join takes 9 seconds to
Below is the result of a query that takes almost 8 seconds and there
I have an SQL query (below) that essentially takes a student from tbStudents, and
I am trying to speed up a long running query that I have (takes
The query below returns rows that have both loginid and ip2 in the bumps
If the query below returns a 'template' id for any template assigned to a
On the query below I keep getting this error: Cannot read the next data
I have a query below that I use to retrieve the records not updated
My native query below is working fine oracle sqlplus. But through JPA native query,
Why does the MySQL query below give error 1066 (Not unique table/alias: 'customer') ?

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.