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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:37:31+00:00 2026-06-10T15:37:31+00:00

I have these tables and queries as defined in sqlfiddle . First my problem

  • 0

I have these tables and queries as defined in sqlfiddle.

First my problem was to group people showing LEFT JOINed visits rows with the newest year. That I solved using subquery.

Now my problem is that that subquery is not using INDEX defined on visits table. That is causing my query to run nearly indefinitely on tables with approx 15000 rows each.

Here’s the query. The goal is to list every person once with his newest (by year) record in visits table.

Unfortunately on large tables it gets real sloooow because it’s not using INDEX in subquery.

SELECT *
FROM people
LEFT JOIN (
  SELECT *
  FROM visits
  ORDER BY visits.year DESC
) AS visits
ON people.id = visits.id_people
GROUP BY people.id

Does anyone know how to force MySQL to use INDEX already defined on visits table?

  • 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-10T15:37:33+00:00Added an answer on June 10, 2026 at 3:37 pm

    Your query:

    SELECT *
    FROM people
    LEFT JOIN (
      SELECT *
      FROM visits
      ORDER BY visits.year DESC
    ) AS visits
    ON people.id = visits.id_people
    GROUP BY people.id;
    
    • First, is using non-standard SQL syntax (items appear in the SELECT list that are not part of the GROUP BY clause, are not aggregate functions and do not sepend on the grouping items). This can give indeterminate (semi-random) results.

    • Second, ( to avoid the indeterminate results) you have added an ORDER BY inside a subquery which (non-standard or not) is not documented anywhere in MySQL documentation that it should work as expected. So, it may be working now but it may not work in the not so distant future, when you upgrade to MySQL version X (where the optimizer will be clever enough to understand that ORDER BY inside a derived table is redundant and can be eliminated).

    Try using this query:

    SELECT 
        p.*, v.*
    FROM 
        people AS p
      LEFT JOIN 
            ( SELECT 
                  id_people
                , MAX(year) AS year
              FROM
                  visits
              GROUP BY
                  id_people
             ) AS vm
          JOIN
              visits AS v
            ON  v.id_people = vm.id_people
            AND v.year = vm.year 
        ON  v.id_people = p.id;
    

    The: SQL-fiddle

    A compound index on (id_people, year) would help efficiency.


    A different approach. It works fine if you limit the persons to a sensible limit (say 30) first and then join to the visits table:

    SELECT 
        p.*, v.*
    FROM 
        ( SELECT *
          FROM people
          ORDER BY name
            LIMIT 30
        ) AS p
      LEFT JOIN 
        visits AS v
          ON  v.id_people = p.id
          AND v.year =
        ( SELECT 
              year
          FROM
              visits
          WHERE
              id_people = p.id
          ORDER BY
              year DESC
            LIMIT 1
         )  
    ORDER BY name ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these tables: comments uid id pid pages pid user users id rank
I have these tables places(place_id, place_name) places_criteria(place_id, criterion_id) criteria(criterion_id, criterion_name) places_criteria have foreign keys
I have these tables: Idea and poll (and others) which are types of what
I have these tables: table_a user_id article_id created_at articles user_id created_at ... I need
I have these tables and values: Table1 ------------------------ ID | Value ------------------------ 2 |
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
Strange situation. If I have these tables: CREATE TABLE t1 (id INT, title VARCHAR(20),
Im trying to JOIN 2 tables and count the results. I have these tables;
I have these 3 tables: and I'm using this code to join them together
Consider a database(MSSQL 2005) that consists of 100+ tables which have primary keys defined

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.