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

  • Home
  • SEARCH
  • 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 7187807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:55:43+00:00 2026-05-28T18:55:43+00:00

I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE

  • 0

I have the following MySQL query:

SELECT value_1, (
    SELECT value_4 
    FROM table_1
    WHERE value_3 < value_1 
    ORDER BY value_3 DESC  
    LIMIT 1
)
AS result_value 
FROM table_2 
WHERE value_1 BETWEEN 1325372400000 AND 1328050800000  
ORDER BY value_1

Which returns 32 results and takes 6.6 seconds to run. The idea is to get the entries from table_1 where value_3 is “closest” to value_1 in table_2.

The two queries the query is comprised of, namely

SELECT value_1
AS result_value 
FROM table_2 
WHERE value_1 BETWEEN 1325372400000 AND 1328050800000  
ORDER BY value_1

and (for example)

SELECT value_4 
FROM table_1
WHERE value_3 < 1328050800000 
ORDER BY value_3 DESC  
LIMIT 1

each take 0.03 seconds to run. Computing the accumulated time for 32 result sets, the compound query should take no longer than 1 second, or probably even less (because I/O overhead for individual queries is not accounted for). Yet at 6.6 seconds, it takes much longer.

Why is that, how can I optimize it? Or is there another/superior way to reach my goal?

Update:

Table definitions:

table_1 (MyISAM, 700000 entries):

'id', 'int(10) unsigned', 'NO', 'PRI', NULL, 'auto_increment'
'value_3', 'bigint(20) unsigned', 'NO', 'UNI', '0', ''
'value_4', 'bigint(20) unsigned', 'NO', '', '0', ''

table_2 (MyISAM, 4000 entries):

'value_1', 'bigint(20) unsigned', 'NO', 'PRI', NULL, ''

EXPLAIN [query]:

'1', 'PRIMARY', 'table_2', 'range', 'PRIMARY,value_3_UNIQUE', 'PRIMARY', '8', NULL, '32', 'Using where; Using index'
'2', 'DEPENDENT SUBQUERY', 'table_1', 'index', 'value_3,value_3_value_4', 'value_3', '8', NULL, '1', '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-05-28T18:55:44+00:00Added an answer on May 28, 2026 at 6:55 pm

    Use a bit of mysql kung fu:

    SELECT * from
    (SELECT value_1, value_4
    FROM table_2 
    join table_1 on value_3 < value_1
    WHERE value_1 BETWEEN 1325372400000 AND 1328050800000  
    ORDER BY value_1, value_3 DESC) x
    GROUP BY value_1 
    ORDER BY value_1
    

    The "kung fu" is that with mysql when the columns you’re not grouping by are not aggregated (eg SUM() etc), you get the first row encountered for each group. If you use this technique on an ordered result set, you can get the value you’re after.

    Not only is this easier to code, but you’ll notice that just one pass is made over the tables (not one query per row as you tried). It should perform well.

    Edit:

    There has been conjecture by some commenters that this special form of GROUP BY is "indeterminate" and/or "not officially supported" etc. The documentation states that the row selected in "indeterminate", however I have never seen nor heard of mysql selecting any row other than the first encountered and using an order by on the inner select is used and relied upon in countless production queries.

    FWIW, I am happy to recommend this approach as being "reliable" and production-worthy.

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

Sidebar

Related Questions

I have the following sql query in mysql: SELECT * FROM _t_test WHERE pret
I have a problem with the following MySQL query: SELECT * FROM TPDSessions WHERE
I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have the following table: mysql> SELECT * FROM `bright_promotion_earnings`; +----+----------+------------+----------+-------+ | id |
At the moment I have the following MySQL query: SELECT COUNT(*) AS `count`, `v`.`value`
So I have following as part of my query SELECT * FROM $table WHERE
I have the following query: $sqltext = SELECT Score FROM HighScore . WHERE fbID='$_POST[fbID]'
I have the following code: $result = mysql_query(select * from ${db_name}_users limit 1); while
I have the following table: mysql> SELECT * FROM temp; +----+------+ | id |
I have the following mysql query which I am running with php like so.

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.