1

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

2
  • tidy up your sql - learn about aliasing and can you refrain from using natural joins (think why) thanks ever so much. Commented Mar 9, 2011 at 5:34
  • 1
    i'm an expert c++ dev, but a noob when it comes to sql. i've googled around but didnt find much that says sql natural joins are bad in this case (i'm natural joining on indexes). can you elaborate on your statement and perhaps provide links? thanks Commented Mar 9, 2011 at 5:53

2 Answers 2

3

If speed is your problem then I'd make sure that you have foreign keys and their indexes defined in your schema. Also make sure you have indexes on all the criteria items you search by.

Not sure on the syntax (I'm an Oracle guy) but maybe something like this:

How to add foreign keys:

ALTER TABLE leaderboard_entry ADD FOREIGN KEY (leaderboard_id) REFERENCES leaderboard(leaderboard_id);

How to add indexes:

alter table leaderboard_entry add index using hash(leaderboard_id);


Also, using prepared statements and bind variables can help too. It will eliminate the parsing step once it is done once.

Here's a link describing how to do this:

http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

See the section "Prepared Statements".


Sign up to request clarification or add additional context in comments.

Comments

1

The problem: since I am making two queries to the database, this significantly increases the load time.

It is not true.

You have bad performance because your queries aren't optimized well (or at all). So stop thinking that 1 query is faster than 2.

Also - you cannot combine those 2 queries since they return different number of columns.

3 Comments

Any thoughts on how to optimize them? Thanks
@swinefeaster: remove all unnecessary from queries at first
there are no unnecessary from queries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.