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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:29:47+00:00 2026-05-23T11:29:47+00:00

i have a very simple query that im trying to optimize, its taking 2~5

  • 0

i have a very simple query that im trying to optimize, its taking 2~5 secs to execute.

This is my CREATE TABLE

CREATE TABLE `artist` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) character set utf8 NOT NULL,
  `bio` MEDIUMTEXT character set utf8 DEFAULT NULL,
  `hits` INTEGER NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `album` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `artist_id` INTEGER NOT NULL,
  `title` VARCHAR(100) character set utf8 NOT NULL,
  `year` INTEGER,
  `hits` INTEGER NOT NULL,
  PRIMARY KEY (`id`),
KEY (`artist_id`)
);

CREATE TABLE `track` (
  `id` INTEGER NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) character set utf8 NOT NULL,
  `lyric` MEDIUMTEXT character set utf8,
  `album_id` INTEGER NOT NULL,
  `hits` INTEGER NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`),
KEY (`album_id`)
);

ALTER TABLE `album` ADD FOREIGN KEY (artist_id) REFERENCES `artist` (`id`);
ALTER TABLE `track` ADD FOREIGN KEY (album_id) REFERENCES `album` (`id`);

and this is the query im running

SELECT DISTINCT artist.name, track.name
FROM track
LEFT JOIN album ON track.album_id = album.id
LEFT JOIN artist ON album.artist_id = artist.id
ORDER BY track.hits DESC
LIMIT 5 

Explain selects show this:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  track   ALL     NULL    NULL    NULL    NULL    103796  Using temporary; Using filesort
1   SIMPLE  album   eq_ref  PRIMARY     PRIMARY     4   lyrics.track.album_id   1    
1   SIMPLE  artist  eq_ref  PRIMARY     PRIMARY     4   lyrics.album.artist_id  1    

I’m new to MySQL but i guess using Using temporary; Using filesort is bad and thats why the query is very slow, can you guys hint me here? thanks!

update: The main problem here is that the very same song can be 5 times in the DB with different ID’s, because the same song can be in different albums. If i dont use distinct, this doesnt happen, bust i must for this reason

  • 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-23T11:29:48+00:00Added an answer on May 23, 2026 at 11:29 am

    You can get it to use an index by adding

    create index idx_tracks_on_album_id_name_hits on track(album_id, name, hits);
    

    And since you are doing a DISTINCT across two tables, there will be no index to possibly find the unique rows so it puts it into a temp table to get rid of the duplicates.

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

Sidebar

Related Questions

I have a very simple query that's giving me unexpected results. Hints on where
I have a very simple query $this->db->select('id, title'); $this->db->order_by('id', 'DESC'); $query = $this->db->get('journal'); foreach($query->result()
I have a very simple table and I can happily query it using LINQ
i have very simple problem. I need to create model, that represent element of
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple query that only returns one record. When I try
Trying to perform a very simple task here. I have an <ol> that contains
I have a ViewModel that is a very simple filter object like this: public
I have a very simple table that contains a list of 'victims' and the
I have a very simple linq to sql query in C#: int acctNum =

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.