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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:46:13+00:00 2026-06-06T04:46:13+00:00

I have this query which is a dependant query and taking much execution time

  • 0

I have this query which is a dependant query and taking much execution time

SELECT
  u.id,
  u.user_name,
  ifnull((select longitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude,
  ifnull((select latitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude,
  (select  created  from map  where user_id = 1  order by created desc   limit 1) as LatestTime
FROM users as u
WHERE id IN(SELECT
          user1_id FROM relation
        WHERE users.id = 1)
ORDER BY id;

I tried this query in (dependant)

SELECT
  u.id,
  u.user_name,
  m.map_id,
  m.longitude,
  m.latitude,
  m.Date as created
FROM users as u
  left join (select
           map_id,
           longitude,
           latitude,
           user_id,
           max(created) as `Date`
         from map
         group by user_id) as m
    on m.user_id = u.id
WHERE id IN(SELECT
          user1_id FROM relation
        WHERE users.id = 1)
ORDER BY id;

The problem is that the first query is dependent and working fine but taking much execution time. With the second query the problem is that it is not fetching the latest created time.
Now i want to optimise this query. The theme is that in subquery i am first making group then i am trying to get the last record of each group. and here is the tables structure.

users : id , user_name
map   : map_id ,  user_id ,longitude , latitude, created 
relations : id , user1_id , user2_id , relation
  • 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-06T04:46:15+00:00Added an answer on June 6, 2026 at 4:46 am

    Where performance is needed, subqueries in the SELECT clause are indeed a pain and have to be banished 🙂

    You can rewrite this part:

    SELECT
        u.id,
        u.user_name,
        ifnull((select longitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude,
        ifnull((select latitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude,
        (select  created  from map  where user_id = 1  order by created desc   limit 1) as LatestTime
    FROM users as u
    

    In:

    SELECT
        u.id,
        u.user_name,
        COALESCE(m1.longitude, 0) as longitude,
        COALESCE(m1.latitude, 0) as latitude
    FROM users u
    LEFT JOIN map m1 ON m1.user_id = u.id
    LEFT JOIN map m2 ON m2.user_id = m1.user_id AND m2.map_id > m1.map_id
    WHERE m2.map_id IS NULL
    

    I wrote a short explanation of the query structure in this answer. It’s a really nice trick to learn as it is more readable, subquery-less and performance wiser.

    I haven’t looked at the IN part yet but will if the above didn’t help.

    Edit1: You can extract the created date and use a MAX() instead.

    SELECT
        u.id,
        u.user_name,
        COALESCE(m1.longitude, 0) as longitude,
        COALESCE(m1.latitude, 0) as latitude,
        created.LatestTime
    FROM (SELECT MAX(created) FROM map WHERE user_id = 1) created
    INNER JOIN users u ON TRUE
    LEFT JOIN map m1 ON m1.user_id = u.id
    LEFT JOIN map m2 ON m2.user_id = m1.user_id AND m2.map_id > m1.map_id
    WHERE m2.map_id IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query, which was working fine. SELECT SUM(amount) FROM company.invoice_line WHERE item_id
So I have this app which needs to query entities from the Azure Tables
I have this query: mysql_select_db('scanner'); $query = SELECT * FROM scanner.proxy ORDER BY RAND()
I have a query which is taking a very very long time to execute
I have the following query: SELECT AVG(time) FROM (SELECT UNIX_TIMESTAMP(max(datelast)) - UNIX_TIMESTAMP(min(datestart)) AS time
I have this query (which works fine in mySQL cmdline), but when I run
I have this query which works correctly in MySQL. More background on it here
I have this query which returns a result set with Date(column) as nvarchar datatype.
I have this LINQ query which returns the indexes of all the items in
Right now I have this SQL query which is valid but always times out:

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.