I have two complex-ish sql statements. One of them generates a count and the other queries for a leaderboard, which has different columns. In the php script I choose only one row out of the second returned query, and then combine the count from the first query result and return it to the caller.
The problem: since I am making two queries to the database, this significantly increases the load time. I would like to combine the two queries to optimize the call.
Here are my queries:
select count(*) as leaderboard_entry_history_tally
from leaderboard_entry
natural join user
natural join leaderboard where
(leaderboard_load_key = 'fr-en-colors' or leaderboard_load_key = '-fr-en-colors' )
and leaderboard_quiz_mode = '0' and user_id = 37
set @t1=0; select
NOW()-leaderboard_entry_timestamp as leaderboard_entry_age_in_some_units ,
TO_DAYS(NOW())-TO_DAYS(leaderboard_entry_timestamp) as leaderboard_entry_age_in_days , leaderboard_entry.user_id ,
leaderboard_entry.leaderboard_entry_id ,
leaderboard_entry.leaderboard_id ,
leaderboard_entry.leaderboard_entry_timestamp ,
leaderboard_entry.leaderboard_entry_friendly_load_key , leaderboard_entry.leaderboard_entry_session_uuid ,
leaderboard_entry.leaderboard_entry_num_mistakes ,
leaderboard_entry.leaderboard_entry_num_unique_cues ,
leaderboard_entry.leaderboard_entry_latitude ,
leaderboard_entry.leaderboard_entry_longitude ,
leaderboard_entry.leaderboard_entry_app_market_code ,
leaderboard.leaderboard_quiz_mode ,
leaderboard.leaderboard_load_key ,
leaderboard_entry.leaderboard_entry_elapsed_time_ms
from leaderboard_entry
natural join leaderboard
where (leaderboard_load_key = 'fr-en-colors' or leaderboard_load_key = '-fr-en-colors' )
and leaderboard_quiz_mode = '0'
and user_id in (37)
order by leaderboard_entry_timestamp desc limit 0, 1
I have looked through other posted solutions. Some of these use temporary tables, which didn't seem to work when I tried it (got syntax errors).
I also tried nested queries, but couldn't get this to work even with simple stuff like the following:
select
(
select count(*) from leaderboard_entry
) , * from leaderboard_entry_timestamp
Any help would be much appreciated!
Thanks swine